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. Android开发学习--ViewPager使用入门

    ViewPager已经有了滑动的功能 activity_main.xml <?xml version="1.0" encoding="utf-8"?> ...

  2. Android开发学习——开发调试工具-DDMS应用,ADB进程,Logcat,Eclipse Debug调试

    DDMS --  Dalvik debug monitor service    调试监控服务 对模拟器进行相关配置. ADB进程   Android debug bridge  建立eclipse和 ...

  3. jquery + ajax 实现多条件查询

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JquerySort.aspx. ...

  4. OpenGl之旅-—初识opengl

    昨天学习了如何使用codeblocks来编译运行一个opengl的项目.在创建一个新的opengl项目时他默认已经写了一个示例,今天我们就上面的例子进行下代码的剖析,以此来敲开opengl的神秘大门. ...

  5. Objective-C Memory Management 内存管理 2

    Objective-C Memory Management 内存管理  2  2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you c ...

  6. 1.了解Objective-C语言

    了解Objective-C语言 ** Objective-C 语言是"消息结构"(messaging structure) 类似C++ .Java 是"函数调用" ...

  7. 【C++】模板简述(三):类模板

    上文简述了C++模板中的函数模板的格式.实例.形参.重载.特化及参数推演,本文主要介绍类模板. 一.类模板格式 类模板也是C++中模板的一种,其格式如下: template<class 形参名1 ...

  8. day23-2 __call__、__str__和__del__

    目录 __call__ __str__ __del__ __call__ 对象后面加括号调用时,会自动触发执行 注:构造方法的执行是由创建对象触发的,即:对象=类名();而对于__call__方法的执 ...

  9. zuul 网关

    1.网关的作用 网关可以拦截客户端所有请求,对该请求进行权限控制,负载均衡.日志管理.接口调用监控等操作. 1)网关对所有服务会话进行拦截 2)网关安全控制,统一异常处理,XXS.SQL注入 3)权限 ...

  10. HTML5小時鐘

    <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/T ...