HDU 1885
来源:互联网 发布:tensorflow 微调模型 编辑:程序博客网 时间:2024/05/16 00:30
Key Task
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1656 Accepted Submission(s): 699
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*........X1 3*#X3 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.
//状态压缩+bfs
#include <iostream>#include <stdio.h>#include <string.h>#include <cmath>#include <map>#include <queue>#include <algorithm>using namespace std;const int maxn=100+10;const int inf=(1<<30);int n,m,start_x,start_y,stop_x,stop_y,dir[4][2]={-1,0,1,0,0,-1,0,1};int vis[maxn][maxn][33],mark[maxn][maxn];char ma[maxn][maxn];struct Node{ int x,y,step,key;};int bfs(int x,int y){ queue <Node> q; q.push(Node{x,y,0,0}); vis[x][y][0]=1; while(!q.empty()) { Node a=q.front(); q.pop(); if(ma[a.x][a.y]=='X')return a.step; for(int i=0;i<4;i++) { Node b=a; b.x+=dir[i][0]; b.y+=dir[i][1]; b.step++; if(b.x<0||b.x>=n||b.y<0||b.y>=m||vis[b.x][b.y][b.key]||ma[b.x][b.y]=='#')continue; if(ma[b.x][b.y]>'a'&&ma[b.x][b.y]<'z') { b.key=b.key|(1<<mark[b.x][b.y]); if(!vis[b.x][b.y][b.key]) //注意标记 { vis[b.x][b.y][b.key]=1; q.push(b); } } else if(ma[b.x][b.y]>'A'&&ma[b.x][b.y]<'Z'&&ma[b.x][b.y]!='X') { if(b.key&(1<<mark[b.x][b.y])) { vis[b.x][b.y][b.key]=1; q.push(b); } } else { vis[b.x][b.y][b.key]=1; q.push(b); } } } return -1;}int main(){ while(~scanf("%d%d",&n,&m)&&(n+m)) { memset(mark,0,sizeof(mark)); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { scanf("%s",ma[i]); for(int k=0;k<m;k++) { if(ma[i][k]=='*') {start_x=i; start_y=k;} if(ma[i][k]=='X') {stop_x=i; stop_y=k;} if(ma[i][k]=='b'||ma[i][k]=='B') mark[i][k]=0; if(ma[i][k]=='y'||ma[i][k]=='Y') mark[i][k]=1; if(ma[i][k]=='r'||ma[i][k]=='R') mark[i][k]=2; if(ma[i][k]=='g'||ma[i][k]=='G') mark[i][k]=3; } } int res=bfs(start_x,start_y); if(res==-1) printf("The poor student is trapped!\n"); else { printf("Escape possible in %d steps.\n",res); } } return 0;}
0 0
- hdu 1885
- HDU 1885
- HDU 1885
- HDU-1885
- HDU-1885-Key Task
- HDU 1885 Key Task
- hdu 1885 Key Task
- HDU 1885 Key Task
- hdu 1885 Key Task
- HDU-1885 Key Task
- hdu 1885 C
- HDU 1885 Key Task
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- Redis学习6之redis多节点集群搭建和使用
- poj3274数位HASH
- 构建Hadoop metric
- Linux下Apache服务器搭建
- uva 11098 - Battle II(强联通)
- HDU 1885
- iOS 之NSJSONReadingOptions说明
- Leet Code 14 Longest Common Prefix
- 二分查找
- linux下为php开启oci8扩展
- HDU2007平方和与立方和
- iOS8下UILabel如何自适应高度(自动换行)
- 软考必备
- LVS 归纳