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.
思路:参考HDU1429,题目基本一样,除了多加了有多个出口的条件
用二进制位管理钥匙,4把钥匙对应0001,0010,0100,1000,设置三维数组,标记访问状态,利用该数组进行bfs
#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
#define INF 1<<28
static const int MAX = 105;
int R,C;
char maze[MAX][MAX];
int visit[MAX][MAX][16];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct node
{
int x, y;
int key;
int step;
node(int X, int Y, int k, int s):x(X), y(Y), key(k), step(s){}
node(){}
bool operator < (const struct node &p) const
{
return step > p.step;
}
};
long long bfs()
{
int sx, sy;
int ex = -1, ey = -1;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
{
if (maze[i][j] == '*')
{
sx = i; sy = j;
}
else if (maze[i][j] == 'X')
{
ex = i; ey = j;
}
}
if (ex == -1)
return -1;
long long ans = INF;
memset(visit, 0, sizeof(visit));
priority_queue<node> Q;
Q.push(node(sx, sy, 0, 0));
visit[sx][sy][0] = 1;
while (!Q.empty())
{
node tmp = Q.top(); Q.pop();
if (maze[tmp.x][tmp.y] == 'X')
{
return tmp.step;
}
//printf("x=%d,y=%d,key=%d,step=%d\n", tmp.x, tmp.y,tmp.key,tmp.step);
for (int i = 0; i < 4; i++)
{
int tx = tmp.x + dx[i];
int ty = tmp.y + dy[i];
if (tx < 0 || tx >= R || ty < 0 || ty >= C)
continue;
if (maze[tx][ty] == '#')
continue;
if (maze[tx][ty] >= 'A' && maze[tx][ty] <= 'Z')
{
int k;
switch(maze[tx][ty])
{
case 'G':
k = tmp.key & 1;
if (visit[tx][ty][tmp.key] == 0 && k)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
break;
case 'R':
k = tmp.key & 2;
if (visit[tx][ty][tmp.key] == 0 && k)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
break;
case 'Y':
k = tmp.key & 4;
if (visit[tx][ty][tmp.key] == 0 && k)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
break;
case 'B':
k = tmp.key & 8;
if (visit[tx][ty][tmp.key] == 0 && k)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
break;
default:
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
break;
}
}
else if (maze[tx][ty] >= 'a' && maze[tx][ty] <= 'z')
{
int k;
switch(maze[tx][ty])
{
case 'g':
k = tmp.key | 1;
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, k, tmp.step + 1));
}
break;
case 'r':
k = tmp.key | 2;
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, k, tmp.step + 1));
}
break;
case 'y':
k = tmp.key | 4;
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, k, tmp.step + 1));
}
break;
case 'b':
k = tmp.key | 8;
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, k, tmp.step + 1));
}
break;
}
}
else
{
if (visit[tx][ty][tmp.key] == 0)
{
visit[tx][ty][tmp.key] = 1;
Q.push(node(tx, ty, tmp.key, tmp.step + 1));
}
}
}
}
return ans;
}
int main()
{
while (scanf("%d %d", &R, &C) && R && C)
{
for (int i = 0; i < R; i++)
scanf("%s", maze[i]);
long long ans = bfs();
if (ans == -1 || ans == INF)
printf("The poor student is trapped!\n");
else
printf("Escape possible in %lld steps.\n", ans);
}
return 0;
}