hdoj1429 (bfs + compressed state)

The Great Escape (cont.)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12549    Accepted Submission(s): 4560


Problem Description
Ignatius was again captured the devil (do not understand why he was so fond of the devil discuss) ......

The devil learned the lessons of the last, the Ignatius n * m locked in a dungeon, and installed in some parts of the dungeon a locked door, another key hidden in some parts of the dungeon. Ignatius was kept in the beginning (sx, sy) position, away from the position of the door in the dungeon (ex, ey) of. Ignatius can only come from a coordinate adjacent to one of the four coordinates per minute. Lord minutes back to the dungeon inspected once every t, if found not original location Ignatius put him carry her back. Been tried several times, Ignatius has drawn the entire dungeon map. Now you can help him calculate once again successfully escape. As long as the export went to the devil before the next inspection even leave the dungeon, if the devil came back just yet to come to exports or export is considered a fugitive fail.
 

 

Input
The first line of each test there are three integers n, m, t (2 < = n, m <= 20, t> 0). The next n rows and m as dungeon map, including:

representative path
* Representative wall
starting position of Ignatius @Delegate
The representative dungeon outlet
AJ representatives of locked doors, respectively corresponding to the key AJ
AJ representative of the key, AJ respectively corresponding to the door

there is a blank line between each test.
 

 

Output
For each test, if you can successfully escape, your output needs how many minutes to leave, if not then output -1.
 

 

Sample Input
4 5 17
@A.B.
a*.*.
*..*^
c..b*
4 5 16
@A.B.
a*.*.
*..*^
c..b*
 

 

Sample Output
16
-1
 

 

Author
LL
 
 Compressed state has a state key, each key corresponds to a binary 1 and 0 in the removed bits in response to the event of the door can be aligned. code show as below:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 char mp[25][25],f[21][21][1205];
 6 int n,m,sx,sy,ex,ey,head,tail,nx,ny,T,ans;
 7 int dx[4]={1,-1,0,0};
 8 int dy[4]={0,0,1,-1};
 9 struct node{
10     int x,y,k,t;
11 }q[500005];
12 int main(){
13     char o;
14     while(scanf("%d%d%d",&n,&m,&T) != EOF){
15         ans = 0;
16         memset(f,0,sizeof f);
17         for(int i=1;i<=n;++i)   scanf("%s",mp[i]+1);
18         for(int i=1;i<=n;++i){
19             for(int j=1;j<=m;j++) {
20                 if (mp[i][j] == '@') sx = i, sy = j;
21                 if (mp[i][j] == '^') ex = i, ey = j;
22             }
23         }
24 
25         head = tail = 1;
26         q[1].x = sx;   q[1].y = sy;
27         f[sx][sy][0] = 1;
28         while(head <= tail){
29             int x,y,k,t;
30             x = q[head].x;y = q[head].y;k = q[head].k;t = q[head].t;
31             //if(t == 4)  printf("t=%d %d %d %d\n",t,x,y,k);
32             if(t == T-1){
33                 break;
34             }
35             for(int i=0;i<4;++i){
36                 nx = x + dx[i]; ny = y + dy[i];
37                 if(nx>0 && nx <= n && ny>0 && ny <= m){
38                     if(nx == ex && ny == ey){
39                         ans = t+1;
40                         break;
41                     }
42                     o = mp[nx][ny];
43                     if(o == '*')    continue;
44                     if((o == '.' || o == '@') &&  !45f [nx] [] [k]) {
                         q[++tail].x = nx;   q[tail].y = ny; q[tail].k = k;q[tail].t = t + 1;
46                         f[nx][ny][k] = 1;
47                     }
48                     else if(o >= 'a' && o <= 'z'){
49                         int z = k>>(o-'a') & 1;
50                         if(!z){
51                             int zz = 1<<(o-'a');
52                             q[++tail].x = nx;   q[tail].y = ny; q[tail].k = k + zz;    q[tail].t = t + 1;
53                             f[nx][ny][k+zz] = 1;
54                         }
55                         else if(!f[nx][ny][k]){
56                             q[++tail].x = nx;   q[tail].y = ny; q[tail].k = k;q[tail].t = t + 1;
57                             f[nx][ny][k] = 1;
58                         }
59                     }
60                     else if(o >='A' && o <= 'Z'){
61                         int z = k>>(o-'A')&1;
62                         if(z == 1 && !f[nx][ny][k]){
63                             q[++tail].x = nx;   q[tail].y = ny; q[tail].k = k;q[tail].t = t + 1;
64                             f[nx][ny][k] = 1;
65                         }
66                     }
67                 }
68             }
69             if(ans) break;
70             head++;
71         }
72         if(ans) printf("%d\n",ans);
73         else    printf("-1\n");
74     }
75     return 0;
76 }

 

Guess you like

Origin www.cnblogs.com/fryoux/p/11582369.html