Red and Black (DFS&BFS)

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). DFS&BFS

#include <stdio.h>
#include  <string.h>
using namespace std;
#define MAXN 50
char str[50][50];
int vis[MAXN][MAXN];
int d[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,ans=1;
void DFS(int x1,int y1)
{
	vis[x1][y1]=1;
	int x2,y2;
	for(int i=0;i<4;i++)
	{
		x2=x1+d[i][0];y2=y1+d[i][1];
		if(x2<0||x2>=m||y2<0||y2>=n) continue;
		if(str[x2][y2]=='#') continue;
		if(vis[x2][y2]==1) continue;
		if(str[x2][y2]=='.')
		{
			ans++;
			vis[x2][y2]=1;
			DFS(x2,y2);
			
		}
	}
}
int main()
{
	int stx,sty;
	while(scanf("%d%d",&n,&m)&&(n+m))
	{
		for(int i=0;i<m;i++)
			scanf("%s",str[i]);
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			if(str[i][j]=='@') stx=i,sty=j;
		//	DFS(stx,sty);
		}
		DFS(stx,sty);
		printf("%d\n",ans);
		memset(vis,0,sizeof(vis));
		ans=1;
	}
}
、、、、、、
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define MAXN 50
struct node{
	int x,y,step;
	bool friend operator < (node a,node b)
	{
		return a.step<b.step;
	}
};
int n,m;
char str[MAXN][MAXN];
int vis[MAXN][MAXN];
int d[4][2]={0,1,0,-1,1,0,-1,0};
void BFS(int x1,int y1)
{
	priority_queue<node>que;
	node e1,e2;
	memset(vis,0,sizeof(vis));
	e1.x=x1,e1.y=y1,e1.step=1;
	int ans=1;
	que.push(e1);
	vis[x1][y1]=1;
	while(!que.empty())
	{
		e1=que.top();
		que.pop();
		for(int i=0;i<4;i++)
		{
			e2.x=e1.x +d[i][0];e2.y=e1.y+d[i][1];
			if(e2.x<0||e2.x>=m||e2.y<0||e2.y>=n) continue;
			if(str[e2.x][e2.y]=='#')  continue;
			if(vis[e2.x][e2.y]==1) continue;
			if(str[e2.x][e2.y]=='.')
			{
				e2.step=e1.step+1;
				que.push(e2);
				vis[e2.x][e2.y]=1;
				ans++;
			}
			
		}
		
	}
	printf("%d\n",ans);
	
}
int main()
{
	int stx,sty;
	while(scanf("%d%d",&n,&m)&&(n+m))
	{
		for(int i=0;i<m;i++) scanf("%s",str[i]);
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(str[i][j]=='@')
				stx=i,sty=j;
			}
		}
		BFS(stx,sty);
	}
}

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/81350105