#abc325c. C - Sensors
C - Sensors
Score : points
问题描述
在一个 行和 列的网格上,放置了零个或多个传感器。令 表示从上到下的第 行以及从左到右的第 列的方格。
每个方格中是否包含传感器由长度为 的字符串 给定。若 的第 个字符为 #
,则表示 中包含一个传感器。
这些传感器与其所在的行、列或对角线相邻的其他传感器进行交互,并作为一个整体工作。这里,我们称单元格 和单元格 在水平、垂直或对角线上相邻,当且仅当 。
注意,如果传感器 与传感器 交互且传感器 也与传感器 交互,则传感器 和传感器 之间也会发生交互。
将相互交互的传感器视为单个传感器,请找出该网格上的传感器总数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are zero or more sensors placed on a grid of rows and columns. Let denote the square in the -th row from the top and the -th column from the left.
Whether each square contains a sensor is given by the strings , each of length . contains a sensor if and only if the -th character of 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 and a cell are said to be horizontally, vertically, or diagonally adjacent if and only if .
Note that if sensor interacts with sensor and sensor interacts with sensor , then sensor and sensor also interact.
Considering the interacting sensors as one sensor, find the number of sensors on this grid.
Constraints
- and are integers.
- is a string of length where each character is
#
or.
.
Input
The input is given from Standard Input in the following format:
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
- The sensor at
- The interacting sensors at
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