zoukankan      html  css  js  c++  java
  • HDU 2612

    按理说是一道挺简单的BFS的

    我刚开始直接写了一个 双向BFS ,结果一直过不掉

    后来想想如果双方搜索速度不一样还是会出BUG

    只好改一改变成两次 单独的BFS + 最后枚举判断最小值

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 const int d[4][2]={1,0,-1,0,0,1,0,-1};
     7 struct node{
     8     int t,x,y;
     9 }s,e;
    10 int vis1[205][205],vis2[205][205];
    11 char map[205][205];
    12 int n,m;
    13 queue <node> qf,qb;
    14 bool check(node& p)
    15 {
    16     return p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&map[p.x][p.y]!='#';
    17 }
    18 void bfs()
    19 {
    20     int i;
    21     node now,nxt;
    22     memset(vis1,0,sizeof(vis1));
    23     memset(vis2,0,sizeof(vis2));
    24     while(!qf.empty()) qf.pop();
    25     while(!qb.empty()) qb.pop();
    26     qf.push(s); qb.push(e);
    27     while(!qf.empty())
    28     {
    29         now=qf.front(); qf.pop();
    30         for(i=0;i<4;++i)
    31         {
    32             nxt.x=now.x+d[i][0];
    33             nxt.y=now.y+d[i][1];
    34             nxt.t=now.t+1;
    35             if(check(nxt)&&!vis1[nxt.x][nxt.y])
    36             {
    37                 vis1[nxt.x][nxt.y]=nxt.t;
    38                 qf.push(nxt);
    39             }
    40         }
    41     }
    42     while(!qb.empty())
    43     {
    44         now=qb.front(); qb.pop();
    45         for(i=0;i<4;++i)
    46         {
    47             nxt.x=now.x+d[i][0];
    48             nxt.y=now.y+d[i][1];
    49             nxt.t=now.t+1;
    50             if(check(nxt)&&!vis2[nxt.x][nxt.y])
    51             {
    52                 vis2[nxt.x][nxt.y]=nxt.t;
    53                 qb.push(nxt);
    54             }
    55         }
    56     }
    57 }
    58 int main()
    59 {
    60     int i,j;
    61     while(~scanf("%d%d",&n,&m))
    62     {
    63         for(i=0;i<n;i++)
    64         {
    65             scanf("%s",map[i]);
    66             for(j=0;j<m;j++)
    67             {
    68                 if(map[i][j]=='Y') s.x=i,s.y=j,map[i][j]='#';
    69                 if(map[i][j]=='M') e.x=i,e.y=j,map[i][j]='#';
    70             }
    71         }
    72         s.t=e.t=0;
    73         bfs();
    74         int ans=99999999;
    75         for(i=0;i<n;i++)
    76             for(j=0;j<m;j++)
    77                 if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j])
    78                 {
    79                     ans=min(ans,vis1[i][j]+vis2[i][j]);    
    80                 }
    81                 
    82         printf("%d
    ",ans*11);
    83     }
    84 }
    85 /*
    86 2 4
    87 .Y@#
    88 M...
    89 */
    我自倾杯,君且随意
  • 相关阅读:
    python中的设计模式
    面向对象与面向过程
    什么时候选择TCP,什么时候选择UDP?
    TCP三次握手 && TCP四次挥手
    Linux---Ubuntu基本操作
    安装Redis
    USB接口时灵时不灵的可尝试的解决办法
    帐号注册和用户登录
    三种居中方式练习(text-align、height 和 line-height 、vertical-align)
    java读取GPS观测文件(IO流、ArrayList集合、HashMap集合练习)
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5525498.html
Copyright © 2011-2022 走看看