hdu 2645 find the nearest station

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2645 

find the nearest station

Description

Since dandelion has left the hometown so long,she finds it's difficult to find the station in the city.So she needs you ,a clear programmer, to help her.
Now you know the map of the city, which has showed every station in the city.You are asked to find the shortest distance between every grid and the stations.You should notice that the road in dandelion's hometown is vertical or horizontal,so the distance of two girds is defined as |x1-x2|+|y1-y2|.

Input

The input consists of several test cases. Each test case start with a line containing two number, n, m(1 <= n, m ≤ 182), the rows and the columns of city. Then n lines follow, each contain exact m characters, representing the type of block in it. (0 for empty place ,1 for station).The data will contains at least one station.

Output

For every case ,print a matrix with n rows and m columns, the number in the i row and j column stands for the distance from this grid to the shortest station.

Sample Input

3 4
0001
0011
0110

Sample Output

3 2 1 0
2 1 0 0
1 0 0 1

bfs爆搜。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::pair;
15 using std::queue;
16 using std::vector;
17 #define pb(e) push_back(e)
18 #define sz(c) (int)(c).size()
19 #define mp(a, b) make_pair(a, b)
20 #define all(c) (c).begin(), (c).end()
21 #define iter(c) decltype((c).begin())
22 #define cls(arr,val) memset(arr,val,sizeof(arr))
23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
26 const int N = 190;
27 typedef unsigned long long ull;
28 namespace work {
29     struct Node {
30         int x, y, s;
31         Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
32     };
33     bool map[N][N], vis[N][N];
34     const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
35     int n, m, res[N][N];
36     inline int bfs(int x, int y) {
37         cls(vis, false);
38         queue<Node> que;
39         que.push(Node(x, y, 0));
40         vis[x][y] = true;
41         while (!que.empty()) {
42             Node t = que.front(); que.pop();
43             if (map[t.x][t.y]) return t.s;
44             rep(i, 4) {
45                 int nx = dx[i] + t.x, ny = dy[i] + t.y;
46                 if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny]) continue;
47                 que.push(Node(nx, ny, t.s + 1));
48             }
49         }
50         return 0;
51     }
52     inline void solve() {
53         char buf[N];
54         while (~scanf("%d %d", &n, &m)) {
55             rep(i, n) {
56                 scanf("%s", buf);
57                 rep(j, m) map[i][j] = buf[j] - '0' & 1;
58             }
59             rep(i, n) {
60                 rep(j, m) res[i][j] = map[i][j] ? 0 : bfs(i, j);
61             }
62             rep(i, n) {
63                 rep(j, m) printf("%d%c", res[i][j], j < m - 1 ? ' ' : '
');
64             }
65         }
66     }
67 }
68 int main() {
69 #ifdef LOCAL
70     freopen("in.txt", "r", stdin);
71     freopen("out.txt", "w+", stdout);
72 #endif
73     work::solve();
74     return 0;
75 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4610809.html