您的位置:首页 > 其它

HDU 1312 Red and Black

2012-03-23 00:23 483 查看
这题就是比较水的一道搜索题了,BFS跟DFS都能做,直接看代码吧!

AC code:

View Code

#include <iostream>
#define MAX 50
using namespace std;
int w, h;
char map[MAX][MAX];
int dir[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int count;
bool inmap(int x,int y)
{
return x >= 0 && x < h && y >= 0 && y < w;
}
void dfs(int x, int y)
{
map[x][y] = '#';//为了对起点进行改动要把这句放在前面
for(int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(inmap(tx, ty) && map[tx][ty] != '#')
{
count ++;
dfs(tx, ty);
}
}
}
int main()
{
while(scanf("%d%d", &w, &h) != EOF && w)
{
int i, j;
int sx, sy;
count = 1;
for(i = 0; i < h; i++)
scanf("%s", map[i]);
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
if(map[i][j] == '@')
{
sx = i;
sy = j;
}
}
dfs(sx, sy);
printf("%d\n", count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: