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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

    思路:

    裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间;

    然后对于@的点,将两者的最小时间相加即为到达此点的总时间,遍历所有@点,更新结果即可;

    代码:

      1 #include<iostream>
      2 #include<cstdlib>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<queue>
      6 #define MAX 0x7f7f7f7f 
      7 using namespace std;
      8 int n,m;
      9 int startY_x,startY_y;
     10 int startM_x,startM_y;
     11 char map[210][210];
     12 int d[4][2]={1,0,-1,0,0,1,0,-1};
     13 int vis1[210][210];
     14 int vis2[210][210];
     15 class node
     16 {
     17         public:
     18         int x;
     19         int y;
     20         int time;
     21 }cur,next;
     22 queue<node>q;
     23 bool Jude(int x,int y)
     24 {
     25       if(x<0||x>=n||y<0||y>=m||map[x][y]=='#')
     26               return false;
     27       return true;
     28 }
     29 void init()
     30 {
     31         for(int i=0;i<210;i++)
     32                 for(int j=0;j<210;j++)
     33                 {
     34                         vis1[i][j]=0;
     35                         vis2[i][j]=0;
     36                 }
     37 
     38 }
     39 void  bfs(int key)
     40 {
     41         while(!q.empty())
     42         {
     43                cur=q.front();
     44                q.pop();
     45                for(int i=0;i<4;i++)
     46                {
     47                        int x=cur.x+d[i][0];
     48                        int y=cur.y+d[i][1];
     49                        if(!Jude(x,y)) continue;
     50                        if(key==1) 
     51                                {
     52                                       if(vis1[x][y]) continue;
     53                                       next.x=x;
     54                                       next.y=y;
     55                                       next.time=cur.time+1;
     56                                       vis1[x][y]=next.time;
     57                                       q.push(next);
     58                                }
     59                        if(key==2)
     60                                 {
     61                                         if(vis2[x][y]) continue;
     62                                         next.x=x;
     63                                         next.y=y;
     64                                         next.time=cur.time+1;
     65                                         vis2[x][y]=next.time;
     66                                         q.push(next);
     67                                 }
     68                 }
     69         }
     70 }
     71 int  main()
     72 {
     73         while(scanf("%d%d",&n,&m)!=EOF)
     74         {
     75                 init();
     76                 for(int i=0;i<n;i++)
     77                 {
     78                        for(int  j=0;j<m;j++)
     79                        {
     80                                cin>>map[i][j];
     81                                if(map[i][j]=='Y')
     82                                {
     83                                        startY_x=i;
     84                                        startY_y=j;
     85                                }
     86                                if(map[i][j]=='M')
     87                                {
     88                                        startM_x=i;
     89                                        startM_y=j;
     90                                }     
     91                        }
     92                 }
     93 
     94                 cur.x=startY_x;
     95                 cur.y=startY_y;
     96                 cur.time=0;
     97                 vis1[cur.x][cur.y]=1;
     98                 q.push(cur);
     99                 bfs(1);
    100 
    101                 while(!q.empty()) q.pop();
    102                 cur.x=startM_x;
    103                 cur.y=startM_y;
    104                 cur.time=0;
    105                 vis2[cur.x][cur.y]=1;
    106                 q.push(cur);
    107                 bfs(2);
    108 
    109                 long long ans=MAX;
    110                 for(int i=0;i<n;i++)
    111                         for(int j=0;j<m;j++)
    112                         {
    113                                if(map[i][j]=='@' && vis1[i][j] && vis2[i][j])
    114                                {
    115                                        long long  temp=vis1[i][j]+vis2[i][j];
    116                                        ans=min(ans,temp);
    117                                }
    118                         }
    119                  cout<<ans*11<<endl;
    120 }
    121         return 0;
    122 }
    View Code
  • 相关阅读:
    【题解】Codeforces Round #600(Div.2)
    【题解】CF 1073 G. Another LCP
    【题解】CF 1129C. Morse Code
    【题解】CF 1200E. Compress Words
    Cpp Chapter 8: Adventures in Functions Part4
    Cpp Chapter 8: Adventures in Functions Part3
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Exercise
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 4
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 3
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 2
  • 原文地址:https://www.cnblogs.com/xiaozhuyang/p/hdu2612.html
Copyright © 2011-2022 走看看