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 ;
}
hdu 2645 find the nearest station的更多相关文章
- hdu - 2645 find the nearest station (bfs水)
http://acm.hdu.edu.cn/showproblem.php?pid=2645 找出每个点到距离最近的车站的距离. 直接bfs就好. #include <cstdio> #i ...
- 【HDOJ】2645 find the nearest station
裸BFS. /* 2645 */ #include <iostream> #include <queue> #include <cstdio> #include & ...
- Nyoj Fire Station
描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...
- POJ 2607 Fire Station(Floyd打表+枚举更新最优)
题目链接: http://poj.org/problem?id=2607 Description A city is served by a number of fire stations. Some ...
- HDU 3656 二分+dlx判定
Fire station Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- POJ 2607 Fire Station
Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- NUC_HomeWork1 -- POJ2067(最短路)
C - Fire Station Description A city is served by a number of fire stations. Some residents have comp ...
- hdu 3879 Base Station 最大权闭合图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...
- HDU 3879 Base Station
Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
随机推荐
- Java中几种常见的排序方式
冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.这个算法的名字 ...
- js动态生成JSON树
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- spring整合freemarker 自定义标签
1.自定义标签实现 TemplateDirectiveModel 接口 2.spring 配置,注意标红的两行 <bean id="freemarkerConfig" cla ...
- openSUSE之SSH登录
环境: 1:操作系统:openSUSE 2:SSH工具:Xshell 3:virtualbox 解决问题:Xshell通过ssh登录openSUSE进行操作. 1:virtualbox安装好openS ...
- Android IOS WebRTC 音视频开发总结(六一)-- 大数据解密国内实时通讯行业开发现状
本文主要介绍国内实时通讯行业现状,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 这几年移动互联网发展势头很猛,与 ...
- 蘑菇街iOS客户端应用源码
蘑菇街iOS客户端应用源码 随着蘑菇街由导购向电商转型,蘑菇街自己的IM也应运而生,IM起初只是用于商家和买家之间沟通的工具.后面我们问自己,既然已经有了用于客服的IM,为什么不自己做一个IM,用于公 ...
- Python开发的3种命令执行方法
在python开发中,我们常常需要执行命令,修改相关信息.那对于初学者来说,python中如何执行命令呢?今天,小编就为大家分享3种python命令执行的方法. 1. 使用os.system(&quo ...
- MallBuilder 多用户商城管理系统 v5.8.1.1
MallBuilder是一款基于PHP+MYSQL的多用户网上商城解决方案.利用MallBuilder可以快速建立一个功能强大的类似京东商城.天猫商城.1号店商城的网上商城,或企业.行业化.本地化和垂 ...
- STL使用sort注意的问题
结构体使用sort算法时,重载operator<(..).如果我们按下面这样写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- (二)、NodeJS 、Express4安装使用方法
第一步:安装Nodejs 第二步:安装express等部件 1.打开命令窗口,安装express.jade npm install -g express npm install -g express- ...