luogu#P12019. [NOISG 2025 Finals] Flooding
[NOISG 2025 Finals] Flooding
题目描述
Pavementland is a rectangle-shaped city, which can be modelled as a grid of cells. The rows of the grid are numbered to from north to south, and the columns of the grid are numbered to from west to east. We refer to the cell located at row and column of the grid as cell .
In the grid, each cell is either empty or contains a building. At least one cell is empty.
Due to a monsoon surge, flash floods are occurring throughout Pavementland. Initially, one empty cell becomes flooded with water by the rain. Then, the water flows according to the following rules:
- If an empty cell is adjacent to at least one flooded cell, it becomes flooded.
- If a cell containing a building is adjacent to at least two flooded cells, the building collapses and the cell becomes flooded.
Note that a cell is adjacent to another cell if they share an edge. A cell is adjacent to at most four other cells. Further note that water may not flow outside the grid. Let be the number of cells that would be flooded after the process if the cell were initially flooded.
City officials are seeking to forecast the extent of flash floods in all possible scenarios. Help them determine the sum of over all empty cells .
输入格式
Your program must read from standard input.
The first line of input contains two space-separated integers and .
The next lines of input each contain a binary string of length . If the -th character of the -th line is , then the cell is empty. If the -th character of the -th line is , then the cell contains a building.
输出格式
Your program must print to standard output.
Output a single integer, the sum of over all empty cells .
3 3
000
011
010
46
5 5
00101
01011
11010
01101
11000
182
1 10
1101011100
6
提示
Subtasks
For all test cases, the input will satisfy the following bounds:
- There is at least one empty cell in the grid.
Your program will be tested on input instances that satisfy the following restrictions:
Subtask | Marks | Additional Constraints |
---|---|---|
Sample test cases | ||
No additional constraints |
Sample Test Case 1 Explanation
This test case is valid for subtasks to .
If cells , or were initially flooded, the entire grid would become flooded after the process. If cell were initially flooded, only cell would become flooded after the process. Hence, the output is .
Sample Test Case 2 Explanation
This test case is valid for subtasks to .
Sample Test Case 3 Explanation
This test case is valid for all subtasks.