Key Task
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 907 Accepted Submission(s): 334
Total Submission(s): 907 Accepted Submission(s): 334
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.
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
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.
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
*........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.
有4把钥匙和4种门,将状态压缩后有2^4=16种状态,开个vis[maxn][maxn][16]的标记数组表示当前钥匙的状态;
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct Node
{
int x;
int y;
int step;
int key;
};
const int maxn=105;
bool vis[maxn][maxn][16];//4把钥匙,16种状态;
char map[maxn][maxn];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int sx,sy,ex,ey;
int n,m,ans;
bool OK(Node cur)
{
if(cur.x<=0||cur.x>n||cur.y<=0||cur.y>m)
return false;
return true;
}
void Get_Position()
{
int i,j;
for(i=1;i<=n;i++)
{
scanf("%s",map[i]);
for(j=m;j>=1;j--)
{
map[i][j]=map[i][j-1];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(map[i][j]=='*')
{
sx=i;
sy=j;
map[i][j]=='.';
}
// else if(map[i][j]=='X')
// {
// ex=i;
// ey=j;
// map[i][j]=='.';
// }//因为不是单一出口,所以不能用这个标记;
}
}
}
int GetNum(char c)
{
switch(c)
{
case 'B':case 'b':return 0;
case 'Y':case 'y':return 1;
case 'R':case 'r':return 2;
case 'G':case 'g':return 3;
}
}
int BFS()
{
queue<Node>q;
Node A,B,cur;
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
A.x=sx;
A.y=sy;
A.step=0;
A.key=0;
vis[A.x][A.y][A.key]=1;
q.push(A);
while(!q.empty())
{
cur=q.front();
q.pop();
if(map[cur.x][cur.y]=='X')
return cur.step;
for(int i=0;i<4;i++)
{
B.x=cur.x+dx[i];
B.y=cur.y+dy[i];
B.key=cur.key;
if(OK(B)&&map[B.x][B.y]!='#')
{
if(map[B.x][B.y]>='a'&&map[B.x][B.y]<='z')
{
int key =(1<<GetNum(map[B.x][B.y]));//左移一位,因为0000的意思应该是0001;
B.key|=key;//拾取钥匙;
if(!vis[B.x][B.y][B.key])
{
B.step=cur.step+1;
vis[B.x][B.y][B.key]=1;
q.push(B);
}
}
else if(map[B.x][B.y]>='A'&&map[B.x][B.y]<='Z'&&map[B.x][B.y]!='X')
{
int key=GetNum(map[B.x][B.y]);
if (((B.key>>key)&1)&&!vis[B.x][B.y][B.key])
{//右移key位后与1,判断是不是存在打开当前门的钥匙
B.step=cur.step+1;
vis[B.x][B.y][B.key]=1;
q.push(B);
}
}
else if(!vis[B.x][B.y][B.key])
{
B.step=cur.step+1;
vis[B.x][B.y][B.key]=1;
q.push(B);
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%*c",&n,&m)&&(n+m))
{
Get_Position();
ans=BFS();
if(ans==-1)
printf("The poor student is trapped!\n");
else
printf("Escape possible in %d steps.\n",ans);
}
return 0;
}
这个问题千万注意不是单一出口,否则WA到死,我第一次就是因为没读好题目才各种WA。。。。