Nightmare Ⅱ
来源:互联网 发布:酒店软件系统排名 编辑:程序博客网 时间:2024/05/17 01:18
Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1788 Accepted Submission(s): 452
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
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cmath>#include <algorithm>#define N 1010using namespace std;char map[N][N];int t,n,m,step;int sx,sy,ex,ey;int z[3][3];int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};struct node{ int x,y;};int check(int x,int y)//曼哈顿距离判断{ int x1=abs(x-z[0][0])+abs(y-z[0][1]); int x2=abs(x-z[1][0])+abs(y-z[1][1]); if((x1<=2*step)||(x2<=2*step)) return 0; return 1;}queue<node>q[3];int bfs(int num, int k, char s, char e){ q[2] = q[num]; for(int t = 0; t<k; t++) { while(!q[2].empty()) { node st = q[2].front(); q[2].pop(); q[num].pop(); node ed; if(!check(st.x, st.y)) continue; for(int i = 0; i<4; i++) { int nx = st.x + dir[i][0]; int ny = st.y + dir[i][1]; if(nx < 0 || ny < 0 || nx >= n || ny >= m || map[nx][ny] == 'X') continue; if(!check(nx, ny) || map[nx][ny] == s) continue; if(map[nx][ny] == e) return 1; map[nx][ny] = s; ed.x = nx, ed.y = ny; q[num].push(ed); } } q[2] = q[num]; } return 0;}void BFS(){ int i; step=0; for(i=0; i<3; i++) { while(!q[i].empty()) q[i].pop(); } node a; a.x=sx,a.y=sy; q[0].push(a); a.x=ex,a.y=ey; q[1].push(a); while(!q[0].empty()&&!q[1].empty()) { step++; if(bfs(0,3,'M','G')||bfs(1,1,'G','M')) { printf("%d\n",step); return; } } printf("-1\n"); return;}int main(){ int i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); k=0; for(i=0; i<n; i++) { scanf("%s",map[i]); for(j=0; j<m; j++) { if(map[i][j]=='M') sx=i,sy=j; else if(map[i][j]=='G') ex=i,ey=j; else if(map[i][j]=='Z') { z[k][0]=i,z[k][1]=j; k++; } } } BFS(); } return 0;}
- Nightmare Ⅱ
- HDOJ 3085 Nightmare Ⅱ
- HDU-3085 Nightmare Ⅱ
- HDU 3085 Nightmare Ⅱ
- HDU 3085 Nightmare Ⅱ
- Nightmare Ⅱ HDU
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Nightmare
- Hadoop 2.0工作原理学习
- Error:Execution failed for task ':app:processDebugResources'.解决
- ajax、反向ajax、jsonp详解
- Strlen在实际使用中的一点体会
- 隐藏状态栏
- Nightmare Ⅱ
- Android自定义BaseAdapter看看小动物们在说啥
- Android属性动画——实现灵动菜单效果
- HDU3507 Print Article
- 使用Flexible实现手淘H5页面的终端适配
- Yii2数据库查询有关的操作
- ORIGIN 9.0 拟合曲线延伸
- c++ 如何获取当前登录用户的名字
- Qt5 - 中文 - 常量中有换行符