【HDOJ】2645 find the nearest station

时间:2023-03-09 13:22:58
【HDOJ】2645 find the nearest station

裸BFS。

 /* 2645 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 200 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; int n, m;
queue<node_t> Q;
char map[MAXN][MAXN];
int dist[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; void bfs() {
int i, j, k;
int x, y;
node_t nd; while (!Q.empty()) {
nd = Q.front();
Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (x< || x>=n || y< || y>=m || map[x][y]=='')
continue;
map[x][y] = '';
dist[x][y] = dist[nd.x][nd.y] + ;
Q.push(node_t(x, y));
}
}
} int main() {
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == '') {
Q.push(node_t(i, j));
dist[i][j] = ;
}
}
}
bfs();
for (i=; i<n; ++i) {
printf("%d", dist[i][]);
for (j=; j<m; ++j)
printf(" %d", dist[i][j]);
printf("\n");
}
} return ;
}