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

    原题链接

    这道题做了改了一整天,到现在终于算是做对了。。。

    不解的是同时对两个队列进行操作的时候位于后面的一个

    队列会出现只能走一两步的情况,在这卡了好久也没想明白

    是怎么回事,于是换了个思路,将两个人走的路线分别求出来,

    最后遍历一下找出符合条件的最小值就行了。

    需要两个布尔数组用来判断两个人在地图中哪些点走过了,

    还有两个计数数组(一个也行, 两个更直观),也就是两个人

    都遍历一下,然后求最小值(到达KFC的最小值)。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 #include <algorithm>
     5 using namespace std;
     6 const int N = 210;
     7 int n, M;
     8 bool vism[N][N], visy[N][N];
     9 char maze[N][N];
    10 int cntm[N][N], cnty[N][N];
    11 struct nodem{
    12     int x, y, d;
    13     nodem(int xx, int yy, int dd){
    14         x = xx, y = yy, d = dd;
    15     }
    16 };
    17 struct nodey{
    18     int x, y, d;
    19     nodey(int xx, int yy, int dd){
    20         x = xx, y = yy, d = dd;
    21     }
    22 };
    23 int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    24 queue<nodem> m;
    25 queue<nodey> y;
    26 int bfs(int mx, int my, int yx, int yy){
    27     m.push(nodem(mx, my, 0));
    28     y.push(nodey(yx, yy, 0));
    29     vism[mx][my] = true;
    30     visy[yx][yy] = true;
    31     while(!m.empty()) {
    32         nodem nowm = m.front();
    33         m.pop();
    34         vism[nowm.x][nowm.y] = true;
    35         for(int i = 0; i < 4; i ++){
    36             int tx = nowm.x + dir[i][0], ty = nowm.y + dir[i][1];
    37             if(tx >= 0 && tx < n && ty >= 0 && ty < M && !vism[tx][ty] && maze[tx][ty] != '#'){
    38                 m.push(nodem(tx, ty, nowm.d + 11));
    39                 cntm[tx][ty] = nowm.d + 11;
    40                 vism[tx][ty] = true;
    41             }
    42         }
    43     }
    44     while(!y.empty()){
    45         nodey nowy = y.front();
    46         y.pop();
    47         visy[nowy.x][nowy.y] = true;
    48         for(int i = 0; i < 4; i ++){
    49             int tx = nowy.x + dir[i][0], ty = nowy.y + dir[i][1];
    50             if(tx >= 0 && tx < n && ty >= 0 && ty < M && !visy[tx][ty] && maze[tx][ty] != '#'){
    51                 y.push(nodey(tx, ty, nowy.d + 11));
    52                 cnty[tx][ty] = nowy.d + 11;
    53                 visy[tx][ty] = true;
    54             }
    55         }
    56     }
    57     int minn = 0x3f3f3f;
    58     for(int i = 0; i < n; i ++){
    59         for(int j = 0; j < M; j ++){
    60             if(vism[i][j] && visy[i][j] && maze[i][j] == '@'){
    61                 minn = min(minn, cntm[i][j] + cnty[i][j]);
    62             }
    63                 
    64         }
    65     }
    66     return minn;
    67 }
    68 int main(){          
    69     while(cin >> n >> M){
    70         memset(vism, 0, sizeof vism);
    71         memset(visy, 0, sizeof visy);
    72         for(int i = 0; i < n; i ++)
    73             cin >> maze[i];
    74         int yx, yy, mx, my;
    75         for(int i = 0; i < n; i ++)
    76             for(int j = 0; j < M; j ++)
    77                 if(maze[i][j] == 'Y')
    78                     yx = i, yy = j;
    79                 else if(maze[i][j] == 'M')
    80                     mx = i, my = j;
    81         int ans = bfs(mx, my, yx, yy);
    82         cout << ans << endl;
    83     }
    84     
    85     return 0;
    86 } 
  • 相关阅读:
    gcd
    Kuglarz
    三分题解
    杜教筛
    第一组dp解题报告
    dp总结1
    cf-BitwiseXor
    6.6总结
    图论总结
    CF1309总结
  • 原文地址:https://www.cnblogs.com/pureayu/p/13715083.html
Copyright © 2011-2022 走看看