The Great Escape of victory (continued)
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 7357 Accepted Submission (s): 2552
Problem Descriptionignatius again by the Devil was captured (do not understand how he so pleasing to the devil like) ...
This time the devil learned from the last lesson, the Ignatius in a N*m dungeon, and in some parts of the dungeon installed a locked door, the key hidden in the dungeon somewhere else. At first the Ignatius was held in (Sx,sy) position, leaving the dungeon door in the (Ex,ey) position. Ignatius can only walk from one coordinate to one of the adjacent four coordinates per minute. The Devil every t minute back to the dungeon inspection once, if found Ignatius not in the original position will carry him back. After several attempts, Ignatius has drawn a map of the entire dungeon. Now, please help him to calculate the possibility of a successful escape again. As long as the devil before the next visit to the exit of the dungeon, even if the devil came back to the exit or not to the exit is counted as escape failure.
Input has three integer n,m,t (2<=n,m<=20,t>0) for the first row of each set of test data. The next n rows of M are listed in the Dungeon Map, which includes:
. The Representative road * represents the wall @ the starting position of the Ignatius ^ represents the dungeon's exit A-j represents the locked door, the corresponding key is A-j a-j for the key, the corresponding door is a-j
There is a blank line between each set of test data.
Output for each set of test data, if you can successfully escape, please export how many minutes to leave, if not the output-1.
Sample Input4 5 17@a.b.a*.*.*. *^c. B*4 5 16@a.b.a*.*.*. *^c. b*
Sample output16-1
Authorll
Puzzle: State compression BFS, game of the time unlimited me .... Heart 10,000 grass mud horse Pentium ... Finally everyone said to use the state to compress the key,,, instantly feel their IQ negative infinite ...
In fact, it is not difficult to think, the key was not how to use the state compression, at the time thought of using the VIS storage position and the number of keys, but do not know how to record, why unexpected state compression na ... To have a long memory;
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7 using namespacestd;8 structnode{9 intNx,ny,step;Ten //int key[11]; One intkey; A }; - Charmp[ -][ -]; - intvis[ -][ -][1025]; the intdisx[4] = {0,0,1,-1}; - intdisy[4] = {1,-1,0,0}; - intn,m,t; - voidBFsintSxintSy) { +memset (Vis,0,sizeof(Vis)); -Queue<node>Q; + Node A, b; AA.nx = Sx;a.ny =Sy; atA.step =0; - //for (int i = 0;i < 11;i++) A.key[i] = 0; -A.key =0; -vis[sx][sy][0] =1; - Q.push (a); - while(!Q.empty ()) { inA =Q.front (); - Q.pop (); to for(inti =0; I <4; i++){ +B.nx = A.nx +Disx[i]; -B.ny = A.ny +Disy[i]; theB.step = A.step +1; * intx = b.nx, y = b.ny, step =B.step; $ //for (int i = 0;i < 11;i++) B.key[i] = A.key[i]; Panax NotoginsengB.key =A.key; - if(X <0|| Y <0|| X >= N | | Y >= m)Continue; the if(Mp[x][y] = ='*')Continue; + if(Step >= t)Continue; A if(Mp[x][y] = ='^'){ theprintf"%d\n", step); + return; - } $ //if (Vis[x][y] >) continue; $ if(Mp[x][y] >='a'&& Mp[x][y] <='J'){ - //B.key[mp[x][y]-' a '] = 1; - intp =1<< (Mp[x][y]-'a'); theB.key |=p; - if(!Vis[x][y][b.key]) {WuyiVis[x][y][b.key] =1; the Q.push (b); - } Wu Continue; - } About if(Mp[x][y] >='A'&& Mp[x][y] <='J'){ $ //if (b.key[mp[x][y]-' A '] = = 0) continue; - intp =1<< (Mp[x][y]-'A'); - if(B.key &p) { - if(!Vis[x][y][b.key]) { AVis[x][y][b.key] =1; + Q.push (b); the } - } $ Continue; the } the if(!Vis[x][y][b.key]) { theVis[x][y][b.key] =1; the Q.push (b); - } in } the } thePuts"-1"); About return; the } the intMain () { the while(~SCANF ("%d%d%d",&n,&m,&t)) { + intSx,sy; - for(inti =0; i < n; i++){ thescanf"%s", Mp[i]);Bayi for(intj =0; J < M; J + +){ the if(Mp[i][j] = ='@'){ theSX =i; -SY =J; - } the } the } theMp[sx][sy] ='.'; the BFS (sx,sy); - } the return 0; the}
Victory Grand Escape (continued) (State compression BFS)