HDU 3085 Nightmare Ⅱ (双向BFS)

Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 886    Accepted Submission(s): 185

Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them. You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die. Note: the new ghosts also can devide as the original ghost.
 
Input
The input starts with an integer T, means the number of test cases. Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800) The next n lines describe the maze. Each line contains m characters. The characters may be: ‘.’ denotes an empty place, all can walk on. ‘X’ denotes a wall, only people can’t walk on. ‘M’ denotes little erriyue ‘G’ denotes the girl friend. ‘Z’ denotes the ghosts. It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
 
Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.
 
Sample Input
3 5 6 XXXXXX XZ..ZX XXXXXX M.G... ...... 5 6 XXXXXX XZZ..X XXXXXX M..... ..G... 10 10 .......... ..X....... ..M.X...X. X......... .X..X.X.X. .........X ..XX....X. X....G...X ...ZX.X... ...Z..X..X
 
Sample Output
1 1 -1
 
Author
二日月
 
 
 
 
双向BFS第一题,刚开始每次拓展节点WA了,其实应该是每次拓展一层。写的时候忘了优先拓展队列元素的少的那一边,不过没T,以后写的时候再加入吧。
这题最让我郁闷的是,读图的时候每次读入一个符号,即scanf(" %c",&MAP[i][j])这样,居然T了,调了半天,最后改成每次读入一行后过了,甚是不解啊
  1 #include <iostream>
  2 #include <cmath>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <queue>
  6 using    namespace    std;
  7 
  8 const    int    SIZE = 805;
  9 const    int    UPDATE[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
 10 int    N,M;
 11 int    STEP;
 12 int    MAP[SIZE][SIZE];
 13 int    VIS_1[SIZE][SIZE],VIS_2[SIZE][SIZE];
 14 int    Z_X_1,Z_Y_1,Z_X_2,Z_Y_2;
 15 
 16 struct    Node
 17 {
 18     int    x,y;
 19     bool    check(void)
 20     {
 21         if(x >= 1 && x <= N && y >= 1 && y <= M && MAP[x][y] != 'X'
 22            && (abs(Z_X_1 - x) + abs(Z_Y_1 - y) > 2 * STEP)
 23            && (abs(Z_X_2 - x) + abs(Z_Y_2 - y) > 2 * STEP))
 24             return    true;
 25         return    false;
 26     }
 27 };
 28 
 29 int    dbfs(Node boy,Node girl);
 30 int    main(void)
 31 {
 32     int    t,ans,flag;
 33     Node    boy,girl;
 34     char    s[SIZE];
 35 
 36     scanf("%d",&t);
 37     while(t --)
 38     {
 39         flag = 1;
 40         scanf("%d%d",&N,&M);
 41         getchar();
 42         for(int i = 1;i <= N;i ++)
 43         {
 44             scanf("%s",&s[1]);
 45             for(int j = 1;j <= M;j ++)
 46             {
 47                 MAP[i][j] = s[j];
 48                 if(MAP[i][j] == 'M')
 49                 {
 50                     boy.x = i;
 51                     boy.y = j;
 52                 }
 53                 else    if(MAP[i][j] == 'G')
 54                 {
 55                     girl.x = i;
 56                     girl.y = j;
 57                 }
 58                 else    if(MAP[i][j] == 'Z' && flag)
 59                 {
 60                     Z_X_1 = i;
 61                     Z_Y_1 = j;
 62                     flag = 0;
 63                 }
 64                 else    if(MAP[i][j] == 'Z')
 65                 {
 66                     Z_X_2 = i;
 67                     Z_Y_2 = j;
 68                 }
 69             }
 70         }
 71     
 72         ans = dbfs(boy,girl);
 73         printf("%d
",ans);
 74     }
 75 
 76     return    0;
 77 }
 78 
 79 int    dbfs(Node boy,Node girl)
 80 {
 81     memset(VIS_1,0,sizeof(VIS_1));
 82     memset(VIS_2,0,sizeof(VIS_2));
 83     VIS_1[boy.x][boy.y] = 1;
 84     VIS_2[girl.x][girl.y] = 1;
 85     STEP = 1;
 86 
 87     queue<Node>    que_boy,que_girl;
 88     que_boy.push(boy);
 89     que_girl.push(girl);
 90 
 91     while(1)
 92     {
 93         for(int i = 0;i < 3;i ++)
 94         {
 95             int    size = que_boy.size();
 96             while(size --)
 97             {
 98                 Node    old = que_boy.front();
 99                 que_boy.pop();
100                 if(!old.check())
101                     continue;
102 
103                 for(int j = 0;j < 4;j ++)
104                 {
105                     Node    cur = old;
106 
107                     cur.x += UPDATE[j][0];
108                     cur.y += UPDATE[j][1];
109                     if(!cur.check() || VIS_1[cur.x][cur.y])
110                         continue;
111                     if(VIS_2[cur.x][cur.y])
112                         return    STEP;
113 
114                     VIS_1[cur.x][cur.y] = 1;
115                     que_boy.push(cur);
116                 }
117             }
118         }
119         
120         if(que_girl.empty() && que_boy.empty())
121             return    -1;
122         if(que_girl.empty())
123             continue;
124 
125         int    size = que_girl.size();
126         while(size --)
127         {
128             Node    old = que_girl.front();
129             que_girl.pop();
130             if(!old.check())
131                 continue;
132 
133             for(int j = 0;j < 4;j ++)
134             {
135                 Node    cur = old;
136 
137                 cur.x += UPDATE[j][0];
138                 cur.y += UPDATE[j][1];
139                 if(!cur.check() || VIS_2[cur.x][cur.y])
140                     continue;
141                 if(VIS_1[cur.x][cur.y])
142                     return    STEP;
143 
144                 VIS_2[cur.x][cur.y] = 1;
145                 que_girl.push(cur);
146             }
147         }
148         STEP ++;
149     }
150 
151     return    -1;
152 }
原文地址:https://www.cnblogs.com/xz816111/p/4369722.html