DFS Exercise - Prince Saves Princess

 

One day, Mr. Tuan Tou dreamed that he became a prince, but unfortunately, his princess was captured by the hateful witch. So Tuantoujun used the power of the whole country to learn that his princess was caught by a witch into a labyrinth. Since only Mr. Tuantou in the whole country can climb over the city wall outside the labyrinth, Mr. Tuantou himself took the road to save his princess.

It happened that the witch went out, and the maze was not too big, so Mr. Garantou could talk to the princess directly, so the two began to approach each other. Each move can only move one square in four directions, up, down, left, and right, and cannot walk into the position of the wall. Tuantoujun is eager to save the princess, and must take two steps in one direction at a time (allowing to cross the walls in the maze); the princess is weak and can only take one step at a time. In this labyrinth, ask if Suantou-kun can rescue the princess.

input format

The first line inputs two integers n(1≤n≤100), m(1≤m≤100) represents the row and column of the maze.

Then there is an n×m map, which consists '.'of , '#', , 'w'and 'g'these four parts. '.'Indicates the traversable road, '#'the wall of the labyrinth, 'w'the place where the prince begins, and the place where the 'g'princess begins.

output format

The output prince is not able to rescue his own princess, if it can be rescued, it will be output "yes", otherwise it will be output "no".

Sample input copy

1 8
w....#.g

Sample output copy

yes

 


Ideas:

The key to this question is that two searches are required, representing the paths the prince and the princess have traveled.

Then see if they can walk through the same spot

If you can, explain that it can be rescued

 

Code:

#include<iostream>
using namespace std;
int n,m;
int ax,ay,bx,by;
char s[105][105];
int w[105][105],g[105][105]; //记录王子公主所经过的点 
//int step_2[4][2] = {
   
   {2,0},{-2,0},{0,2},{0,-2}};
int step[4][2] = {
   
   {1,0},{-1,0},{0,1},{0,-1}};
int vis[105][105][3];  //标记是否走过,第三维度表示是公主走的还是王子


void dfs(int x, int y, int tar)
{
	if(s[x][y]  == '#')
		return ;
	if(tar == 2) //王子 
		w[x][y] = 1; //记录王子经过的点 
	if(tar == 1)
		g[x][y] = 1; //记录公主经过的点 
						
	for(int i=0;i<4;i++){
		int tx = x + tar * step[i][0];
		int ty = y + tar * step[i][1];
		if(tx<=0 || tx>n || ty<=0 || ty>m)
			continue;
		if(!vis[tx][ty][tar]){
			vis[tx][ty][tar] = true;
			dfs(tx, ty, tar);	
		}		
	}
	return ;
} 

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1; i<=n;i++){
		for(int j=1; j<=m;j++){
			cin>>s[i][j];
			if(s[i][j] == 'w'){ //记录王子的坐标 
				ax = i; ay = j;
			}
			if(s[i][j] == 'g'){
				bx = i; by = j;
			}	
		}
	}
	dfs(ax, ay, 2); //由王子进行搜索,标记出所有王子到达的点 
	dfs(bx, by, 1);//由公主进行搜索,标记出所有公主到达的点
	for(int i=1; i<=n;i++){
		for(int j=1; j<=m;j++){//若王子公主能到达同一点,则可以救出 
			if(w[i][j] == g[i][j] && w[i][j] && g[i][j]){
				cout<<"yes"<<endl;
				return 0;
			}
		}
	}
	cout<<"no"<<endl;
	return 0; 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390940&siteId=291194637
dfs