標籤:des style blog color java os io strong
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7083 Accepted Submission(s): 3409
Problem DescriptionIgnatius 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.
InputThe 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.
OutputFor 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 Input33 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 35 81 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 Output4-113 題目意思:給出一個n*m的圖,初始時一個人在map[i][j]=2處,只能向上、下、左、右移動1格,每走一步時間減1,初始化時間為6,當時間為1時還沒找到出口3位置處,就輸出1,否則輸出最少走的步數,0是牆,1是空地,2是初始所在地方,3是目標,4可以把時間重設為6。 思路:最優走迷宮問題一般就是bfs,類比一下即可,判斷下一步是否可走,若可走且走一步後時間不為0,就走這一步,然後判斷此處是否是目標,是的話返回步數,是否是4,是的話更新時間。在My Code中加了個visited[][]數組進行最佳化,因為每個位置最多走4次即可,再多的話進行無用功或者一直在兩個地方之間徘徊跳不出bfs函數。 代碼:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <iostream> 7 using namespace std; 8 9 struct node {10 int x, y, w, step;11 };12 13 int n, m;14 int map[10][10];15 int visited[10][10];16 int xx[]={1,-1,0,0};17 int yy[]={0,0,1,-1};18 19 int judge(int x,int y){20 if(map[x][y]==0||x<0||x>=n||y<0||y>=m) return 0;21 return 1;22 }23 24 int bfs(int x,int y){25 int i, j;26 node p, q;27 p.x=x;p.y=y;p.w=6;p.step=0;28 queue<node>Q;29 Q.push(p);30 while(!Q.empty()){31 p=Q.front();32 Q.pop();33 for(i=0;i<4;i++){34 q.x=p.x+xx[i];35 q.y=p.y+yy[i];36 q.w=p.w-1;37 q.step=p.step+1;38 if(judge(q.x,q.y)){39 if(q.w>0&&visited[q.x][q.y]<4&&map[q.x][q.y]!=2){ //因為從2出發,再回到2已經多餘了 40 if(map[q.x][q.y]==3) return q.step;41 else if(map[q.x][q.y]==4){42 q.w=6;43 }44 visited[q.x][q.y]++;45 Q.push(q);46 }47 }48 }49 }50 return 0;51 }52 main()53 {54 int i, j, t, x, y;55 cin>>t;56 while(t--){57 memset(visited,0,sizeof(visited));58 scanf("%d %d",&n,&m);59 for(i=0;i<n;i++){60 for(j=0;j<m;j++){61 scanf("%d",&map[i][j]);62 if(map[i][j]==2){63 x=i;y=j;64 }65 }66 }67 int ans=bfs(x,y);68 if(!ans) printf("-1\n");69 else printf("%d\n",ans);70 }71 }