zoukankan      html  css  js  c++  java
  • hdu

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

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

    直接bfs就好。

     1 #include <cstdio>
     2 #include <queue>
     3 #include <cstring>
     4 using namespace std;
     5 int n,m;
     6 int maze[200][200],dis[200][200],vis[200][200];
     7 int dir[4][2]={-1,0,1,0,0,1,0,-1};
     8 struct point
     9 {
    10     int x,y,step;
    11 };
    12 
    13 int bfs(int a,int b)
    14 {
    15    // printf("%d %d
    ",a,b);
    16     memset(vis,0,sizeof(vis));
    17     queue<point>que;
    18     point s;
    19     s.x=a;s.y=b;s.step=0;
    20     que.push(s);
    21     vis[s.x][s.y]=1;
    22     while(!que.empty())
    23     {
    24         point e=que.front();que.pop();
    25        // printf("%d %d %d
    ",e.x,e.y,e.step);
    26         if(maze[e.x][e.y]) return e.step;
    27         for(int i=0;i<4;i++)
    28         {
    29             s.x=e.x+dir[i][0];
    30             s.y=e.y+dir[i][1];
    31             if(!vis[s.x][s.y]&&s.x>=0&&s.x<n&&s.y>=0&&s.y<m)
    32             {
    33                 vis[s.x][s.y]=1;
    34                 s.step=e.step+1;
    35                 que.push(s);
    36             }
    37         }
    38     }
    39 }
    40 
    41 int main()
    42 {
    43    // freopen("a.txt","r",stdin);
    44     char s[200];
    45     while(~scanf("%d%d",&n,&m))
    46     {
    47         for(int i=0;i<n;i++)
    48         {
    49             scanf("%s",s);
    50             for(int j=0;j<m;j++)
    51             {
    52                 maze[i][j]=s[j]-'0';
    53                 //printf("%d
    ",maze[i][j]);
    54             }
    55         }
    56         memset(dis,0,sizeof(dis));
    57         for(int i=0;i<n;i++)
    58             for(int j=0;j<m;j++)
    59             if(!maze[i][j])
    60                dis[i][j]=bfs(i,j);
    61         for(int i=0;i<n;i++)
    62         {
    63             for(int j=0;j<m-1;j++)
    64                 printf("%d ",dis[i][j]);
    65             printf("%d
    ",dis[i][m-1]);
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    蛋疼的时候写三消游戏(十一) 圆形时钟
    C# 中的volatile关键字 (我今天才知道)
    第十四周助教总结
    第十周助教总结
    第十二周助教总结
    C语言I博客作业04
    C语言I博客作业05
    C语言I博客作业02
    第十一周助教总结
    第十三周助教总结
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4547650.html
Copyright © 2011-2022 走看看