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爆搜。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
namespace work {
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
};
bool map[N][N], vis[N][N];
const int dx[] = { , , -, }, dy[] = { -, , , };
int n, m, res[N][N];
inline int bfs(int x, int y) {
cls(vis, false);
queue<Node> que;
que.push(Node(x, y, ));
vis[x][y] = true;
while (!que.empty()) {
Node t = que.front(); que.pop();
if (map[t.x][t.y]) return t.s;
rep(i, ) {
int nx = dx[i] + t.x, ny = dy[i] + t.y;
if (nx < || nx >= n || ny < || ny >= m || vis[nx][ny]) continue;
que.push(Node(nx, ny, t.s + ));
}
}
return ;
}
inline void solve() {
char buf[N];
while (~scanf("%d %d", &n, &m)) {
rep(i, n) {
scanf("%s", buf);
rep(j, m) map[i][j] = buf[j] - '' & ;
}
rep(i, n) {
rep(j, m) res[i][j] = map[i][j] ? : bfs(i, j);
}
rep(i, n) {
rep(j, m) printf("%d%c", res[i][j], j < m - ? ' ' : '\n');
}
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
work::solve();
return ;
}
上一篇:Jmeter(十八)Logic Controllers 之 Random Controller and Random order Controller


下一篇:还有这种好事!netty自带http2的编码解码器framecodec