hdu 1885 Key Task(bfs+状态压缩)
2015-09-09 20:22
330 查看
[align=left]Problem Description[/align]
[align=left]Input[/align]
View Code
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
相关文章推荐
- 进程间通信IPC-消息队列
- Android Fragment 真正解析(上)
- 机试题及答案整理
- MemberCached安装
- socket阻塞与非阻塞,同步与异步、I/O模型
- C++ string string string string string string string string string string
- vs2010创建和使用动态链接库(dll)
- Oracle如何实现跨库查询
- 第二周 项目1-C/C++语言中函数参数传递的三种方式
- 软件设计杂谈<二>
- spring基于java的配置
- GreenDao的使用
- IntentService AsyncTask,Thread
- Vs2013 坑爹的Target framework问题
- IOS NSFileHandle 基本用法
- C++函数调用之thiscall
- 转 :asp教程.net c#数组遍历、排序、删除元素、插入、随机元素 数组遍历
- 总结一下最近整合spring4和hibernate4遇到的问题
- 天眼
- Oracle 学习之 性能优化(十二) 分区表