Lake counting
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:20003 |
|
Accepted:10063 |
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 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.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. question Analysis: A string area represents a map, W represents water, And the rest represents land. The water connection represents a lake. We need to calculate the number of lakes in this map. Algorithm Analysis: Read strings... Not complete to be continued! # Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Char map [101] [101];
Int VT [101] [101];
Int dir [8] [2] = {0,-1}, {0, 1}, {-1, 0}, {1, 0}, {1, 1 },{ 1,-1 },{-1, 1 },{-1,-1 }};
Int n, m;
Void DFS (int x, int y)
{
Int I;
For (I = 0; I <8; I ++)
{
Int xx = x + dir [I] [0];
Int YY = Y + dir [I] [1];
If (Map [XX] [YY] = 'W' & XX> = 0 & XX <M & YY> = 0 & YY <n & VT [XX] [YY] = 0)
{
VT [XX] [YY] = 1;
DFS (XX, YY );
}
}
}
Int main ()
{
Int CNT;
Int I, J;
While (scanf ("% d % * C", & M, & N )! = EOF)
{
If (M = 0 & n = 0)
Break;
CNT = 0;
Memset (Vt, 0, sizeof (VT ));
For (I = 0; I <m; I ++)
{
Scanf ("% s", map [I]);
}
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
{
If (Map [I] [J] = 'W' & VT [I] [J] = 0)
{
VT [I] [J] = 1;
CNT ++;
DFS (I, j );
}
}
}
Printf ("% d \ n", CNT );
}
Return 0;
}