提交时间:2024-03-09 10:23:35

运行 ID: 136545

#include<bits/stdc++.h> using namespace std; const int N=110; bool f[N][N]; vector<int> res[2]; int n,m; int a[N][N]; int dir[5][2]={{0,1},{1,0},{-1,0},{0,-1}}; bool dfs(int x,int y){ if(x==m&&y==n){ res[0].push_back(x); res[1].push_back(y); return true; } f[x][y]=true; for(int i=0;i<4;i++){ int tx=x+dir[i][0]; int ty=y+dir[i][1]; if(x>=1&&x<=n&&y>=1&&y<=m&&!f[tx][ty]&&!a[tx][ty]){ if(dfs(tx,ty)){ res[0].push_back(x); res[1].push_back(y); return true; } } } return false; } int main(){ scanf("%d%d",&m,&n); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); } } if(!dfs(1,1)){ printf("-1\n"); } else{ for(int i=res[0].size()-1;i>=0;i--){ printf("%d %d\n",res[0][i],res[1][i]); } } return 0; }