← 返回 google 的题目列表Count the Number of Lakes Within the Island Containing a Given Cell
类型:online_judge
Problem: Count the Number of Lakes Within the Island Containing a Given Cell
You are given an m x n binary matrix grid:
1 represents land
0 represents water
You are also given a coordinate (r, c) (0-indexed).
Definitions:
Island: a set of land cells connected via 4-directional adjacency.
Lake: a 4-directionally connected water component that lies inside that island and does not touch the grid boundary (i.e., it is fully enclosed by land).
Return the number of lakes inside the island that contains (r, c).
Special cases
If grid[r][c] == 0, output 0.
If the island containing (r, c) has no lakes, output 0.
Input (stdin)
m n
<m lines of 0/1 strings of length n>
r c
Output (stdout)
lakes
Constraints
1 <= m, n <= 2000
Target near O(mn) time.
Example 1
Input:
5 5
11111
10001
10101
10001
11111
2 2
Output:
1
Example 2
Input:
3 3
010
010
010
1 1
Output:
0
Example
Input
5 5
11111
10001
10101
10001
11111
2 2
Output
1