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

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

2014 ACM/ICPC Asia Regional Guangzhou Online

题意:

孙悟空要去救唐僧;图中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 }

时间: 2024-12-29 01:43:10

hdu 5025 Saving Tang Monk(bfs+状态压缩)的相关文章

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

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 41    Accepted Submission(s): 10 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi

[ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 941    Accepted Submission(s): 352 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Clas

HDU 5025 Saving Tang Monk(BFS+状压)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5025 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,

hdu 5025 Saving Tang Monk(bfs)

题目链接 题意: 给出n*n的网格,有且只有一个K(孙悟空)和一个T(唐僧),最多有m把钥匙,最多5条蛇,每走一格的时间为1,走到蛇的格子(杀蛇时间为1)的时间为2, 取钥匙要按照顺序来,问能救到唐僧,如果可以输出最短时间. 分析:

HDU 5025 Saving Tang Monk(广州网络赛D题)

HDU 5025 Saving Tang Monk 题目链接 思路:记忆化广搜,vis[x][y][k][s]表示在x, y结点,有k把钥匙了,蛇剩余状态为s的步数,先把图预处理出来,然后进行广搜即可 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; const int N = 105; const int

ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

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 Wujin

HDU 5025 Saving Tang Monk(bfs)

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 Wujin

HDU 5025 Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 565    Accepted Submission(s): 210 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Ch

HDU 5025 Saving Tang Monk【bfs搜索】【北大ACM/ICPC竞赛训练】

bfs的难点在于怎么去表示一个问题的状态[也就是如何去判重] 1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 #include<map> 5 using namespace std; 6 7 struct node{ 8 int r,c; 9 int keys; 10 int kill;//记录当前杀死守卫的状态 11 int d;//时间 12 bool operator <