提交时间:2024-03-09 11:14:59

运行 ID: 136632

#include<iostream> #include<cstdio> using namespace std; struct node { int x,y; }; const int NR=110,X[]={-1,-1,0,1,1,1,0,-1},Y[]={0,1,1,1,0,-1,-1,-1}; int n,m,step=0; bool vis[NR][NR]; char c[NR][NR]; node d[NR*NR]; void dfs(int x,int y) { vis[x][y]=true; d[++step]={x,y}; int i; if(x==n && y==m) { for(i=1;i<=step;i++) cout<<d[i].y<<" "<<d[i].x<<endl; exit(0); } for(int i=0;i<=7;i++) { int tx=x+X[i],ty=y+Y[i]; if(1<=tx && tx<=n && 1<=ty && ty<=m && vis[tx][ty]==false && c[tx][ty]=='0') dfs(tx,ty); } vis[x][y]=false; step--; return; } int main() { cin>>n>>m; int i,j; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>c[i][j]; dfs(1,1); cout<<-1; return 0; }