-
Notifications
You must be signed in to change notification settings - Fork 0
/
1256dfs.cpp
72 lines (72 loc) · 1.05 KB
/
1256dfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int inf = 0x3f3f3f;
int n,m;
bool vis[182][182];
int p[182][182];
int move[4][2]={
1,0,
0,1,
0,-1,
-1,0,
};
int search(int,int);
int mf(int x,int y)
{
int minans=inf,ans=0;
for(int i=0;i<4;i++){
ans=search(x+move[i][0],y+move[i][1]);
if(ans==-1) continue;
minans=min(min(minans,ans),p[x][y]);
}
return p[x][y]=minans;
}
int search(int x,int y)
{
if(vis[x][y])
return -1;
vis[x][y]=1;
if(x==0||y==0||x==n+1||y==m+1)
return -1;
if(p[x][y]==-1)
return 0;
if(p[x][y]<=inf/2)
return p[x][y]+1;
int minans=mf(x,y);
vis[x][y]=0;
return minans+1;
}
int main()
{
cin>>n>>m;
char t;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
t=getchar();
if(t==' '||t=='\n')
{
j--;
continue;
}
if(t=='1') p[i][j]=-1;
else p[i][j]=inf;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++){
memset(vis,0,sizeof(vis));
search(i,j);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++){
cout<<p[i][j]+1<<" ";
}
cout<<endl;
}
return 0;
}