1455: [蓝桥杯2019初赛]迷宫 【难 / bfs 】

在这里插入图片描述
http://oj.ecustacm.cn/problem.php?id=1455

#include<cstring> 
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int N=55;

int n,m;
string g[N];
int dist[N][N];
int dx[4]={
    
    1,0,0,-1},dy[4]={
    
    0,-1,1,0};
char dir[4]={
    
    'D','L','R','U'};
void bfs()//先找到路径 
{
    
    
	queue<pair<int,int>> q;
	memset(dist,-1,sizeof dist);
	dist[n-1][m-1]=0;
	q.push({
    
    n-1,m-1});
	while(q.size())
	{
    
    
		auto t=q.front();
		q.pop();
		
		for(int i=0;i<4;i++)
		{
    
    
			int x=t.first+dx[i], y=t.second+dy[i];
			if(x>=0&&x<n&&&y>=0&&y<m&&dist[x][y]==-1&&g[x][y]=='0')
			{
    
    
				dist[x][y]=dist[t.first][t.second]+1;
				q.push({
    
    x,y});
			}
		}
	}
}
int main(void)
{
    
    
	cin>>n>>m;
	for(int i=0;i<n;i++) cin>>g[i];
	bfs();
	int x=0,y=0;
	string res;
	while(x!=n-1||y!=m-1)
	{
    
    
		for(int i=0;i<4;i++)
		{
    
    
			int nx=x+dx[i],ny=y+dy[i];
			if(nx>=0&&nx<n&&ny>=0&&ny<m&&g[nx][ny]=='0')
			{
    
    
				if(dist[x][y]==1+dist[nx][ny] )
				{
    
    
					x=nx,y=ny;
					res+=dir[i];
					break;
				}
			}
		}
	}
	cout<<res<<endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/bettle_king/article/details/115441578