zoukankan      html  css  js  c++  java
  • Find a way (BFS)

    Find a way

     HDU - 2612 

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<queue>
      6 using namespace std;
      7 
      8 char map[201][201];
      9 int book1[205][205],book2[205][205];
     10 int step1[205][205],step2[205][205];
     11 int dir[4][2]={ {1,0},{0,1},{-1,0},{0,-1} };
     12 int n,m,x1,y1,x2,y2;
     13 
     14 struct note
     15 {
     16     int x;
     17     int y;
     18     int s; 
     19 }a,b;
     20 queue<note>q;
     21 
     22 void bfs1(int xx,int yy)
     23 {
     24     memset(book1,0,sizeof(book1));
     25     while(!q.empty()) //清空 
     26         q.pop();
     27     a.x= xx;
     28     a.y= yy;
     29     a.s= 0 ;
     30     book1[a.x][a.y]=1;
     31     step1[a.x][a.y]=0;
     32     q.push(a);
     33     while(!q.empty())
     34     {
     35         a=q.front();
     36         q.pop();//及时出队 
     37         for(int i=0;i<4;i++)
     38         {
     39             b.x=a.x+dir[i][0];
     40             b.y=a.y+dir[i][1];
     41             if(map[b.x][b.y]=='#'||
     42                 book1[b.x][b.y]==1||
     43                 b.x<1||b.x>n||b.y<1||b.y>m)
     44                 continue;
     45             book1[b.x][b.y]=1; //标记:走过了 
     46             b.s=a.s+11;
     47             step1[b.x][b.y]=b.s;  //记录到每个位置的步数
     48             q.push(b); //入队 
     49         }
     50     }
     51     return;
     52 }
     53 
     54 void bfs2(int xx,int yy)
     55 {
     56     memset(book2,0,sizeof(book2));
     57     while(!q.empty())
     58         q.pop();
     59     a.x= xx;
     60     a.y= yy;
     61     a.s= 0 ;
     62     book2[a.x][a.y]=1;
     63     step2[a.x][a.y]=0;
     64     q.push(a); 
     65     while(!q.empty())
     66     {
     67         a=q.front();
     68         q.pop(); 
     69         for(int i=0;i<4;i++)
     70         {
     71             b.x=a.x+dir[i][0];
     72             b.y=a.y+dir[i][1];
     73             if(map[b.x][b.y]=='#'||
     74                 book2[b.x][b.y]==1||
     75                 b.x<1||b.x>n||b.y<1||b.y>m)
     76                 continue;
     77             book2[b.x][b.y]=1;
     78             b.s=a.s+11;
     79             step2[b.x][b.y]=b.s;
     80             q.push(b);
     81         }
     82     }
     83     return;
     84 }
     85 
     86 int main()
     87 {
     88     while(cin>>n>>m)
     89     {
     90         for(int i=1;i<=n;i++)
     91         {
     92             for(int j=1;j<=m;j++)
     93             {
     94                 cin>>map[i][j];
     95                 if(map[i][j]=='Y')
     96                 {
     97                     x1=i; y1=j;
     98                 }
     99                 else if(map[i][j]=='M')
    100                 {
    101                     x2=i; y2=j;
    102                 }
    103             }
    104         }
    105         
    106         bfs1(x1,y1);
    107         bfs2(x2,y2);
    108         
    109         int minn=0x3f3f3f3f;
    110         for(int i=1;i<=n;i++)
    111         {
    112             for(int j=1;j<=m;j++)
    113             {
    114                 if(map[i][j]=='@'&&book1[i][j]==1&&book2[i][j]==1) //这里一定要book检查(因为可能有人无法到达 @的地方 
    115                     minn=min( minn, step1[i][j]+step2[i][j]);
    116             }
    117         }
    118         cout<<minn<<endl;
    119     }
    120 }
  • 相关阅读:
    区间更新+单点查询
    第15题 三个数的和为确定值
    第12题 数字转为罗马数字
    第11题 容器装水问题
    第22章 软件安装:源码与Tarball
    第18章 认识系统服务(daemons)
    第10题 正则表达式匹配(动态规划)
    动态规划
    第6题 ZigZag转换
    第17章 程序管理与SELinux初探
  • 原文地址:https://www.cnblogs.com/thunder-110/p/8992552.html
Copyright © 2011-2022 走看看