#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
char res[4]={'D','L','R','U'};
char map[30][50];
int vis[30][50]={0};
int x1,y1,x2,y2;
struct node{
int x;
int y;
int step;
string path;
};
void bfs(){
queue<node> q;
node p;
p.x=x1,p.y=y1,p.step=0,p.path="",vis[0][0]=1;
q.push(p);
while(!q.empty()){
node p;
p=q.front();
if(p.x==x2 && p.y==y2){
if(p.step==0){
}
cout<<p.step<<endl<<p.path<<endl;
}
node v;
for(int i=0;i<4;i++){
v.x = p.x + dir[i][0];
v.y = p.y + dir[i][1];
if(v.x>=0 && v.y>=0 && v.x<30 && v.y<50 && vis[v.x][v.y]==0&&map[v.x][v.y]=='0'){
vis[v.x][v.y] = 1;
v.step = p.step+1;
v.path = p.path+res[i];
q.push(v);
}
}
q.pop();
}
}
int main(){
for(int i=0;i<30;i++){
for(int j=0;j<50;j++){
cin>>map[i][j];
}
}
x1=0,y1=0,x2=29,y2=49;
bfs();
return 0;
}
历届试题—迷宫
最新推荐文章于 2022-09-16 15:46:39 发布