#include <bits/stdc++.h>
using namespace std;
int maps[1001][1001],dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int n,m;
void dfs(int x,int y){
if(maps[x][y] <= 1)
return;
for(int i = 1;i <= 4;i++){
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
maps[tx][ty] = max(maps[tx][ty],maps[x][y] - 1);
dfs(tx,ty);
maps[x][y] = 1;
}
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
cin >> maps[i][j];
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(maps[i][j] > 1)
dfs(i,j);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++)
cout << maps[i][j] << " ";
cout << endl;
}
return 0;
}
输入
7 7
1 0 0 0 0 0 1
0 2 0 0 3 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 2
0 0 2 0 0 0 0
0 3 0 0 2 0 2
1 0 0 2 0 0 1
后出不来,这是为什么