#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=5e3+10;
struct node{
int x,y;
char qwq;
}pre[N][N];
queue<node>q;
int fx[]={0,1,0,0,-1};
int fy[]={0,0,-1,1,0};
int n,m;
bool v[N][N],f;
char a[N][N];
void print(int x,int y){
if(x==1&&y==1)return;
print(pre[x][y].x,pre[x][y].y);
if(pre[x][y].qwq!=' ')cout<<pre[x][y].qwq;
}
void bfs(int x,int y){
v[x][y]=1;
q.push({x,y,' '});
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==n&&t.y==m){
print(t.x,t.y);
f=1;
return;
}
for(int i=1;i<=4;i++){
int tx=t.x+fx[i];
int ty=t.y+fy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]=='0'&&v[tx][ty]==0){
char awa=0;
if(i==1)awa='D';
else if(i==2)awa='L';
else if(i==3)awa='R';
else awa='U';
q.push({tx,ty,awa});
pre[tx][ty]={t.x,t.y,awa};
v[tx][ty]=1;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
bfs(1,1);
if(f==0)cout<<-1;
return 0;
}城主:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=30+10;
int n,m,sx,sy,sz,ex,ey,ez,v[N][N][N],k;
char a[N][N][N];
int fx[]={0,0,1,0,-1,0,0};
int fy[]={0,1,0,-1,0,0,0};
int fz[]={0,0,0,0,0,1,-1};
struct node{
int x,y,z,step;
};
queue<node>q;
void bfs(int x,int y,int z){
v[x][y][z]=1;
q.push({x,y,z,0});
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==ex&&t.y==ey&&t.z==ez){
printf("Escaped in %d minute(s).",t.step);
exit(0);
}
for(int i=1;i<=6;i++){
int tx=t.x+fx[i];
int ty=t.y+fy[i];
int tz=t.z+fz[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&tz>=1&&tz<=k&&a[tx][ty][tz]!='#'&&v[tx][ty][tz]==0){
q.push({tx,ty,tz,t.step+1});
v[tx][ty][tz]=1;
}
}
}
return;
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int l=1;l<=k;l++){
cin>>a[i][j][l];
if(a[i][j][l]=='S'){
sx=i;
sy=j;
sz=l;
}
if(a[i][j][l]=='E'){
ex=i;
ey=j;
ez=l;
}
}
}
}
bfs(sx,sy,sz);
cout<<"Trapped!";
return 0;
}#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,ex,ey,dx[8]={-1,1,0,0,-1,1,-1,1},dy[8]={0,0,-1,1,-1,1,1,-1};
char a[16384][8005];
queue<int>x;
queue<int>y;
queue<int>s;
bool check(int q,int p){return q>=1&&q<=n&&p>=1&&p<=m;}
bool look(int mx,int my){
if(mx==ex&&my==ey)
return true;
for(int i=0;i<8;i++){
int ux=mx+dx[i],uy=my+dy[i];
while(check(ux,uy)&&a[ux][uy]=='O'){
if(ux==ex&&uy==ey)
return true;
ux+=dx[i],uy+=dy[i];
}
}
return false;
}
void bfs(){
bool f[n+1][m+1];
memset(f,0,sizeof(f));
while(!x.empty()) x.pop();
while(!y.empty()) y.pop();
while(!s.empty()) s.pop();
x.push(sx);
y.push(sy);
s.push(0);
while(!x.empty()){
int ox=x.front(),oy=y.front(),os=s.front();
if(look(ox,oy)){
cout<<os<<"\n";
return;
}
for(int i=0;i<4;i++){
int px=ox+dx[i],py=oy+dy[i];
if(check(px,py)&&f[px][py]==0&&a[px][py]!='X')
x.push(px),y.push(py),s.push(os+1),f[px][py]=1;
}
x.pop(),y.pop(),s.pop();
}cout<<"Poor Harry\n";
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
cin>>ex>>ey>>sx>>sy;
while(!(sx==0&&sy==0&&ex==0&&ey==0)){
bfs();
cin>>ex>>ey>>sx>>sy;
}
return 0;
}