http://acm.hdu.edu.cn/showproblem.php?pid=2645

找出每个点到距离最近的车站的距离。

直接bfs就好。

 #include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
int maze[][],dis[][],vis[][];
int dir[][]={-,,,,,,,-};
struct point
{
int x,y,step;
}; int bfs(int a,int b)
{
// printf("%d %d\n",a,b);
memset(vis,,sizeof(vis));
queue<point>que;
point s;
s.x=a;s.y=b;s.step=;
que.push(s);
vis[s.x][s.y]=;
while(!que.empty())
{
point e=que.front();que.pop();
// printf("%d %d %d\n",e.x,e.y,e.step);
if(maze[e.x][e.y]) return e.step;
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(!vis[s.x][s.y]&&s.x>=&&s.x<n&&s.y>=&&s.y<m)
{
vis[s.x][s.y]=;
s.step=e.step+;
que.push(s);
}
}
}
} int main()
{
// freopen("a.txt","r",stdin);
char s[];
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
{
scanf("%s",s);
for(int j=;j<m;j++)
{
maze[i][j]=s[j]-'';
//printf("%d\n",maze[i][j]);
}
}
memset(dis,,sizeof(dis));
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(!maze[i][j])
dis[i][j]=bfs(i,j);
for(int i=;i<n;i++)
{
for(int j=;j<m-;j++)
printf("%d ",dis[i][j]);
printf("%d\n",dis[i][m-]);
}
}
return ;
}

hdu - 2645 find the nearest station (bfs水)的更多相关文章

  1. hdu 2645 find the nearest station

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dande ...

  2. 【HDOJ】2645 find the nearest station

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

  3. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  5. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  6. HDU 6386 Age of Moyu 【BFS + 优先队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...

  7. nyoj--523--亡命逃窜(BFS水题)

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃被魔 ...

  8. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu1240 bfs 水题

    原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...

随机推荐

  1. ADO.net增删改的使用

    添加数据 -------------------------------------------------- //让用户输入要添加的内容 Console.WriteLine("请输入要添加 ...

  2. jq星星评分

    html代码 <div class="make_mark"> <h5>请为这次服务打分</h5> <div class="mar ...

  3. vue 数组和对象的双向绑定不响应问题

    对象和数组的数据类型是对象,对象是对象这个是毫无疑问的.数组可以把索引当成键名,把索引对应的元素当成该键名的键值. vue对象有些操作不能双向绑定的原因是vue未改变原对象,以及未给新增属性增加set ...

  4. Java数组的交集、并集

    // 求两个数组的交集 public static int[] SameOfTwoArrays(int[] arr1, int[] arr2) { // 新建一个空数组,用于存储交集,空数组长度应该为 ...

  5. avd manager或sdk manager无法打开

    最近开始搞安卓,使用AS启动项目时老是报各种错误,而网上这方面的资料很多都解决不了.只能边实验边做. 定位到avd manager或sdk manager无法打开,网上找了很多资料,都不能解决,知道看 ...

  6. 第二章 TCP/IP 基础知识

    第二章 TCP/IP 基础知识   TCP/IP  transmission control protocol and ip internet protocol 是互联网众多通信协议中最为著名的.   ...

  7. R in action读书笔记(12)第九章 方差分析

    第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...

  8. CAS4.0 server 环境的搭建

    1.上cas的官网下载cas server 官网地址:https://github.com/Jasig/cas/releases,下载好后 解压下载的 cas-server-4.0.0-release ...

  9. php接收json格式数据(text/xml)

    在API服务中,目前流行采用json形式来交互. 给前端调用的接口输出Json数据,这个比较简单,只需要组织好数据,用json_encode($array) 转化一下,前端就得到json格式的数据. ...

  10. Python2和Python3除法

    Python2和Python3除法   Python2除法:/,//,% "/":整数相除,向下取整:浮点数相除,结果包含小数(类似1/2,想保留小数应该写成1.0/2或者1*1. ...