zoukankan      html  css  js  c++  java
  • 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/ 转载请说明
  • 相关阅读:
    Linux Hung Task分析
    Linux内存都去哪了:(1)分析memblock在启动过程中对内存的影响
    《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll
    Linux内核和用户空间通信之netlink
    Linux soft lockup分析
    一款DMA性能优化记录:异步传输和指定实时信号做async IO
    Linux下时钟框架实践---一款芯片的时钟树配置
    使用Kernel NetEm和tc模拟复杂网络环境
    使用Flame Graph进行系统性能分析
    sigsuspend()阻塞:异步信号SIGIO为什么会被截胡?
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4610809.html
Copyright © 2011-2022 走看看