Nightmare hdu1072
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8871 Accepted Submission(s): 4270
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 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
此题重点是有些点可以重复走,但是重置时间点不需要从新走,如果走到该点tim小于以前的或者走到该点路程更短走该地可以重复走。
此题可以用BFS来写,下次补充BFS代码
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define size 10 5 #define Max 10000 6 using namespace std; 7 int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0}; 8 int map[size][size]; 9 int ti[size][size]; 10 int st[size][size]; 11 int n,m; 12 int tim,len; 13 bool flag=0; 14 int Minlen=Max; 15 int xs,ys; 16 bool flag0; 17 int nx,ny; 18 int dfs(int x,int y,int tim,int len) 19 { 20 int i,j; 21 if(map[x][y]==0||tim<=0||x<0||x>=n||y<0||y>=m||len>=Minlen) 22 return 0; 23 if(map[x][y]==3&&len<Minlen) 24 { 25 flag=1; 26 Minlen=len; 27 return 0; 28 } 29 if(map[x][y]==4) 30 tim=6; 31 if(tim>ti[x][y]||len<st[x][y]) 32 { 33 ti[x][y]=tim; 34 st[x][y]=len; 35 for(i=0;i<4;i++) 36 { 37 nx=x+dx[i]; 38 ny=y+dy[i]; 39 dfs(nx,ny,tim-1,len+1); 40 } 41 } 42 return 0; 43 } 44 int main() 45 { 46 int T; 47 int i,j; 48 freopen("in.txt","r",stdin); 49 cin>>T; 50 while(T--) 51 { 52 cin>>n>>m; 53 for(i=0;i<n;i++) 54 for(j=0;j<m;j++) 55 { 56 cin>>map[i][j]; 57 if(map[i][j]==2) 58 { 59 xs=i; 60 ys=j; 61 } 62 } 63 len=0; 64 tim=6; 65 Minlen=Max; 66 flag=0; 67 memset(ti,0,sizeof(ti)); 68 for(i=0;i<size;i++) 69 for(j=0;j<size;j++) 70 st[i][j]=200000; 71 dfs(xs,ys,tim,len); 72 if(!flag) cout<<-1<<endl; 73 else cout<<Minlen<<endl; 74 } 75 }