Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

时间:2021-06-07 04:02:00

Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile

‘#’ - a red tile

‘@’ - a man on a black tile(appears exactly once in a data set)

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9

….#.

…..#

……

……

……

……

……

@…

.#..#.

11 9

.#………

.#.#######.

.#.#…..#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#…….#.

.#########.

………..

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

.

…@…

.

..#.#..

..#.#..

0 0

Sample Output

45

59

6

13

BFS

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w,h,sx,sy,cnt;
char map[30][30];
int vis[30][30];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
struct Node
{
int x;
int y;
}Q[450];
Node s;
void bfs()
{
int front=0,rear=0;
Q[rear++]=s;
while(front<rear)
{
Node t=Q[front++];
for(int i=0;i<4;i++)
{
int x0=t.x+dx[i];
int y0=t.y+dy[i];
Node f;
f.x=x0;f.y=y0;
if(!vis[f.x][f.y]&&f.x>=0&&f.x<h&&f.y>=0&&f.y<w&&map[f.x][f.y]!='#')
{
vis[f.x][f.y]=1;
Q[rear++]=f;
if(map[f.x][f.y]=='.')
cnt++;
}
}
}
}
int main()
{
while(~scanf("%d%d",&w,&h))
{if(w==0||h==0)
break;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);
for(int j=0;j<w;j++)
if(map[i][j]=='@')
{
s.x=i;
s.y=j;
break;
}
}
cnt=0;
bfs();
printf("%d\n",cnt+1);}
return 0;
}

DFS

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w,h,sx,sy,cnt;
char map[30][30];
int vis[30][30];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
void dfs(int x,int y)
{
if(map[x][y]=='.')
cnt++;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
return;
for(int i=0;i<4;i++)
{
int x0=x+dx[i];
int y0=y+dy[i];
if(!vis[x0][y0])
{
vis[x0][y0]=1;
dfs(x0,y0);
}
}
}
int main()
{
while(~scanf("%d%d",&w,&h))
{if(w==0||h==0)
break;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);
for(int j=0;j<w;j++)
if(map[i][j]=='@')
{
sx=i;
sy=j;
break;
}
}
cnt=0;
dfs(sx,sy);
printf("%d\n",cnt+1);}
return 0;
}
//6 9
//....#.
//.....#
//......
//......
//......
//......
//......
//#@...#
//.#..#.

可以看出:写BFS时一般要有结构体来表示状态。

求最短路一般用BFS,其他的可能更多用的是DFS

两者的关键都在于找转态。

版权声明:本文为博主原创文章,未经博主允许不得转载。