Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14260 Accepted Submission(s): 6930
Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
Sample Input
3
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
Sample Output
4
-1
13
Author Ignatius.L
Recommend We have carefully selected several similar problems for you: 1026 1016 1010 1175 1180
-------------------------------------------------------------------------------------------------------------------------------------------------------
这道题目显然是用bfs来做的
但是有几点必须要考虑
因为存在时间归零的情况
所以一个点可能经过多次
又因为时间归零了过后
在这一点会有重复
所以将n=4的点标记为走过即可
这样最后如果到不了就会队列会一直运行直到空
下面拿出我有超多注释
谁都能看懂的代码
-------------------------------------------------------------------------------------------------------------------------------------------------------
1 //Author:LanceYu 2 #include3 #include<string> 4 #include ||map[dx][dy]==2)||map[dx][dy]==3)//2也有可能重复走过 60 { 61 q.push(node{dx,dy,t.t-1,t.step+1});//此处可以重复走故vis不进行赋值 62 } 63 if(dx>=0&&dy>=0&&dx5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include<set> 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #define ll long long 25 using namespace std; 26 const double clf=1e-8; 27 //const double e=2.718281828; 28 const double PI=3.141592653589793; 29 const int MMAX=2147483647; 30 //priority_queue p; 31 //priority_queue,greater 32 int n,m,map[101][101]; 33 int vis[101][101]; 34 int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; 35 struct node 36 { 37 int x,y,t,step; 38 }; 39 40 int bfs(int a,int b,int x,int y) 41 { 42 int i; 43 queue>pq; q; 44 while(!q.empty()) 45 q.pop(); 46 q.push(node{a,b,6,0});//开始也能回来再走一次vis保持0 47 while(!q.empty()) 48 { 49 node t=q.front(); 50 q.pop(); 51 if(t.x==x&&t.y==y) 52 return t.step; 53 if(t.t<=1)//会爆炸的情况扔掉 54 continue; 55 for(int i=0;i<4;i++) 56 { 57 int dx=t.x+dir[i][0]; 58 int dy=t.y+dir[i][1]; 59 if(dx>=0&&dy>=0&&dx 1 4) 64 { 65 vis[dx][dy]=1; 66 q.push(node{dx,dy,6,t.step+1});//因为是直接归零,没必要重复走故直接把vis变成1 67 } 68 } 69 } 70 return -1; 71 } 72 int main() 73 { 74 int t,a,b,x,y; 75 cin>>t; 76 while(t--) 77 { 78 scanf("%d%d",&n,&m); 79 memset(vis,0,sizeof(vis)); 80 for(int i=0;i ) 81 { 82 for(int j=0;j ) 83 { 84 scanf("%d",&map[i][j]); 85 if(map[i][j]==2)//记录起点 86 { 87 a=i; 88 b=j; 89 } 90 if(map[i][j]==3)//记录终点 91 { 92 x=i; 93 y=j; 94 } 95 } 96 } 97 int ans=bfs(a,b,x,y); 98 printf("%d\n",ans); 99 } 100 return 0; 101 }
Notes:思想要求较高
PS:笔者在做这道题的时候把bfs里面的两个判断语句弄反了,WA了好多次,可能这就是菜吧
2018-11-16 05:09:49 Author:LanceYu