HDU 5025 Saving Tang Monk(bfs+状态压缩)

来源:互联网 发布:电影符号学 知乎 编辑:程序博客网 时间:2024/05/20 03:06

Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2011    Accepted Submission(s): 720


Problem Description
《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.
 

Input
There 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.
 

Output
For 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 1K.S##11#T3 1K#T.S#1#.3 2K#T.S.21.0 0
 

Sample Output
5impossible8
 

Source
2014 ACM/ICPC Asia Regional Guangzhou Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5679 5678 5677 5676 5674 
 

题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐僧,集齐m个钥匙之前可以经过唐僧,集齐x个钥匙以前可以经过x+1,x+2..个钥匙,问最少多少步救到唐僧。
第一次接触状态压缩,写的我完全凌乱了,并且比较要意思的一个地方是用二进制记录钥匙是否拿过,并且需要用到三维数组
这里附上代码加注释
#include<cstdio>  #include<cstring>  #include<queue>  #include<algorithm>  using namespace std;  int dx[4]={0,0,1,-1};  int dy[4]={1,-1,0,0};  char map[110][110];  int vis[110][110][1024],n,m,sx,sy,f;  int a[15]={1,2,4,8,16,32,64,128,256,512,1024,2048};//非常有意思的一个地方  struct node  {      int x,y;//当前坐标 int step;//当前走的步数 int key; //收集的钥匙数     int flog[12];      int s; //蛇的状态,用二进制数表示,某位为一,表示已经杀过     int X[12],Y[12];//杀掉蛇的坐标      friend bool operator < (node s1,node s2)      {          return s1.step>s2.step;      }  }p,temp;  bool judge(node s)  {      if(s.x<0||s.x>=n||s.y<0||s.y>=n||map[s.x][s.y]=='#'||vis[s.x][s.y][s.key])      return true;      return false;  }  void bfs()  {      priority_queue<node>q;      while(!q.empty())q.pop();      memset(vis,0,sizeof(vis));      p.s=0;       p.x=sx;      p.y=sy;      p.key=0;       p.step=0;      for(int i=0;i<=10;i++){     p.X[i]=p.Y[i]=0;  p.flog[i]=0;  }      q.push(p);//放入起点      vis[p.x][p.y][p.key]=1;      while(!q.empty())      {          p=q.top();          q.pop();          for(int i=0;i<4;i++)          {              temp.s=p.s;              temp.x=p.x+dx[i];              temp.y=p.y+dy[i];              temp.key=p.key;             for(int j=0;j<=10;j++)              {                  temp.flog[j]=p.flog[j];                  temp.X[j]=p.X[j];                  temp.Y[j]=p.Y[j];              }              if(judge(temp)) continue;              if(map[temp.x][temp.y]=='T'&&temp.key==f)              {                  printf("%d\n",p.step+1);                  return ;//如果遇到了唐僧,并且钥匙收集完毕               }              if(map[temp.x][temp.y]=='S')              {                  int ans=0;//如果遇到一条蛇,判断是否杀过                   for(int j=0;j<p.s;j++)                  {                      if(p.X[j]==temp.x&&p.Y[j]==temp.y)                      {                          ans=1;                      }                  }                  if(ans==1) temp.step=p.step+1;//杀过的话步数加一                   else                  {                      temp.X[temp.s]=temp.x;                      temp.Y[temp.s]=temp.y;                      temp.s++;//没杀过的话杀死步数加二,并且记录                       temp.step=p.step+2;                  }              }              else temp.step=p.step+1;              if(map[temp.x][temp.y]=='1')              {                  if(!p.flog[map[temp.x][temp.y]-'1'])                  {//如果是第一把钥匙,并且没有,直接获取                       temp.key+=a[map[temp.x][temp.y]-'1'];                      temp.flog[map[temp.x][temp.y]-'1']=1;                  }                  vis[temp.x][temp.y][temp.key]=1;                  q.push(temp);              }              else if(map[temp.x][temp.y]>'1'&&map[temp.x][temp.y]<='9')              {//如果遇到一个钥匙,判断钥匙是否已经有了,并且判断段上一个有木有                   if(!p.flog[map[temp.x][temp.y]-'1']&&temp.flog[map[temp.x][temp.y]-'1'-1])                  {                      temp.key+=a[map[temp.x][temp.y]-'1'];                      temp.flog[map[temp.x][temp.y]-'1']=1;                  }                  vis[temp.x][temp.y][temp.key]=1;                  q.push(temp);              }              else              {                  vis[temp.x][temp.y][temp.key]=1;                  q.push(temp);              }          }      }      printf("impossible\n");  }  int main()  {      while(scanf("%d%d",&n,&m)!=EOF)      {          if(n==0&&m==0) break;          f=0;          for(int i=0;i<m;i++)          f+=a[i];//将钥匙的总数变为二进制数           memset(map,0,sizeof(map));          for(int i=0;i<n;i++)          {              scanf("%s",map[i]);              for(int j=0;j<n;j++)              {                  if(map[i][j]=='K')                  {                      sx=i;                      sy=j;                  }              }          }          bfs();      }      return 0;  }  


0 0
原创粉丝点击