UVA 572 - Oil Deposits(DFS)
用DFS把相邻的@字符标记掉,并统计块的个数就好了。[cpp]
#include <stdio.h>
int m, n;
char map[105][105];
int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, // 定义8个方向数组
0, 1, 1, -1, 1, 0, 1, 1};
void dfs(int x, int y) {
map[x][y] = '*';
for (int i=0; i<8; i++) {
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx<1 || xx>m) continue;
if (yy<1 || yy>n) continue;
if ('*' == map[xx][yy]) continue;
dfs(xx, yy);
}
}
int main() {
while (scanf("%d%d", &m, &n)) {
if (0==m && 0==n)
break; www.zzzyk.com
for (int i=1; i<=m; i++)
scanf("%s", map[i]+1);
int nCount = 0;
for (int i=1; i<=m; i++)
for (int j=1; j<=n; j++)
if ('@' == map[i][j]) {
dfs(i, j);
nCount++;
}
printf("%d\n", nCount);
}
return 0;
}
补充:软件开发 , C++ ,