首页 > 代码库 > HDU 1885 BFS+状态压缩
HDU 1885 BFS+状态压缩
Key Task
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1239 Accepted Submission(s): 495
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
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.
Note that it is allowed to have
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
Sample Output
Escape possible in 9 steps. The poor student is trapped! Escape possible in 45 steps.
Source
2008 “Shun Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2)
/************************************************************************* > File Name: 3.cpp > Author:yuan > Mail: > Created Time: 2014年11月26日 星期三 13时09分34秒 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; char mat[105][105]; int ans; struct node { int x,y; int num,key; }; bool vis[105][105][30]; queue<node> q; int sx,sy; int n,m,top,base; int next[4][2]={1,0,0,1,-1,0,0,-1}; void BFS() { int x1,x2,y1,y2;node p,qq; while(!q.empty()){ p=q.front(); x1=p.x;y1=p.y; if(mat[x1][y1]=='X'){ ans=p.num; return; } for(int i=0;i<4;i++) { int key; x2=x1+next[i][0];y2=y1+next[i][1]; if(x2<0||x2>=n||y2<0||y2>=m||mat[x2][y2]=='#') continue; if(mat[x2][y2]=='.'||mat[x2][y2]=='*'||mat[x2][y2]=='X') { key=p.key; if(vis[x2][y2][key]==0) { qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key; q.push(qq); vis[x2][y2][key]=1; } } else if(mat[x2][y2]=='B'||mat[x2][y2]=='Y'||mat[x2][y2]=='R'||mat[x2][y2]=='G') { key=p.key; if(vis[x2][y2][key]==0){ int d; if(mat[x2][y2]=='B') d=0; else if(mat[x2][y2]=='Y') d=1; else if(mat[x2][y2]=='R') d=2; else if(mat[x2][y2]=='G') d=3; int lock=1<<d; if(lock&key){ qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key; q.push(qq); vis[x2][y2][key]=1; } } } else if(mat[x2][y2]=='b'||mat[x2][y2]=='y'||mat[x2][y2]=='r'||mat[x2][y2]=='g') { int d; if(mat[x2][y2]=='b') d=0; else if(mat[x2][y2]=='y') d=1; else if(mat[x2][y2]=='r') d=2; else if(mat[x2][y2]=='g') d=3; key=1<<d; if(vis[x2][y2][p.key]==0){ qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key|key; q.push(qq); vis[x2][y2][p.key]=1; } } } q.pop(); } } int main() { while(1){ scanf("%d%d",&n,&m); if(n==0&&m==0) break; memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); for(int i=0;i<n;i++) scanf("%s",mat[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(mat[i][j]=='*') {sx=i,sy=j;} } node p; p.x=sx;p.y=sy;p.num=0;p.key=0; q.push(p); vis[sx][sy][0]=1; ans=100000000; BFS(); if(ans<100000000) printf("Escape possible in %d steps.\n",ans); else printf("The poor student is trapped!\n"); } return 0; }
HDU 1885 BFS+状态压缩
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。