zoukankan      html  css  js  c++  java
  • HDU3085(KB2-G 双向bfs)

    Nightmare Ⅱ

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


    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
     
    被读入卡了超时,按行读快,一个一个读超时
      1 //2017-03-08
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <queue>
      6 
      7 using namespace std;
      8 
      9 struct node 
     10 {
     11     int x, y;
     12     void setNode(int x, int y)
     13     {
     14         this->x = x;
     15         this->y = y;
     16     }
     17 };
     18 int n, m, zx[2], zy[2], ans, TIME;
     19 char grid[850][850];
     20 bool vis[2][850][850], ok;
     21 int dx[4] = {0, 0, 1, -1};
     22 int dy[4] = {1, -1, 0, 0};
     23 queue<node> q[2];
     24 
     25 bool judge(int x, int y)
     26 {
     27     if(grid[x][y] == 'X')return false;
     28     for(int i = 0; i < 2; i++)
     29         if((abs(x-zx[i])+abs(y-zy[i]))<=2*TIME)
     30             return false;
     31     return true;
     32 }
     33 
     34 bool bfs(int id)
     35 {
     36     int x, y, nx, ny, s;
     37     node tmp;
     38     s = q[id].size();
     39     while(s--)
     40     {
     41         x = q[id].front().x;
     42         y = q[id].front().y;
     43         q[id].pop();
     44         if(!judge(x, y))continue;
     45         for(int i = 0; i < 4; i++)
     46         {
     47             nx = x + dx[i];
     48             ny = y + dy[i];
     49             if(nx>=0&&nx<n&&ny>=0&&ny<m&&judge(nx, ny)&&!vis[id][nx][ny])
     50             {
     51                 if(vis[id^1][nx][ny]){
     52                     ok = true;
     53                     return true;
     54                 }
     55                 vis[id][nx][ny] = 1;
     56                 tmp.setNode(nx, ny);
     57                 q[id].push(tmp);
     58             }
     59         }
     60     }
     61     return false;
     62 }
     63 
     64 int main()
     65 {
     66     int T;
     67     scanf("%d", &T);
     68     while(T--)
     69     {
     70         node tmp;
     71         while(!q[0].empty())q[0].pop();
     72         while(!q[1].empty())q[1].pop();
     73         memset(vis, 0, sizeof(vis));
     74         scanf("%d%d", &n, &m);
     75         getchar();
     76         for(int i = 0; i < n; i++)//被读入卡了超时,按行读快,一个一个读超时
     77               scanf("%s", grid[i]);
     78         int cnt = 0;
     79         for(int i = 0; i < n; i++)
     80         {
     81             for(int j = 0; j < m; j++)
     82             {
     83                 if(grid[i][j] == 'Z'){
     84                     zx[cnt] = i;
     85                     zy[cnt] = j;
     86                     cnt++;
     87                 }
     88                 if(grid[i][j] == 'M'){
     89                     tmp.setNode(i, j);
     90                     vis[0][i][j] = 1;
     91                     q[0].push(tmp);
     92                 }
     93                 if(grid[i][j] == 'G'){
     94                     tmp.setNode(i, j);
     95                     vis[1][i][j] = 1;
     96                     q[1].push(tmp);
     97                 }
     98             }
     99         }
    100         ok = false;
    101         TIME = 0;
    102         while(!q[0].empty() || !q[1].empty())
    103         {
    104             TIME++;
    105             if(bfs(0))break;
    106             if(bfs(0))break;
    107             if(bfs(0))break;
    108             if(bfs(1))break;
    109         }
    110         if(ok)printf("%d
    ", TIME);
    111         else printf("-1
    ");
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    Storyboard中segue使用总结
    Present ViewController Modally
    UILabel设置富文本格式显示
    objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    ios中static的作用
    NSBundle的使用,注意mainBundle和Custom Bundle的区别
    OC的基础语法OC继承和复合语言特性目标动作回调
    动态规划-被3整除的子序列
    A
    Coins POJ
  • 原文地址:https://www.cnblogs.com/Penn000/p/6517742.html
Copyright © 2011-2022 走看看