hdu1072 BFS Time Optimization pruning

Source: Internet
Author: User

Nightmare

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 9424 Accepted Submission (s): 4551


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 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 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 equ Ipment to reset the bomb.
5. A Bomb-reset-equipment can used as many times as you wish, if it's needed, Ignatius can get to any areas in the lab Yrinth 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-rest- 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
7 ·
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 output4-113 Very simple BFS+DP, memory search, only when to Dp[now]<dp[pre], to reach pity Dorado, Dp[now] indicates the current time to reach this point of the rest of the afternoon to listen to the MS BB Training team, the topic has been looked at for a long time, back to the bedroom immediately have ideas
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<stack>#include<queue>#include<string>Const intINF = (1<< to)-1;Const intMAXN =1e1;using namespacestd;structstep{intx; inty; intT; intlimit;};intmov[4][2]={-1,0,1,0,0,1,0,-1};queue<step>Q;CharG[MAXN][MAXN];intDP[MAXN][MAXN];intn,m;intCheckintXinty) {    if(x<0|| y<0|| x>=n| | Y&GT;=M)return 0; if(g[x][y]=='0')return 0; Else return 1;}voidinit () { for(intI=0; i<n;i++){         for(intj=0; j<m;j++) Dp[i][j]=inf; }}intMain () {intT; intSx,sy; intFlag; Charts[3]; scanf ("%d",&t);  while(t--) {flag= -1; scanf ("%d%d",&n,&m);        Init ();  for(intI=0; i<n;i++){             for(intj=0; j<m;j++) {scanf ("%s", TS); G[I][J]= ts[0]; if(g[i][j]=='2') {SX=i; Sy=J; }            }        }      /*printf ("debug\n");        for (int i=0;i<n;i++) {printf ("%s\n", G[i]); }*/step Tp,fro; Tp.x=SX; TP.Y=Sy; TP.T=0; Tp.limit=0;        Q.push (TP); G[sx][sy]='1'; Dp[sx][sy]=0; intNx,ny;  while(!Q.empty ()) {Fro=Q.front ();            Q.pop (); if(g[fro.x][fro.y]=='3') {flag=fro.t;  Break; }            if(fro.limit>=5)Continue;  for(intI=0;i<4; i++) {NX= fro.x+mov[i][0]; NY= fro.y+mov[i][1]; if(check (Nx,ny)) {if(g[nx][ny]=='4') {Tp.limit=0; }Else{tp.limit= fro.limit+1; }                    if(tp.limit<Dp[nx][ny]) {tp.t= fro.t +1; Tp.x=NX; TP.Y=NY;                        Q.push (TP); Dp[nx][ny]=Tp.limit; }                }            }        }         while(!Q.empty ())        {Q.pop (); }       /*for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {cout<<dp[i][j]<< "";        } cout<<endl; }*/cout<<flag<<Endl; }    //cout << "Hello world!" << Endl;    return 0;}/*33 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*/
View Code

hdu1072 BFS Time Optimization pruning

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.