是橙题呀,有大佬看看吗
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,a[10][10],sx,ey,ex,sy,fx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool b[10][10];
struct node{
int x,y,z,step;
};
void bfs(){
queue<node>q;
node t;
t.x=sx;
t.y=sy;
t.z=6;
t.step=0;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
node tt;
for(int i=0;i<4;i++){
tt.x=t.x+fx[i][0];
tt.y=t.y+fx[i][1];
tt.z=t.z-1;
tt.step=t.step+1;
printf("%d %d %d %d\n",tt.x,tt.y,tt.z,tt.step);
if(tt.x>=1&&tt.y>=1&&tt.x<=n&&tt.y<=m&&a[tt.x][tt.y]!=0&&(tt.z>0||a[tt.x][tt.y]==4)&&b[tt.x][tt.y]==false){
if(tt.x==ex&&tt.y==ey){
printf("%d",tt.step);
return;
}
else{
if(a[tt.x][tt.y]==4){
tt.z=6;
}
b[tt.x][tt.y]=true;
q.push(tt);
}
}
}
}
printf("-1");
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==2){
sx=i;
sy=j;
}
if(a[i][j]==3){
ex=i;
ey=j;
}
}
}
printf("\n");
bfs();
return 0;
}