您的位置:首页 > 其它

hdu 1885 Key Task(bfs+状态压缩)

2015-09-09 20:22 330 查看
[align=left]Problem Description[/align]

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.


[align=left]Input[/align]

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:


#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][22];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int n,m;
Node st;
char Door[4]={'B','Y','R','G'};
char Key[4]={'b','y','r','g'};

void bfs(){
memset(mark,false,sizeof(mark));
queue<Node>Q;
Node p,q;
st.key=st.step=0;
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=0;i<4;i++){
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step+1;
q.key=p.key;
if(mark[q.x][q.y][q.key]) continue;
if(q.x<1||q.x>n||q.y<1||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=0;j<4;j++){
if(map[q.x][q.y]==Door[j]){
if(q.key&(1<<j)){

mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
}else if(islower(map[q.x][q.y])){
for(int j=0;j<4;j++){
if(map[q.x][q.y]==Key[j]){
if((q.key&(1<<j))==0){
q.key+=(1<<j);
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
else{
if(mark[q.x][q.y][q.key]==0){
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=1;i<=n;i++){
scanf("%s",map[i]+1);
for(int j=1;j<=m;j++){
if(map[i][j]=='*')st.x=i,st.y=j;
}
}
bfs();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: