CC

自然万物都趋向从有序变得无序

0%

Key Task HDU - 1885

题目链接:点我

题目


    The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places. 
    The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game. 

    The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color. 

    You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

Input

    The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following: 

这里写图片描述

Note that it is allowed to have 
*more than one exit,

*no exit at all,

*more doors and/or keys of the same color, and

*keys without corresponding doors and vice versa.


    You may assume that the marker of your position (“*”) will appear exactly once in every map. 

    There is one blank line after each map. The input is terminated by two zeros in place of the map size.

Output

    For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead. 

    One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

Sample Input

1 10
*........X

1 3
*#X

3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################

0 0

Sample Output

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

题意:

大写字母代表门,小写字母代表钥匙,每次遇到大写字母必须有相应的小写字母才能穿过大写字母,问是否能从起点走到终点.

思路:

状态压缩 + bfs,开一个数组记录钥匙的状态即可.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;

int r,c;
int w[102][102];
int sx,sy;
struct ss
{
int x,y,step;
int key;
};
char k[5] = {'b', 'y', 'r', 'g'};
char d[5] = {'B', 'Y', 'R', 'G'};
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool vis[110][110][18];

bool judge(int x,int y)
{
if(x>0&&y>0&&x<=r&&y<=c)
return true;
return false;
}

void bfs()
{
memset(vis,false,sizeof(vis));
ss q[1000000+10];
int kk;
int tail=0;
int head=0;
q[tail].x=sx;
q[tail].y=sy;
q[tail].key=0;
q[tail++].step=0;
vis[sx][sy][0]=true;
while(head<tail){
ss p=q[head++];
if(w[p.x][p.y]=='X'){
printf("Escape possible in %d steps.\n",p.step);
return ;
}
for(int i=0;i<4;++i){
int x=p.x+dx[i];
int y=p.y+dy[i];
if(judge(x,y)&&w[x][y]!='#'){

if(w[x][y]=='.')
kk=p.key;
int j;
if(w[x][y]>='a'&&w[x][y]<='z'){
for( j=0;j<4;j++)
if(w[x][y]==k[j])
kk=(p.key|(1<<j));
}
if(w[x][y]>='A'&&w[x][y]<='Z'){
for( j=0;j<4;++j)
if(d[j]==w[x][y]){
if((p.key&(1<<j)))
kk=p.key;
else break;
}
if(j<4)
continue;
}
if(vis[x][y][kk]==false){
vis[x][y][kk]=true;
q[tail].x=x;
q[tail].y=y;
q[tail].key=kk;
q[tail++].step=p.step+1;
}
}
}
}
printf("The poor student is trapped!\n");
}

int main()
{
while(scanf("%d %d",&r,&c)!=EOF&&(r||c)){
for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j){
scanf(" %c",&w[i][j]);
if(w[i][j]=='*'){
sx=i,sy=j;
w[i][j]='.';
}
}
bfs();
}
return 0;
}