(BFS/状态压缩)HDU 5025 Saving Tang Monk

《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts. 

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do. 

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way. 

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in. 

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind). 

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.

InputThere are several test cases. 

For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M). 

Then the N × N matrix follows. 

The input ends with N = 0 and M = 0.OutputFor each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print "impossible"(no quotes).Sample Input

3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0

Sample Output

5
impossible
8

用flag[x][y][key][st] 的四维下标记录 是否在最多得到key钥匙,打败过st(状态压缩)蛇的状态下 到达过坐标(x,y)的位置。

本题与相较于最基础的迷宫问题一是增加了钥匙这一物品,对此只需要通过记录每个状态时得到的钥匙的最大编号即可处理。另一个不同之处就是引入了蛇,并且还存在重新走回原本有蛇的位置的可能。这就需要我们通过状态压缩记录每一个状态时蛇是否被消灭了。为此将蛇预先编上号,状压即可。(总共最多5条,只有32种状态)

  1 #include <iostream>
  2 #include <string>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <queue>
  8 #include <set>
  9 #include <map>
 10 #include <list>
 11 #include <vector>
 12 #include <stack>
 13 #define mp make_pair
 14 //#define P make_pair
 15 #define MIN(a,b) (a>b?b:a)
 16 //#define MAX(a,b) (a>b?a:b)
 17 typedef long long ll;
 18 typedef unsigned long long ull;
 19 const int MAX=1e2+5;
 20 const int INF=1e8+5;
 21 using namespace std;
 22 //const int MOD=1e9+7;
 23 typedef pair<ll,int> pii;
 24 const double eps=0.00000001;
 25 int n,m;
 26 int sx,sy,ex,ey;//起点终点的坐标
 27 int dx[4]={0,0,1,-1};
 28 int dy[4]={1,-1,0,0};
 29 char a[MAX][MAX];
 30 bool vi[MAX][MAX][10][35];
 31 map <pii,int> id;//守卫的编号
 32 bool check(int x,int y)
 33 {
 34     return x>=1&&x<=n&&y>=1&&y<=n&&a[x][y]!='#';
 35 }
 36 struct status
 37 {
 38     short x,y;
 39     short keys;
 40     short fighted;
 41     int steps;
 42     status(){}
 43     status(short p1,short p2,short p3,short p4,int p5){x=p1,y=p2,keys=p3,fighted=p4,steps=p5;}
 44 };
 45 void pre()
 46 {
 47     int cnt=0;
 48     for(int i=1;i<=n;i++)
 49         for(int j=1;j<=n;j++)
 50         {
 51             if(a[i][j]=='K')
 52             {
 53                 sx=i,sy=j;
 54             }
 55             else if (a[i][j]=='T')
 56             {
 57                 ex=i,ey=j;
 58             }
 59             else if(a[i][j]=='S')
 60             {
 61                 id[mp(i,j)]=cnt++;
 62             }
 63         }
 64 }
 65 int bfs()
 66 {
 67     status tem;
 68     tem.x=sx,tem.y=sy,tem.keys=0,tem.fighted=0,tem.steps=0;
 69     queue<status>que;
 70     int an=INF;
 71     que.push(tem);
 72     vi[sx][sy][0][0]=true;
 73     while(!que.empty())
 74     {
 75         tem=que.front();
 76         int nowx=tem.x,nowy=tem.y;
 77         if(nowx==ex&&nowy==ey&&tem.keys==m)
 78             an=min(an,tem.steps);
 79         que.pop();
 80         for(int i=0;i<4;i++)
 81         {
 82             int x1=nowx+dx[i];
 83             int y1=nowy+dy[i];
 84             if(check(x1,y1))
 85             {
 86                 if(a[nowx][nowy]!='S'||((1<<id[mp(nowx,nowy)])&tem.fighted))//非在蛇的位置 或在已经消灭的蛇那里
 87                 {
 88                     if(a[x1][y1]>='1'&&a[x1][y1]<='9')
 89                     {
 90                         int num=a[x1][y1]-'0';
 91                         if(tem.keys+1==num)//如果是可以得到的新钥匙
 92                         {
 93                             if(!vi[x1][y1][num][tem.fighted])
 94                             {
 95                                 vi[x1][y1][num][tem.fighted]=true;
 96                                 que.push(status(x1,y1,num,tem.fighted,tem.steps+1));
 97                             }
 98                         }
 99                         else
100                         {
101                             if(!vi[x1][y1][tem.keys][tem.fighted])
102                             {
103                                 vi[x1][y1][tem.keys][tem.fighted]=true;
104                                 que.push(status(x1,y1,tem.keys,tem.fighted,tem.steps+1));
105                             }
106                         }
107                     }
108                     else//普通的房间
109                     {
110                         if(!vi[x1][y1][tem.keys][tem.fighted])
111                         {
112                             vi[x1][y1][tem.keys][tem.fighted]=true;
113                             que.push(status(x1,y1,tem.keys,tem.fighted,tem.steps+1));
114                         }
115                     }
116                 }
117                 else//当前在有蛇 且没有与其打过的房间
118                 {
119                     if(a[x1][y1]>='1'&&a[x1][y1]<='9')
120                     {
121                         int num=a[x1][y1]-'0';
122                         if(tem.keys+1==num)//如果是可以得到的新钥匙
123                         {
124                             if(!vi[x1][y1][num][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
125                             {
126                                 vi[x1][y1][num][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
127                                 que.push(status(x1,y1,num,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
128                             }
129                         }
130                         else
131                         {
132                             if(!vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
133                             {
134                                 vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
135                                 que.push(status(x1,y1,tem.keys,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
136                             }
137                         }
138                     }
139                     else//普通的房间
140                     {
141                         if(!vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
142                         {
143                             vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
144                             que.push(status(x1,y1,tem.keys,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
145                         }
146                     }
147                 }
148             }
149         }
150     }
151     return an;
152 }
153 int main()
154 {
155     while(scanf("%d%d",&n,&m)&&n)
156     {
157         memset(vi,false,sizeof(vi));
158         id.clear();
159         for(int i=1;i<=n;i++)
160             scanf("%s",a[i]+1);
161         pre();
162         int an=bfs();
163         if(an==INF)
164             printf("impossible\n");
165         else
166             printf("%d\n",an);
167     }
168     return 0;
169 }

 

posted @ 2017-07-20 19:33  perplex  阅读(199)  评论(0编辑  收藏  举报