SDOI2005 位图 发表于 2018-09-03 | 分类于 各省省选 | 字数统计: 349 字 题目链接 题解从每一个白块周围扩展即可。1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cctype>#define INF 2000000000using 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;}
v1.5.2