题目大意:一个人站在一块黑色瓷砖上,他只能向四个方向走,不能走到红色瓷板上,求能走过的黑色瓷板,走过了的黑色瓷板不算
解题思路:bfs, dfs均可,,,,水题
BFS:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
struct node
{
int x, y;
};
const int maxn = 25;
char room[maxn][maxn];
int w, h;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int bfs(int x, int y);
int main()
{
int sx, sy;
while(true)
{
scanf("%d %d", &w, &h);
if(0 == w && 0 == h)
break;
for(int i = 0; i < h; i++)
{
scanf("%s", room[i]);
for(int j = 0; j < w; j++)
{
if(room[i][j] == '@')
{
sx = i, sy = j;
}
}
}
printf("%d\n", bfs(sx, sy));
}
return 0;
}
int bfs(int x, int y)
{
int val = INT_MIN;
queue<node> que;
node t, tmp;
t.x = x; t.y = y;
room[t.x][t.y] = '#';
que.push(t);
int nu = 1;
while(!que.empty())
{
tmp = que.front();
que.pop();
for(int i = 0; i < 4; i++)
{
t.x = tmp.x + dir[i][0];
t.y = tmp.y + dir[i][1];
if(t.x >= 0 && t.x < h && t.y >= 0 && t.y < w && room[t.x][t.y] == '.')
{
room[t.x][t.y] = '#';
que.push(t);
nu++;
}
}
}
return nu;
}