A - Red and Black (bfs || dfs)

网址:https://vjudge.net/contest/241948#problem/A

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

simple out

45
59
6
13

(1)BFS 

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct node{
	int x,y,step;
};
int m,n;
char str[40][40];
int vis[40][40];
bool operator < (node a,node b){
	return a.step>b.step;
}
void BFS(int x1,int y1){
	priority_queue<node>que;
	node e1,e2;
	memset(vis,0,sizeof(vis));
	int ans=0;
	e1.x=x1, e1.y=y1, e1.step=0;
	que.push(e1);
	while(!que.empty()){
		e1=que.top();
		ans++;
		que.pop();
		vis[e1.x][e1.y]=1;
		int p[4][2]={1,0,-1,0,0,1,0,-1};
		for(int i=0;i<4;i++){
			e2.x=e1.x+p[i][0];
			e2.y=e1.y+p[i][1];
			if(str[e2.x][e2.y]=='#'||vis[e2.x][e2.y]==1) continue;
			if(e2.x<0||e2.x>=n||e2.y<0||e2.y>=m) continue;
			else e2.step=e1.step+1;
			que.push(e2);
			vis[e2.x][e2.y]=1; 
		}
	}
	cout<<ans<<endl;
}
int main()
{
	int stax,stay;
	while(cin>>m>>n&&n&&m){
		memset(str,0,sizeof(str));
		for(int i=0;i<n;i++){
			scanf("%s",&str[i]);
			for(int j=0;j<m;j++){
				if(str[i][j]=='@'){
					stax=i,stay=j;
				}
			}
		}
		BFS(stax,stay);
	}
}

(2)DFS 

#include<iostream>
#include<string.h>
using namespace std;
char str[100][100];
int vis[100][100];
int m,n;
int ans;
int x2,y2;
void DFS(int x1,int y1){
	//int ans=0;
	//memset(vis,0,sizeof(vis));
	vis[x1][y1]=1;
	ans++;
	int p[4][2]={1,0,-1,0,0,1,0,-1};
	for(int i=0;i<4;i++){
		int x2=x1+p[i][0];
		int y2=y1+p[i][1];
		if(str[x2][y2]=='#') continue;
		if(x2<0||x2>=n||y2<0||y2>=m) continue;
		if(vis[x2][y2]==1) continue;
			DFS(x2,y2);
	}
}
int main()
{
	int stax,stay;
	while(cin>>m>>n&&(n+m)){
		memset(str,0,sizeof(str));
		memset(vis,0,sizeof(vis));
		ans=0;
		for(int i=0;i<n;i++){
			scanf("%s",&str[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(str[i][j]=='@'){
					stax=i;stay=j;
					DFS(stax,stay);
					break;
				}
			}
		}
	    cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41555192/article/details/81349684