http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
Meaning
In the rectangular orchard of H * W, there are three kinds of fruit trees of apple, pear and Mandarin, adjacent (up and down) of the same fruit trees belong to the same area, give out the distribution of fruit trees in the orchard, ask how many regions in total. (The original picture of the apple as リ, pear for カ, mandarin Orange for ミ, a total of 10 regions)
Input:
Multiple sets of data, the first behavior of each group of data two integers h,w (h <=, W <=), h = 0 and W = 0 represents the end of input. The following H Line W column indicates orchard fruit tree distribution, Apple is @, pear is #, Mandarin is *.
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45524.htm
Output:
For each set of data, the number of its regions is output.
Complete code:
/*0ms,1360kb*/
#include <cstdio>
#include <cstring>
char grid[105][105], flag;
BOOL Dfs (int i, int j)
{
if (!grid[i][j] | | grid[i][j]!= flag) return false;
GRID[I][J] = 0;
DFS (I-1, j); DFS (i + 1, j); DFS (i, j-1); DFS (I, j + 1);
return true;
}
int main ()
{
int x, Y, I, J, CNT;
while (scanf ("%d%d\n", &y, &x), x)
{
memset (grid, 0, sizeof (GRID));
CNT = 0;
for (i = 1; I <= y; ++i)
gets (Grid[i] + 1);
for (i = 1; I <= y; ++i) for
(j = 1; j <= x; ++j)
{
flag = grid[i][j];
if (Dfs (i, j)) ++cnt
}
printf ("%d\n", CNT);
}
return 0;
}