[恢]hdu 1312

2011-12-26 09:19:37

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

题意:在铺满红砖和黑砖的房间里,一个人每次只能移动到黑砖,问有多少个砖块是可达的。

mark:bfs和dfs都可以。。。数据又很小,才20。

代码:

# include <stdio.h>
# include <string.h>


int ans, n, m ;
char graph[30][30] ;
int visited[30][30] ;


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


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



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