码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-20 06:47:03      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

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 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
 

 

Source
 

题意:

孙悟空要去救唐僧;图中K是孙悟空,T是唐僧,S是蛇,数字是钥匙;

孙悟空必须拿到所有的钥匙,才能救唐僧,而且钥匙必须有顺序,你没拿到1,就不能拿2.

'#'不能走,走一步要花一个时间,经过S时,要花一个时间打蛇(蛇只要打一次,下次经过就不用打);

问最少的时间;

 

说说在这道题遇到的问题。

首先不知道如何标记,原来要开个四维数组,vis[i][j][k][l],第一维、第二维表示坐标,第三维表示拿到的钥匙数,第四维表示蛇的二进制值,这样就可以标记了。

然后在蛇的设定上,要把蛇存起来,目的是为了后面是要到二进制的移位和判断是否走过

最后写完的时候,运行一直程序崩溃,原来是数组开太大了,直接不能运行。之后交的一直Runtime Error(ACCESS_VIOLATION),检查了好久没检查出来,原来是在一个for(int i=0;i<4;i++)的for循环里面又写了一个for(int i=0;i<numSna;i++),导致了这个问题。感觉直接太弱了。。。

 

技术分享
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<stdlib.h>
  6 #include<algorithm>
  7 #include<queue>
  8 #include<map>
  9 using namespace std;
 10 #define N 106
 11 int n,m;
 12 char mp[N][N];
 13 int vis[N][N][16][36];
 14 struct Node
 15 {
 16     int x,y;
 17     int keyNum;
 18     int t;
 19     int snake;
 20     friend bool operator < (Node a,Node b)
 21     {
 22         return a.t>b.t;
 23     }
 24 }st,ed;
 25 int dirx[]={0,0,-1,1};
 26 int diry[]={-1,1,0,0};
 27 struct Node1
 28 {
 29     int x,y;
 30 }s[10];
 31 int numSna;
 32 void bfs()
 33 {
 34     priority_queue<Node>q;
 35     q.push(st);
 36     vis[st.x][st.y][st.keyNum][st.snake]=1;
 37     Node t1,t2;
 38     while(!q.empty())
 39     {
 40         t1=q.top();
 41         q.pop();
 42         if(t1.x==ed.x && t1.y==ed.y)
 43         {
 44             if(t1.keyNum==m+1)
 45             {
 46                 printf("%d\n",t1.t);
 47                 return;
 48             }
 49         }
 50         
 51         for(int i=0;i<4;i++)
 52         {
 53             t2=t1;
 54             t2.x=t1.x+dirx[i];
 55             t2.y=t1.y+diry[i];
 56             if(vis[t2.x][t2.y][t2.keyNum][t2.snake]) continue;
 57             if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=n) continue;
 58             if(mp[t2.x][t2.y]==#) continue;
 59             if(mp[t2.x][t2.y]>=1 && mp[t2.x][t2.y]<=9)
 60             {
 61                 int tmp=mp[t2.x][t2.y]-0;
 62                 if(t2.keyNum==tmp)
 63                 {
 64                     t2.t++;
 65                     t2.keyNum++;
 66                     vis[t2.x][t2.y][t2.keyNum][t2.snake]=1;
 67                     q.push(t2);
 68                 }
 69                 else 
 70                 {
 71                     t2.t++;
 72                     vis[t2.x][t2.y][t2.keyNum][t2.snake]=1;
 73                     q.push(t2);
 74                 }
 75             }
 76             else if(mp[t2.x][t2.y]==S)
 77             {
 78                 for(int j=0;j<numSna;j++)
 79                 {
 80                     if(s[j].x==t2.x && s[j].y==t2.y)
 81                     {
 82                         if((t2.snake>>j)&1)
 83                         {
 84                             t2.t++;
 85                             vis[t2.x][t2.y][t2.keyNum][t2.snake]=1;
 86                             q.push(t2);
 87                         }
 88                         else 
 89                         {
 90                             t2.t+=2;
 91                             t2.snake=t2.snake+(1<<j);
 92                             vis[t2.x][t2.y][t2.keyNum][t2.snake]=1;
 93                             q.push(t2);
 94                         }
 95                         break;
 96                     }
 97                     
 98                 }
 99             }
100             else 
101             {
102                 t2.t++;
103                 vis[t2.x][t2.y][t2.keyNum][t2.snake]=1;;
104                 q.push(t2);
105             }
106         }
107     }
108     printf("impossible\n");
109 }
110 int main()
111 {
112     while(scanf("%d%d",&n,&m)==2 && n+m )
113     {
114         numSna=0;
115         for(int i=0;i<n;i++)
116         {
117             scanf("%s",mp[i]);
118             for(int j=0;j<n;j++)
119             {
120                 if(mp[i][j]==K)
121                 {
122                     st.x=i;
123                     st.y=j;
124                     st.t=0;
125                     st.keyNum=1;
126                     st.snake=0;
127                 }
128                 if(mp[i][j]==T)
129                 {
130                     ed.x=i;
131                     ed.y=j;
132                 }
133                 if(mp[i][j]==S)
134                 {
135                     s[numSna].x=i;
136                     s[numSna++].y=j;
137                 }
138             }
139         }
140         memset(vis,0,sizeof(vis));
141         bfs();
142     }
143     return 0;
144 }
View Code

 

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

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4743984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!