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 ...
随机推荐
- OpenStack和Redis
前言: 最近开始捣鼓OpenStack了,在用RDO部署OpenStack的时候,发现装了Redis, 遂决定看看OpenStack哪些地方(可以)用到Redis. Redis作为OpenStack ...
- WWF3XOML方式创建和启动工作流 <第十篇>
一.XOML使用工作流的好处 通过Xoml方式使用工作流的好处在于,它能够不重新启动程序的情况下,仅仅通过配置xoml就能够实现改变工作流,非常灵活. 创建一个WinForm程序如下: 代码如下: n ...
- ios开发之OC基础-类和对象
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- HTTP MIME类型即HttpResponse.ContentType属性值列表
MIME-Typ Dateiendung(en) Bedeutung application/acad *.dwg AutoCAD-Dateien (nach NCSA) application/ap ...
- 地球坐标系与投影方式的理解(关于北京54,西安80,WGS84;高斯,兰勃特,墨卡托投影)
一.地球模型 地球是一个近似椭球体,测绘时用椭球模型逼近,这个模型叫做参考椭球,如下图: 赤道是一个半径为a的近似圆,任一圈经线是一个半径为b的近似圆.a称为椭球的长轴半径,b称为椭球的短轴半径. a ...
- java初始化深度剖析
当用一个类来创建对象的时候,JVM会帮我们执行下面的6个步骤. 1.在磁盘上找到对应内的class文件: 2.加载class文件到内存,初始化所有静态域(成员变量和方法): 3.如果使用new cla ...
- 银行ATM机工作流程模拟编程(代码)
#include<stdio.h>#include<stdlib.h>#include <conio.h>#include <string.h> voi ...
- C 小复习
C语言 signed 与 unsigned: C语言中,当表达式中存在有符号类型和无符号类型时所有的操作数都自动转换为无符号类型 signed ; unsigned ; cout << a ...
- Xcode Alcatraz插件管理介绍和使用
Xcode Alcatraz插件管理介绍和使用http://www.jianshu.com/p/7a2484123bf6 1.简介 Alcatraz是一个能帮你管理Xcode插件丶模版及颜色配置的工具 ...
- Jquery入门之---------基本事件------------
Javascript有一个非常重要的功能,就是事件驱动. 当页面完成加载后,用户通过鼠标或键盘触发页面中绑定事件的元素即可触发.Jquery为开发者更有效率的编写事件行为,封装了大量有益的事件方法供我 ...