[Blue Bridge Cup] Crossing the minefield in the 2015 finals (DFS || BFS)

Title description

The X-Star’s tank chariot is very strange. It must alternately pass through the positive energy radiation area and the negative energy radiation area to maintain normal operation, otherwise it will be scrapped.
A certain tank needs to go from zone A to zone B (zones A and B are safe zones and have no positive or negative energy characteristics). How can the path be the shortest?
The known map is a square matrix, with letters marking areas A and B, and other areas are marked with positive or negative signs to indicate positive and negative energy radiation areas, respectively.

Insert picture description here

Tank cars can only move to adjacent areas horizontally or vertically.

enter

The first line of input is an integer n, which represents the size of the square matrix. 4<=n<100 The
next is n lines, each line has n data, which may be one of A, B, +, -, and the middle is used Separated by spaces.
Enter to ensure that both A and B only appear once.

Output

It is required to output an integer that represents the minimum number of steps the tank can move from zone A to zone B.
If there is no plan, output -1

Sample input

5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

Sample output

10

BFS code:

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=110;
bool vis[maxn][maxn]={
    
    false}; //记录是否走过
char site[maxn][maxn]; //地图
int n,ans=1<<27,sx,sy,cnt=0;
int dx[4]={
    
    0,0,-1,1}; //四个方向
int dy[4]={
    
    1,-1,0,0};

struct node{
    
     
	int x,y,step;
	node(int a,int b,int c){
    
    x=a;y=b;step=c;}
};

void bfs()
{
    
    
	queue<node> q;
	q.push(node(sx,sy,0));
	vis[sx][sy]=1;
	
	while(!q.empty())
	{
    
    
		node now = q.front();
		q.pop();
		
		int x=now.x;
		int y=now.y;
		int step=now.step;
		
		if(site[x][y]=='B')  //到达终点
		{
    
    
			cout<<step;
			break;
		}
		
		for(int i=0;i<4;i++)
		{
    
    
			int newx=x+dx[i];
			int newy=y+dy[i];
			if(!vis[newx][newy] && newx>=0 && newx<n && newy>=0 && newy<n && site[newx][newy]!=site[x][y])
			//没访问过 && 在边界内 && 和上一步的字符不同
			{
    
    
				vis[newx][newy]=1;
				q.push(node(newx,newy,step+1));
			}
		}
	}
	
}

int main() 
{
    
    
	cin>>n;
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			cin>>site[i][j];
			if(site[i][j]=='A') sx=i,sy=j; //确定起点
		}
	}
	
	bfs();
	return 0;
}


DFS code:

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=110;
char site[maxn][maxn]; //地图
bool vis[maxn][maxn]={
    
    false}; //记录是否走过
int n,ans=1<<27,sx,sy;
int dx[4]={
    
    0,0,-1,1}; //四个方向
int dy[4]={
    
    1,-1,0,0};

void dfs(int x,int y,int step)  
{
    
    
	if(step>=ans) return; //剪枝,当前步数已经无法再优化了
	
	if(site[x][y]=='B') //到终点,更新ans
	{
    
    
		ans=min(ans,step);
		return;
	}
	
	for(int i=0;i<4;i++)
	{
    
    
		int newx=x+dx[i];
		int newy=y+dy[i];
		if(!vis[newx][newy] && newx>=0 && newx<n && newy>=0 && newy<n && site[newx][newy]!=site[x][y])
		//没访问过 && 在边界内 && 和上一步的字符不一样
		{
    
    
			vis[newx][newy]=1;
			dfs(newx,newy,step+1);
			vis[newx][newy]=0;
		}
	}
}

int main() 
{
    
    
	cin>>n;
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			cin>>site[i][j];
			if(site[i][j]=='A') sx=i,sy=j; //找到起点
		}
	}
	
	dfs(sx,sy,0);
	if(ans!=1<<27) cout<<ans;
	else cout<<-1;
	return 0;
}

This time-consuming gap is obvious (the first line is BFS, the second line is DFS)

Because of the characteristics of BFS, it must be the shortest when it reaches the end for the first time, and DFS needs to find all the paths, and then update, find ans

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45260385/article/details/109457798