hdu 2645

find the nearest station

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 240    Accepted Submission(s): 129

Problem 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 <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node
{
    int x,y,step;
};
int n,m;
char map[190][190];  
int ans[190][190],vis[190][190]; //搜索完返回的最小值用ans数组记录输出即可,vis标记数组
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //方向数组

int bfs(int xx,int yy)
{
    queue <Node> q;
    memset(vis,0,sizeof(vis));
    Node a;
    a.x=xx,a.y=yy,a.step=0;
    q.push(a);
    while(!q.empty())
    {
        Node b;
        b=q.front();
        q.pop();
        vis[b.x][b.y]=1;
        if(map[b.x][b.y]=='1')
            return b.step;
        for(int i=0;i<4;i++)
        {
            Node c;
            c=b;
            c.x+=dir[i][0];
            c.y+=dir[i][1];
            c.step++;
            if(vis[c.x][c.y]==0&&c.x>=0&&c.x<n&&c.y>=0&&c.y<m)  //判断边界
            { 
                q.push(c);
                vis[c.x][c.y]=1;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int k=0;k<m;k++)
            {
                ans[i][k]=bfs(i,k);
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int k=0;k<m-1;k++)
            {
                printf("%d ",ans[i][k]);
            }
            printf("%d\n",ans[i][m-1]);
        }

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值