Nightmare (DFS)

Source: Internet
Author: User

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 Descriptionignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The Labyrinth has a exit, Ignatius should get out of the 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, which is to move from one area to the nearest are A (that's, 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) Tak Es 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 The Labyr Inth, if he could, output the minimum time that he had to use to find the exit of the The labyrinth, else output-1.

Here is some rules:
1. We can assume the Labyrinth is a 2 array.
2. Each minute, Ignatius could-only get-one of the nearest area, and he should not walk out of the border, of cours E 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 The Labyrinth.
4. If Ignatius get to the area which contains bomb-rest-equipment then the exploding time turns to 0, he can ' t use the Equipment to reset the bomb.
5. A bomb-reset-equipment can used as many times as you wish, if it was needed, Ignatius can get to all areas in the Labyrinth as many times as you wish.
6. The time to reset the exploding time can is ignore, in and words, if Ignatius get to a area which contain bomb-r Est-equipment, and the exploding time is larger than 0, the exploding time would being reset to 6.

Inputthe input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is starts with the 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 is five integers which indicate the different type of area in the Labyrinth:
0:the area was 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 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

Sample Output
4
-1
13

The point is that some points can be repeated, but resetting the point of time does not require a new walk, if you go to the point where the TIM is smaller than the previous, or go to the point shorter distances to go to the place can be repeated.

The problem can be written in BFs, the next replenishment of the BFS code

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineSize 105 #defineMax 100006 using namespacestd;7 intdx[4]={0,0,1,-1},dy[4]={1,-1,0,0};8 intMap[size][size];9 intTi[size][size];Ten intSt[size][size]; One intn,m; A intTim,len; - BOOLflag=0; - intminlen=Max; the intXs,ys; - BOOLflag0; - intNx,ny; - intDfsintXintYintTimintlen) + { -     inti,j; +     if(map[x][y]==0|| tim<=0|| x<0|| x>=n| | y<0|| y>=m| | len>=Minlen) A         return 0; at     if(map[x][y]==3&&len<Minlen) -     { -flag=1; -minlen=Len; -         return 0; -     } in     if(map[x][y]==4) -tim=6; to     if(tim>ti[x][y]| | len<St[x][y]) +     { -ti[x][y]=Tim; thest[x][y]=Len; *          for(i=0;i<4; i++) $         {Panax Notoginsengnx=x+Dx[i]; -ny=y+Dy[i]; theDFS (nx,ny,tim-1, len+1); +         } A     } the     return 0; + } - intMain () $ { $     intT; -     inti,j; -Freopen ("In.txt","R", stdin); theCin>>T; -      while(t--)Wuyi     { theCin>>n>>m; -          for(i=0; i<n;i++) Wu              for(j=0; j<m;j++) -             { AboutCin>>Map[i][j]; $                 if(map[i][j]==2) -                 { -xs=i; -ys=J; A                 }     +             } thelen=0; -tim=6; $minlen=Max; theflag=0; theMemset (Ti,0,sizeof(TI)); the          for(i=0; i<size;i++) the              for(j=0; j<size;j++) -st[i][j]=200000; in DFS (Xs,ys,tim,len); the         if(!flag) cout<<-1<<Endl; the         Elsecout<<minlen<<Endl; About     } the}

Nightmare (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.