Description
Due to recent rains, water has pooled in varous places in Farmer John's field, which is represented by a rectangle of N x m (1 <= n <= 100; 1 <= m <= 100) squares. each square contains either water ('W') or dry land ('. '). farmer John wowould like to figure out How sort ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a distriof Farmer John's field, determine how he ponds he has.
Input
* Line 1: two space-separated integers: N and m
* Lines 2 .. n + 1: m characters per line representing one row of Farmer John's field. each character is either 'W' or '. '. the characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12
W ...... ww.
. Www... www
... Ww... ww.
......... Ww.
..
... W ..
. W. w... ww.
W. W. W... w.
. W. w... w.
... W.
Sample output
3
Hint
Output details:
There are three ponds: one in the upper left, one in the lower left, and one along the right side.
# Include <iostream> </P> <p> using namespace STD; </P> <p> const int max = 110; <br/> char map [Max] [Max]; <br/> int vis [Max] [Max]; </P> <p> void DFS (int x, int y) <br/>{< br/> If (vis [x] [Y] = 1 | map [x] [Y] = '. '| map [x] [Y] = 0) return; // if you have ever accessed this grid, or the current grid is an open space, or the current grid is 0 (that is, it is outbound) returns <br/> vis [x] [Y] = 1; <br/> // recursively accesses the eight surrounding grids <br/> DFS (x-1, Y-1 ); DFS (x-1, Y); DFS (x-1, Y + 1); <br/> DFS (x, Y-1); DFS (X, Y + 1 ); <br/> DFS (x + 1, Y-1); DFS (x + 1, Y); DFS (x + 1, Y + 1); <br/>}</P> <p> int main () <br/> {<br/> memset (MAP, 0, sizeof (MAP); // The value is initialized to 0 and serves as the boundary. <br/> memset (VIS, 0, sizeof (VIS); // Initialization is 0, indicating that no access has been made <br/> int m, n, CNT = 0; <br/> CIN> m> N; </P> <p> for (INT I = 1; I <= m; ++ I) <br/> for (Int J = 1; j <= N; ++ J) <br/> CIN> map [I] [J]; // place the map in the middle of the matrix, with a circle of 0 around it to facilitate the judgment of the perimeter </P> <p> for (INT I = 1; I <= m; + + I) <br/> for (Int J = 1; j <= N; ++ J) <br/> {<br/> If (! Vis [I] [J] & map [I] [J] = 'W ') // If an unaccessed grid is found and the current grid is water <br/>{< br/> ++ CNT; <br/> DFS (I, j ); <br/>}< br/> cout <CNT <Endl; </P> <p> return 0; <br/>}</P> <p>