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. WinForm 对话框,流

    private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...

  2. css中display设置为table、table-row、table-cell后的作用及其注意点

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

  3. 001原始编译全志r6平台tinav3.0.2系统

    001原始编译全志r6平台tinav3.0.2系统 2018/6/8 11:32 版本:V1.0 开发板:R6 SDK:tina v3.0.2 1.01原始编译全志r16平台tinav3.0系统: r ...

  4. ES6语法糖集锦

    sublime3安装Es6插件 javascriptNext,然后安装即可 Java​Script​Next - ES6 Syntax()高亮插件 -------------------------- ...

  5. WebAPI中Area的使用

    很简单,创建area后,添加一下代码到AreaRegistration中即可 context.Routes.MapHttpRoute( name: "api_default", r ...

  6. (转)淘淘商城系列——VMware添加已配置好的虚拟机

    http://blog.csdn.net/yerenyuan_pku/article/details/72802323 我们有时候会碰到虚拟机环境搭建特别麻烦,很容易出错的问题,而这时我们又刚好有别人 ...

  7. (转)淘淘商城系列——dubbo监控中心

    http://blog.csdn.net/yerenyuan_pku/article/details/72777623 之前我们就已学过了dubbo,想必大家对dubbo的架构有所了解,dubbo的架 ...

  8. window_c++_socket编程_winsock2.h

    1.初始化动态链接库 WSAStartup: The WSAStartup function initiates use of the Winsock DLL by a process. WSASta ...

  9. webpack遇见的坑:Please install 'webpack-cli' in addition to webpack itself to use the CLI.

    webpack-cli没被找到: 在webpack4.0之后,需要全局安装webpack-cli, 在全局安装webpack之后,cnpm i webpack-cli -g 在局部使用webpack时 ...

  10. Linux C下变量和常量的存储的本质

    源代码 #cat main.c #include <stdio.h> int i = 100; int main(void) { func(); return 0; } #cat func ...