SDOI2005 位图

题目链接

题解

从每一个白块周围扩展即可。

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
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, m, mp[155][155], ans[155][155];
int que[22505][2], r, l;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
void init(){
n = read(), m = read();
memset(ans, -1, sizeof(ans));
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
mp[i][j] = read();
r = l = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j){
if(mp[i][j]) ans[i][j] = 0;
else{
for(int k = 0; k < 4; ++k){
int ex = i + dx[k], ey = j + dy[k];
if(ex >= 0 && ex < n && ey >= 0 && ey < m && mp[ex][ey]){
que[r][0] = i, que[r++][1] = j;
ans[i][j] = 1;
break;
}
}
}
}
}
void solve(){
while(r - l){
int cx = que[l][0], cy = que[l++][1];
for(int i = 0; i < 4; ++i){
int ex = cx + dx[i], ey = cy + dy[i];
if(ex >= 0 && ex < n && ey >= 0 && ey < m && ans[ex][ey] < 0){
ans[ex][ey] = ans[cx][cy] + 1;
que[r][0] = ex, que[r++][1] = ey;
}
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < m - 1; ++j)
printf("%d ", ans[i][j]);
printf("%d\n", ans[i][m - 1]);
}
}
int main(){
init();
solve();
return 0;
}