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.
题意:走迷宫,问走出迷宫最少需要多少步,最多有四个门BYRG,对应byrg四个钥匙,其中要想通过一个含有门的格子,需要先经过含有该门对应的钥匙的地方(比如通过B必须走到b的格子拿到钥匙),注意有多个门,还有1步的时候也是steps,这两点卡了我好久。。
思路:
1 刚开始做这个题目的时候先想到不能走重复的路,但不走重复的路拿到钥匙就无法正常走了;
2 我们可以开一个三维mark数组来标记拿到钥匙的状态,我们就得用状态压缩(即二进制)来节省时间,0001代表拿到一个钥匙,当遇到门时,我们用 || 来比较是否拿到钥匙,遇到钥匙时用 && 来标记拿到钥匙;
代码:
#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int x,y,t,status;
}z[1000]; //多个门;
int vis[4][2]={0,1,0,-1,1,0,-1,0};
char map[200][200];
int mark[1<<5][200][200];//第一维用来记录状态
int n,m,dx,dy,s;
int ok(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#')
return 1;
else
return 0;
}
int find2(char c)
{
if(c=='b')
return 1;
else if(c=='y')
return 2;
else if(c=='r')
return 3;
else
return 0;
}
int find1(char c)
{
if(c=='B')
return 1;
else if(c=='Y')
return 2;
else if(c=='R')
return 3;
else
return 0;
}
int bfs()
{
node q,p;
q.x=dx,q.y=dy,q.t=0,q.status=0;
queue<node>que;
que.push(q);
while(que.size())
{
q=que.front();
que.pop();
for(int j=0;j<s;j++)
if(q.x==z[j].x&&q.y==z[j].y)
return q.t;
for(int i=0;i<=3;i++)
{
p.x=q.x+vis[i][0];
p.y=q.y+vis[i][1];
if(ok(p.x,p.y)&&!mark[q.status][p.x][p.y])
{
int temp;
if(map[p.x][p.y]=='b'||map[p.x][p.y]=='y'||map[p.x][p.y]=='r'||map[p.x][p.y]=='g')
{
temp=find2(map[p.x][p.y]);
p.status=q.status|(1<<temp);
}
else if(map[p.x][p.y]=='B'||map[p.x][p.y]=='Y'||map[p.x][p.y]=='R'||map[p.x][p.y]=='G')
{
temp=find1(map[p.x][p.y]);
if(q.status&(1<<temp))
{
p.status=q.status;
}
else
{
p.status=q.status;
mark[p.status][p.x][p.y]=1;
}
}
else
{
p.status=q.status;
}
if(!mark[p.status][p.x][p.y])
{
p.t=q.t+1;
mark[p.status][p.x][p.y]=1;
que.push(p);
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
for(int i=0;i<n;i++)
scanf("%s",map[i]);
s=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[i][j]=='*')
dx=i,dy=j;
else if(map[i][j]=='X')
{
z[s].x=i,z[s++].y=j;
}
}
memset(mark,0,sizeof(mark));
mark[0][dx][dy]=1;
int tt=bfs();
if(tt==-1)
printf("The poor student is trapped!\n");
else
printf("Escape possible in %d steps.\n",tt);
}
}