Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1995    Accepted Submission(s): 871

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

```

Solution

胜利大逃亡(续)非常相似。

1.出口可以有0或者更多；2.门有4种，钥匙有四种，钥匙可以多次开门，有的地图有门没相应的钥匙，或者有钥匙没有相应门。用最少的步数逃出迷宫。

一样用二进制状态压缩，钥匙最多4把，所以状态数组的第三维开1<<4（1<<5保险点，不过二进制1111转换十进制是15,1<<4是16，已经够了）。&按位与开门，|按位或捡钥匙。

```#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <list>
#include <stack>
#include <string>
#include <vector>
#include <cstring>
#include <cctype>
#include <queue>
#include <set>
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
char mp[101][101];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
bool vis[101][101][1<<5];//x，y，钥匙
int n,m,ec;
struct node
{
void ini()
{
x=0,y=0,step=0,key=0;
}
int x,y;
int step;
int key;
} t,tt;
bool check(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)return false;
if(mp[x][y]=='#')return false;
return true;
}
int bfs()
{
queue<node> q;
ms(vis);
q.push(t);
vis[t.x][t.y][t.key]=1;
while(!q.empty())
{
t=q.front(),q.pop();
if(mp[t.x][t.y]=='^')
return t.step;//走到出口
for(int i=0; i<4; i++)
{
tt.x=t.x+dir[i][0],tt.y=t.y+dir[i][1];
if(check(tt.x,tt.y))
{
tt.key=t.key;
tt.step=t.step+1;
if(isupper(mp[tt.x][tt.y]))//门
{
int nk=tt.key&1<<(mp[tt.x][tt.y]-'A');
if(nk&&!vis[tt.x][tt.y][tt.key])
{
vis[tt.x][tt.y][tt.key]=true;
q.push(tt);
}
}
else if(islower(mp[tt.x][tt.y]))//钥匙
{
tt.key=tt.key|1<<(mp[tt.x][tt.y]-'a');
if(!vis[tt.x][tt.y][tt.key])
{
vis[tt.x][tt.y][tt.key]=true;
q.push(tt);
}
}
else
{
if(!vis[tt.x][tt.y][tt.key])
{
vis[tt.x][tt.y][tt.key]=1;
q.push(tt);
}
}
}
}
}
return -1;
}
int main()
{
while(cin>>n>>m&&(n||m))
{
ec=0,t.ini();
for(int i=0; i<n; i++)cin>>mp[i];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(mp[i][j]=='*')t.x=i,t.y=j;//记录初始位置
else if(mp[i][j]=='B')mp[i][j]='A';//将4种门和钥匙替换为ABCD
else if(mp[i][j]=='b')mp[i][j]='a';
else if(mp[i][j]=='Y')mp[i][j]='B';
else if(mp[i][j]=='y')mp[i][j]='b';
else if(mp[i][j]=='R')mp[i][j]='C';
else if(mp[i][j]=='r')mp[i][j]='c';
else if(mp[i][j]=='G')mp[i][j]='D';
else if(mp[i][j]=='g')mp[i][j]='d';
else if(mp[i][j]=='X')mp[i][j]='^',ec++;//出口改为^，防止和门冲突
}
}
int ans;
if(!ec)printf("The poor student is trapped!\n");
else
{
ans=bfs();
if(ans==-1)printf("The poor student is trapped!\n");
else printf("Escape possible in %d steps.\n",ans);
}
}
return 0;
}
```

欢迎转载，请保留出处与链接。Ocrosoft » HDU 1885 Key Task(BFS+状态压缩)