Q: A map of m * n, where the grid is * or @. For @, the grid is a block that is connected horizontally, vertically, and diagonally, there are multiple @ blocks in total.
--> Repeat the simple winter vacation search questions and do not use scanf or getchar for input.
[Cpp] # include <cstdio>
# Include <iostream>
# Include <cstring>
Using namespace std;
Const int maxn = 100 + 10;
Char MAP [maxn] [maxn];
Int cnt, m, n;
Int dx [] = {-1,-1,-1, 0, 1, 1, 1, 0 };
Int dy [] = {-1, 0, 1, 1, 1, 0,-1,-1 };
Bool vis [maxn] [maxn];
Void dfs (int x, int y)
{
Vis [x] [y] = 1;
For (int I = 0; I <8; I ++)
{
Int newx = x + dx [I];
Int newy = y + dy [I];
If (newx> = 0 & newx <m & newy> = 0 & newy <n & MAP [newx] [newy] = '@'&&! Vis [newx] [newy])
Dfs (newx, newy );
}
}
Int main ()
{
Int I, j;
While (scanf ("% d", & m, & n) = 2 & m)
{
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
Cin> MAP [I] [j];
Cnt = 0;
Memset (vis, 0, sizeof (vis ));
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
{
If (MAP [I] [j] = '@'&&! Vis [I] [j])
{
Cnt ++;
Dfs (I, j );
}
}
Printf ("% d \ n", cnt );
}
Return 0;
}
# Include <cstdio>
# Include <iostream>
# Include <cstring>
Using namespace std;
Const int maxn = 100 + 10;
Char MAP [maxn] [maxn];
Int cnt, m, n;
Int dx [] = {-1,-1,-1, 0, 1, 1, 1, 0 };
Int dy [] = {-1, 0, 1, 1, 1, 0,-1,-1 };
Bool vis [maxn] [maxn];
Void dfs (int x, int y)
{
Vis [x] [y] = 1;
For (int I = 0; I <8; I ++)
{
Int newx = x + dx [I];
Int newy = y + dy [I];
If (newx> = 0 & newx <m & newy> = 0 & newy <n & MAP [newx] [newy] = '@'&&! Vis [newx] [newy])
Dfs (newx, newy );
}
}
Int main ()
{
Int I, j;
While (scanf ("% d", & m, & n) = 2 & m)
{
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
Cin> MAP [I] [j];
Cnt = 0;
Memset (vis, 0, sizeof (vis ));
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
{
If (MAP [I] [j] = '@'&&! Vis [I] [j])
{
Cnt ++;
Dfs (I, j );
}
}
Printf ("% d \ n", cnt );
}
Return 0;
}