• HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离


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


    Nightmare Ⅱ

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

    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
    二日月
     
    Source



    题意:

    1.Z一秒可以扩散到两步以内的范围,每一步都可以走四个方向, 可以穿墙。

    2.M一秒可以走0123步, 每一步都可以是四个方向, 不可穿墙。

    3.G一秒可以走01步, 每一步都可以是四个方向, 不可穿墙。



    题解:

    1.M和G是否可以相遇,典型的双向BFS问题。

    2.由于ghost可以穿墙, 所以对于整个图来说,ghost畅通无阻, 因此可以利用曼哈顿距离来判断M和G是否会被抓到,避免了ghost加入队列的麻烦。




    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <vector>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 #define ms(a,b) memset((a),(b),sizeof((a)))
     13 using namespace std;
     14 typedef long long LL;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int MOD = 1e9+7;
     18 const int MAXN = 800+10;
     19 
     20 char maze[MAXN][MAXN];
     21 int n, m, dir[4][2] = {1,0,0,1,-1,0,0,-1};
     22 int ghost[2][2];
     23 
     24 struct node
     25 {
     26     int x, y;
     27 };
     28 node M, G;
     29 
     30 bool caught(node now, int time) //判断会不会被ghost抓住,使用曼哈顿距离判断
     31 {
     32     if(abs(now.x-ghost[0][0])+abs(now.y-ghost[0][1])<=2*time) return true;
     33     if(abs(now.x-ghost[1][0])+abs(now.y-ghost[1][1])<=2*time) return true;
     34     return false;
     35 }
     36 
     37 queue<node>q[2];
     38 bool vis[2][MAXN][MAXN];
     39 bool bfs(int id, int time)
     40 {
     41     int Size = q[id].size();    //用于刹车,防止把新入队的点也进行更新
     42     node now, tmp;
     43     while(Size--)
     44     {
     45         now = q[id].front();
     46         q[id].pop();
     47 
     48         if(caught(now, time)) continue;   //欲出发的点会被ghost抓住, 下一个
     49         for(int i = 0; i<4; i++)
     50         {
     51             tmp.x = now.x + dir[i][0];
     52             tmp.y = now.y + dir[i][1];
     53             if(tmp.x>=1 && tmp.x<=n && tmp.y>=1 && tmp.y<=m   //没出界
     54                && maze[tmp.x][tmp.y]!='X' && maze[tmp.x][tmp.y]!='Z'   //可行通道
     55                && !vis[id][tmp.x][tmp.y] && !caught(tmp, time))   //没有被访问过,其此时此地不会被抓
     56             {
     57                 vis[id][tmp.x][tmp.y] = true;
     58                  //对方已经访问过,则meet。不需判断两者到达的时间是否相等,因为可以走0步,所以就可以一直停留
     59                 if(vis[!id][tmp.x][tmp.y]) return true;
     60                 q[id].push(tmp);
     61 
     62             }
     63         }
     64     }
     65     return false;
     66 }
     67 
     68 int solve()
     69 {
     70     ms(vis, 0);
     71     while(!q[0].empty()) q[0].pop();
     72     while(!q[1].empty()) q[1].pop();
     73     vis[0][M.x][M.y] = 1;
     74     vis[1][G.x][G.y] = 1;
     75     q[0].push(M);
     76     q[1].push(G);
     77 
     78     int time = 0;   //计时器
     79     while(!q[0].empty() || !q[1].empty())
     80     {
     81         time++;
     82         for(int i = 1; i<=3; i++)   //M在一秒内,可以走0、1、2、3步
     83             if(bfs(0, time)) return time;
     84         if(bfs(1, time)) return time;  //G只能走0、1步
     85     }
     86     return -1;
     87 }
     88 
     89 int main()
     90 {
     91     int T;
     92     scanf("%d", &T);
     93     while(T--)
     94     {
     95         scanf("%d%d", &n, &m);
     96         for(int i = 1; i<=n; i++)
     97             scanf("%s", maze[i]+1);
     98 
     99         int cnt = 0;
    100         for(int i = 1; i<=n; i++)
    101         for(int j = 1; j<=m; j++)
    102         {
    103            if(maze[i][j]=='Z') ghost[cnt][0] = i, ghost[cnt++][1] = j;
    104            if(maze[i][j]=='M') M.x = i, M.y = j;
    105            if(maze[i][j]=='G') G.x = i, G.y = j;
    106         }
    107         cout<< solve() <<endl;
    108     }
    109 }
    View Code
  • 相关阅读:
    @EnableTransactionManagement的使用
    Spring事务管理之几种方式实现事务
    instr和like的使用区别
    linux查看服务安装目录redis
    struts2的结果类型
    ajax 的 get 方式
    数据库隔离级别
    数据库隔离级别
    input from 表单提交 使用 属性 disabled=&quot;disabled&quot; 后台接收不到name=&quot;username&quot;的值
    Path Sum
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538575.html
Copyright © 2020-2023  润新知