传送门HDU3085

描述

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.
For example, given “ACGT”,”ATGC”,”CGTT” and “CAGT”, you can make a sequence in the following way. It is the shortest but may be not the only one.

输入

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 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.

样例

  • 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

  • Output
    1
    1
    -1

题解

  • 题意:G和M在迷宫里寻找对方,M每秒可以走3步,G每次走一步,且只能上下左右走。迷宫里有鬼,鬼每次分裂到上下左右两格位置,直到占满所有格子,鬼可以穿墙,求G和M在不遇到鬼的情况下的最短相遇时间。
  • 双向bfs,由于M每次走3步,所以M每次要处理3次。
  • 由于鬼可以穿墙,所以判断人和鬼是否相遇时根据曼哈顿距离来算就行。当时间time*2<曼哈顿距离时会和鬼相遇。

Code

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
#include<bits/stdc++.h>#define INIT(a,b) memset(a,b,sizeof(a))#define LL long longusing namespace std;const int inf=0x3f3f3f3f;const int N=1e3+7;const int mod=1e9+7;char mp[N][N];int vis[2][N][N];int dir[4][2]={1,0,-1,0,0,1,0,-1};int n,m,ans;struct node{    int x,y;}M,G,Z[2];queue<node> que[2];bool judge(int tab,node a){    for(int i=0;i<2;i++)        if(ans*2 >= abs(Z[i].x-a.x)+abs(Z[i].y-a.y) )            return false;    if(a.x<0||a.x>=n||a.y<0||a.y>=m)return false;    if(mp[a.x][a.y]=='X')return false;    return true;}int solve(int tab,int step){    node tem,q;    queue<node> qt;    qt=que[tab];    for(int i=0;i<step;i++){         while(!qt.empty()){            q=qt.front();qt.pop();que[tab].pop();            if(!judge(tab,q))continue;            for(int i=0;i<4;i++){                tem.x=q.x+dir[i][0],tem.y=q.y+dir[i][1];                if(!judge(tab,tem)||vis[tab][tem.x][tem.y])continue;                if(vis[tab^1][tem.x][tem.y]) return 1;                vis[tab][tem.x][tem.y]=1;                que[tab].push(tem);            }        }        qt=que[tab];    }    return 0;}int bfs(){    ans=0;    INIT(vis,0);    while(!que[0].empty())que[0].pop();    while(!que[1].empty())que[1].pop();    que[0].push(M);que[1].push(G);    vis[0][M.x][M.y]=vis[1][G.x][G.y]=1;    while(!que[0].empty()||!que[1].empty()){        ans++;        if(solve(0,3)||solve(1,1))            return ans;    }    return -1;}int main(){   int t;    scanf("%d",&t); while(t--){        int z=0;        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++){            //scanf("%s",mp[i]);            getchar();            for(int j=0;j<m;j++){                mp[i][j]=getchar();                if(mp[i][j]=='M') M=(node){i,j};                if(mp[i][j]=='G') G=(node){i,j};                if(mp[i][j]=='Z') Z[z++]=(node){i,j};            }        }        printf("%d\n",bfs()); }

   return 0;}

Nightmare2相关推荐

  1. bottom sheets_Excel 2013中的SHEET和SHEETS函数

    bottom sheets I've been experimenting with the new SHEET and SHEETS functions in Excel 2013, to see ...

最新文章

  1. 人大赵鑫:基于图神经网络,建模知识图谱
  2. Linux进程间通信中的文件和文件锁
  3. 展示前一阵子做的小熊!
  4. MySQL日期和时间类型
  5. Zabbix探索:无法获取Windows主机CPU利用率、负载等问题处理
  6. React中diff算法的理解
  7. [OpenShift 4 - DevSecOps Workshop (16) - 使用 VSCode 编辑运行 Tekton Pipeline 资源
  8. ASP.NET Identity “角色-权限”管理 2
  9. 初识Flink广播变量broadcast
  10. 040、JVM实战总结:案例实战:每日百亿数据量的实时分析引擎,为啥频繁发生Full GC ?
  11. [转] 春晚诗朗诵《心里话》之程序员版!!
  12. SpringBoot的自定义配置方法一,通过自定义配置文件
  13. hdoj 4940 强连通图
  14. 17.3.13--pycharm2和3的常识问题
  15. 数据结构(C语言)基础入门
  16. linux 流量蠕虫 查杀,linux系统服务器中的蠕虫病毒怎么清除
  17. android画板过程分析,Android涂鸦画板原理详解——从初级到高级(二)
  18. 无线打印机与手机连接到服务器,图文详解手机通过无线Wifi连接打印机的方法...
  19. “人活着是为了什么?”“生活的中心是什么?”
  20. OOP 概念的编程基础思想----我的感想

热门文章

  1. 不等距双杆模型_电磁感应之双杆模型ppt课件
  2. 2021-2-26编程语言知识点整理
  3. 2020最新安卓版本是多少_百度浏览器2020最新版本下载,百度浏览器2020官方最新版本下载安装 v7.19.13.0...
  4. 百信社区-社区服务一体化
  5. linux定时删除或者压缩日志文件
  6. 眼见不一定为实!18个神奇的视错觉,看完不相信眼睛系列
  7. 【工具】系统工具推荐网速显示等
  8. python inputs[:,::2,::2,:]
  9. 获取中国银行网页中外汇率
  10. Unity3D Behavior Designer 行为树1 认识