Topic links: http://icpc.njust.edu.cn/Problem/Hdu/1072/
Meaning of the questions: escape from the maze, there may be a bomb in the road, with a total time of six units in the location of a bomb, if the arrival time period is greater than 0, then returned to 6 times, the location of the bomb can repeat arrival, at least obtain a final how many need to step out of the maze to reach the end. Such optimization problem and map-related, bfs should be sufficient to solve. We consider that a position may be several visits, the state should set a time parameter set to the remaining time of the visit, because if a location is the first visit when the remaining time t, the next visit if time still remaining t, then walk a few steps must be better than the first visit to the big, it can not be the optimal solution, which is a pruning. To pay attention every time status updates to determine whether the state should have been searched in the presence of the state space. Next, conditional more unreachable state, between cur-> nxt as toggles required to change the state of all enumerated.
code show as below:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define dbg(args) cout<<#args<<":"<<args<<endl; 17 #define inf 0x3f3f3f3f 18 #define maxn 105 19 int n,m,t,sx,sy,tx,ty; 20 int Map[maxn][maxn]; 21 int dir[][2]={0,1,0,-1,1,0,-1,0}; 22 is struct Node { 23 is int X, Y, Time, STEP; 24 }; 25 BOOL VIS [MAXN] [MAXN] [MAXN]; 26 is // also need to consider the time out of the labyrinth, it is possible in a different location time went by 27 // first time come when the remaining time t (i, j) position of a certain minimum step 28 Node CUR, NXT; 29 int BFS () 30 { 31 is Queue <Node> Q; 32 Node ST ; 33 is st.x = SX, SY = st.y, st.time = . 6 , st.step = 0 ; 34 is q.push (ST); 35 VIS [SX] [SY] [ . 6 ] = to true; 36 // a location if the remaining time t has come, that is passing by at the next time t, then take a more distance must be 37 [ the while (! Q.empty ()) 38 is { 39 CUR = q.front (); 40 q.pop (); 41 is IF (cur.x == == TX TY && && cur.y cur.time> 0 ) 42 is { 43 is return cur.step; 44 is } 45 F (I, 0 , . 3 ) 46 is { 47 NXT = CUR; 48 nxt.x + = the dir [I] [ 0 ]; 49 + = the dir nxt.y [I] [ . 1 ]; 50 nxt.time-- ; 51 is nxt.step ++ ; 52 is // Note exemplified state can not walk, 53 is IF (nxt.x < . 1 || nxt.x> n-| | nxt.y < . 1 || nxt.y> m || the Map [nxt.x] [nxt.y] == 0 || nxt.time <= 0 ) Continue ; 54 is IF (VIS [nxt.x] [ nxt.y] [nxt.time]) Continue ; 55 56 is // Since the state quantity is time, so each change must check the state of the state space visited 57 is iF (the Map [nxt.x] [NXT .y] == . 4 && nxt.time> 0 ) 58 { 59 nxt.time=6; 60 if(vis[nxt.x][nxt.y][nxt.time])continue; 61 } 62 vis[nxt.x][nxt.y][nxt.time]=1; 63 q.push(nxt); 64 } 65 } 66 return -1; 67 } 68 int main() 69 { 70 //freopen("input.txt","r",stdin); 71 //freopen("output.txt","w",stdout); 72 std::ios::sync_with_stdio(false); 73 scan(t); 74 while(t--) 75 { 76 mem(vis,false); 77 scan(n); 78 scan(m); 79 char c; 80 f(i,1,n) 81 f(j,1,m) 82 { 83 scanf(" %c",&c); 84 Map[i][j]=c-'0'; 85 if(Map[i][j]==3)tx=i,ty=j; 86 if(Map[i][j]==2)sx=i,sy=j; 87 } 88 pf("%d\n",bfs()); 89 } 90 }