#abc325c. C - Sensors

C - Sensors

Score : 300300 points

问题描述

在一个 HH 行和 WW 列的网格上,放置了零个或多个传感器。令 (i,j)(i, j) 表示从上到下的第 ii 行以及从左到右的第 jj 列的方格。

每个方格中是否包含传感器由长度为 WW 的字符串 S1,S2,,SHS_1, S_2, \ldots, S_H 给定。若 SiS_i 的第 jj 个字符为 #,则表示 (i,j)(i, j) 中包含一个传感器。

这些传感器与其所在的行、列或对角线相邻的其他传感器进行交互,并作为一个整体工作。这里,我们称单元格 (x,y)(x, y) 和单元格 (x,y)(x', y') 在水平、垂直或对角线上相邻,当且仅当 max(xx,yy)=1\max(|x-x'|,|y-y'|) = 1

注意,如果传感器 AA 与传感器 BB 交互且传感器 AA 也与传感器 CC 交互,则传感器 BB 和传感器 CC 之间也会发生交互。

将相互交互的传感器视为单个传感器,请找出该网格上的传感器总数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are zero or more sensors placed on a grid of HH rows and WW columns. Let (i,j)(i, j) denote the square in the ii-th row from the top and the jj-th column from the left.
Whether each square contains a sensor is given by the strings S1,S2,,SHS_1, S_2, \ldots, S_H, each of length WW. (i,j)(i, j) contains a sensor if and only if the jj-th character of SiS_i is #.
These sensors interact with other sensors in the squares horizontally, vertically, or diagonally adjacent to them and operate as one sensor. Here, a cell (x,y)(x, y) and a cell (x,y)(x', y') are said to be horizontally, vertically, or diagonally adjacent if and only if max(xx,yy)=1\max(|x-x'|,|y-y'|) = 1.
Note that if sensor AA interacts with sensor BB and sensor AA interacts with sensor CC, then sensor BB and sensor CC also interact.

Considering the interacting sensors as one sensor, find the number of sensors on this grid.

Constraints

  • 1H,W10001 \leq H, W \leq 1000
  • HH and WW are integers.
  • SiS_i is a string of length WW where each character is # or ..

Input

The input is given from Standard Input in the following format:

HH WW

S1S_1

S2S_2

\vdots

SHS_H

Output

Print the answer.

Sample Input 1

5 6
.##...
...#..
....##
#.#...
..#...

Sample Output 1

3

When considering the interacting sensors as one sensor, the following three sensors exist:

  • The interacting sensors at (1,2),(1,3),(2,4),(3,5),(3,6)(1,2),(1,3),(2,4),(3,5),(3,6)
  • The sensor at (4,1)(4,1)
  • The interacting sensors at (4,3),(5,3)(4,3),(5,3)

Sample Input 2

3 3
#.#
.#.
#.#

Sample Output 2

1

Sample Input 3

4 2
..
..
..
..

Sample Output 3

0

Sample Input 4

5 47
.#..#..#####..#...#..#####..#...#...###...#####
.#.#...#.......#.#...#......##..#..#...#..#....
.##....#####....#....#####..#.#.#..#......#####
.#.#...#........#....#......#..##..#...#..#....
.#..#..#####....#....#####..#...#...###...#####

Sample Output 4

7

update @ 2024/3/10 01:47:53