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. css中display设置为table、table-row、table-cell后的作用及其注意点

    html: <div class="table"> <div class="row"> <div class="cell ...

  2. TabLayout+ViewPager实现标签卡效果

    转载请注明原文地址:http://www.cnblogs.com/yanyojun/p/8082391.html 代码已经上传至Github:https://github.com/YanYoJun/V ...

  3. Python学习 Day 2-数据类型和变量

    数据类型和变量 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080, ...

  4. 把WebForm移植到.Net MVC中

    最近写项目,想把以前项目中的几个功能页面移植过来(想偷懒一下),在网上查了很多的资料,多数资料都是直接在MVC中添加WebForm,和我的需求不同.在此非常感谢网友“Jason”给予的帮助,终于搞定了 ...

  5. js 日期时间大小比较

    <body> 开始时间:<input onfocus="setday(this)" id="startTime" name="sta ...

  6. 原生 js 整理

    常见的事件 window.event     代表着,事件的状态,只有在事件的过程中才有效.

  7. Laravel Homestead的安装和使用(照搬)

    原文:https://blog.csdn.net/woqianduo/article/details/81091154/ 1.简介 1.1.Homestead是什么 Laravel Homestead ...

  8. Java C

    先说一下自己叫什么,免得面试的人张冠李戴. 介绍自己有几个方面:1学什么专业的那方面学的过硬,可以说的具体点. 2以前做过什么.(这家公司要你肯定是和你的经历有关.) 3现在来这家公司的目的是什么(当 ...

  9. controller,sevices层,java初步了解

    一.controller层 二.service层 1.接口 2.接口的实现 转换 ClearingAccountArgument对象

  10. C# WebService 的缓存机制

    C# WebService 的缓存机制   [转]WebService的缓存机制 2008年02月19日 星期二 11:22 WebService的缓存分为两种,一种是简单的输出缓存,一种是强大的数据 ...