#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0,d[]={-1,0,1,0},e[]={0,1,0,-1},f[11][11];
bool vis[11][11],a[11][11];
void dfs(int x,int y){
f[x][y]=++ans;
vis[x][y]=1;
for(int i=0;i<4;i++){
x+=d[i];
y+=e[i];
if(vis[x][y]==0&&!a[x][y]&&x>=0&&y>=0&&x<n&&y<m){
dfs(x,y);
x-=d[i];
y-=e[i];
}
else x-=d[i],y-=e[i];
}
}int main(){
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}dfs(0,0);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)cout<<f[i][j]<<" ";
cout<<"\n";
}return 0;
}
1 个赞
特判起点能不能走
1 个赞