Problem Description
The Czech Technical University is rather old — you already know that it celebrates years of its existence in . 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 -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 ( ≤ R, C ≤ ) 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
*........X *#X ####################
#XY.gBr.*.Rb.G.GG.y#
####################
Sample Output
Escape possible in steps.
The poor student is trapped!
Escape possible in steps.
Source
我的标准写法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define MAXN 110
struct Node{
int x,y,step;
int key;
};
char map[MAXN][MAXN];
bool mark[MAXN][MAXN][];
int dir[][]={{-,},{,},{,-},{,}};
int n,m;
Node st;
char Door[]={'B','Y','R','G'};
char Key[]={'b','y','r','g'}; void bfs(){
memset(mark,false,sizeof(mark));
queue<Node>Q;
Node p,q;
st.key=st.step=;
mark[st.x][st.y][st.key]=true;
Q.push(st);
while(!Q.empty()){
p=Q.front();
Q.pop();
if(map[p.x][p.y]=='X'){
printf("Escape possible in %d steps.\n",p.step);
return ;
}
for(int i=;i<;i++){
q.x=p.x+dir[i][];
q.y=p.y+dir[i][];
q.step=p.step+;
q.key=p.key;
if(mark[q.x][q.y][q.key]) continue;
if(q.x<||q.x>n||q.y<||q.y>m||map[q.x][q.y]=='#')
continue;
if(isupper(map[q.x][q.y])&&map[q.x][q.y]!='X'){
for(int j=;j<;j++){
if(map[q.x][q.y]==Door[j]){
if(q.key&(<<j)){ mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
}else if(islower(map[q.x][q.y])){
for(int j=;j<;j++){
if(map[q.x][q.y]==Key[j]){
if((q.key&(<<j))==){
q.key+=(<<j);
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
else{
if(mark[q.x][q.y][q.key]==){
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
}
}else {
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
puts("The poor student is trapped!");
} int main(){
while(scanf("%d%d",&n,&m),(n+m)){
for(int i=;i<=n;i++){
scanf("%s",map[i]+);
for(int j=;j<=m;j++){
if(map[i][j]=='*')st.x=i,st.y=j;
}
}
bfs();
}
return ;
}