[恢]hdu 1241

2011-12-26 09:33:25

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1241

题意:给map,求'@'的联通片数。

mark:注意相邻是指周围8格区域内。循环内套bfs或dfs都可以。

代码:

# include <stdio.h>


char graph[110][110] ;
int n, m ;


void dfs (int x, int y)
{
int i, xx, yy ;
int tab[8][2] = {-1, -1, -1, 0, -1, 1,
0, -1, 0, 1,
1, -1, 1, 0, 1, 1} ;
graph[x][y] = '-' ;
for (i = 0 ;i < 8 ; i++)
{
xx = x+tab[i][0] ;
yy = y+tab[i][1] ;
if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue ;
if (graph[xx][yy] != '@') continue ;
dfs (xx, yy) ;
}
}


int main ()
{
int i, j, ans ;
while (~scanf ("%d %d%*c", &n, &m) && (n||m))
{
for (i = 0 ; i < n ; i++)
scanf ("%s%*c", graph[i]) ;
ans = 0 ;
for (i = 0 ; i < n ; i++)
for (j = 0 ; j < m ; j++)
if (graph[i][j] == '@')
{
ans++ ;
dfs(i, j) ;
}
printf ("%d\n", ans) ;
}
return 0 ;
}



posted @ 2012-01-06 23:46  Seraph2012  阅读(122)  评论(0编辑  收藏  举报