题目连接

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 ;
}

hdu 2645 find the nearest station的更多相关文章

  1. hdu - 2645 find the nearest station (bfs水)

    http://acm.hdu.edu.cn/showproblem.php?pid=2645 找出每个点到距离最近的车站的距离. 直接bfs就好. #include <cstdio> #i ...

  2. 【HDOJ】2645 find the nearest station

    裸BFS. /* 2645 */ #include <iostream> #include <queue> #include <cstdio> #include & ...

  3. Nyoj Fire Station

    描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...

  4. POJ 2607 Fire Station(Floyd打表+枚举更新最优)

    题目链接: http://poj.org/problem?id=2607 Description A city is served by a number of fire stations. Some ...

  5. HDU 3656 二分+dlx判定

    Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. POJ 2607 Fire Station

    Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  7. NUC_HomeWork1 -- POJ2067(最短路)

    C - Fire Station Description A city is served by a number of fire stations. Some residents have comp ...

  8. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  9. HDU 3879 Base Station

    Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

随机推荐

  1. 慕课网-安卓工程师初养成-2-6 Java中的数据类型

    来源:http://www.imooc.com/code/1230 通常情况下,为了方便物品的存储,我们会规定每个盒子可以存放的物品种类,就好比在“放臭袜子的盒子”里我们是不会放“面包”的!同理,变量 ...

  2. php 调用.net的webservice 需要注意的

    首先 SoapClient类这个类用来使用Web services.SoapClient类可以作为给定Web services的客户端.它有两种操作形式:* WSDL 模式* Non-WSDL 模式在 ...

  3. Linux自动化安装cobbler

    1介绍 1.1 PXE PXE技术与RPL技术不同之处为RPL是静态路由,PXE是动态路由.RPL是根据网卡上的ID号加上其他记录组成的一个Frame(帧)向服务器发出请求.而服务器中已有这个ID数据 ...

  4. leetcode007. Reverse Integer

    /* a good way to predict overflow * each time *10 must predict int overflow * not only the last time ...

  5. DEDECMS如何修改数据库密码以及忘记了后台密码怎么办

    忘记后台密码1.登录PHPmyadmin登录phpmyadmin,找到忘记密码网站所在的数据库并打开2.打开dede_admin数据表:找到dede_admin这项如图,pwd下的值就是你的密码,织梦 ...

  6. import的用法

    转自python学习笔记--模块和命名空间 模块(module)是Python中非常重要的一个概念,模块其实就一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的 ...

  7. c++ builder ListView实现可编辑任意列(转)

    // --------------------------------------------------------------------------- // Form的构造函数中填充StrinG ...

  8. C# 常用小技巧

    int? id 表示id是可以为null的整型 跟Nullable<int> id 是一样的 id ?? 1等于 id==null?1:id;

  9. 设备版本,设备号,APP版本,APP名称获取

    //获取设备id号 UIDevice *device = [UIDevice currentDevice];//创建设备对象 NSString *deviceUID = [[NSString allo ...

  10. openssl mac中使用终端生成RSA私钥和公钥文件

    RSA密钥生成命令生成RSA私钥openssl>genrsa -out rsa_private_key.pem 1024生成RSA公钥openssl>rsa -in rsa_private ...