poj2157Maze(bfs,有门,需要先找钥匙)
来源:互联网 发布:淘宝海蓝之谜小样真假 编辑:程序博客网 时间:2024/05/16 02:04
Maze
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3245 Accepted: 1022
Description
Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by 'A', 'B', 'C', 'D', 'E' respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door's keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that's three 'a's which denote the keys of 'A' in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.
Input
The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: 'X' (a block of wall, which the explorer cannot enter), '.' (an empty block), 'S' (the start point of Acm), 'G' (the position of treasure), 'A', 'B', 'C', 'D', 'E' (the doors), 'a', 'b', 'c', 'd', 'e' (the keys of the doors). The input is terminated with two 0's. This test case should not be processed.
Output
For each test case, in one line output "YES" if Acm can find the treasure, or "NO" otherwise.
Sample Input
4 4 S.X. a.X. ..XG .... 3 4 S.Xa .aXB b.AG 0 0
Sample Output
YES NO
Source
POJ Monthly,Wang Yijie
dfs,若找到G,标记之,循环bfs,根据跳出条件执行,钥匙增加就循环,不增加就跳出。
再根据标记,输出结果。dfs,bfs应该都行吧。
#include <iostream>#include <cstring>#include <cctype>using namespace std;bool vis[20][20];int dir[4][2]={-1,0,1,0,0,-1,0,1};char mat[25][25];int r,c,sx,sy,ex,ey,flag,ok,p;int totalKey[5], haveKey[5];void dfs(int x, int y){ if(x==ex && y==ey) ok=1; if(mat[x][y]=='X' || vis[x][y]) return; if(islower(mat[x][y])) {haveKey[mat[x][y]-'a']++, mat[x][y]='.',flag++;} if(mat[x][y]=='A' || mat[x][y]=='B' || mat[x][y]=='C' || mat[x][y]=='D' || mat[x][y]=='E'){ if(haveKey[mat[x][y]-'A']==totalKey[mat[x][y]-'A'] && haveKey[mat[x][y]-'A']!=0) mat[x][y]='.'; //&& 写成 || 找了半天错 else return; } vis[x][y] = true; for(int i=0; i<4; i++){ int tx = x + dir[i][0]; int ty = y + dir[i][1]; if(tx<0 || tx>r-1 || ty<0 || ty>c-1) continue; dfs(tx,ty); }}int main(){ while(cin>>r>>c &&r+c){ flag=ok=0; memset(vis,false,sizeof(vis)); memset(mat,'\0',sizeof(mat)); memset(haveKey,0,sizeof(haveKey)); memset(totalKey,0,sizeof(totalKey)); for(int i=0; i<r; i++){ for(int j=0; j<c; j++){ cin>>mat[i][j]; if(islower(mat[i][j])) totalKey[mat[i][j]-'a']++; if(mat[i][j]=='S') sx = i, sy = j, mat[i][j]='.'; if(mat[i][j]=='G') ex = i, ey = j; //一开始ey写成sy,找了半天错 } } dfs(sx,sy);p=flag; while(true){ memset(vis,false,sizeof(vis)); dfs(sx,sy); if(flag==p) break; else p=flag; } // int m = 405; //棋盘是20*20的,所以最多搜索400次,其实肯定不可能这么多,懒得推证了// while(m--){// dfs(sx,sy);// memset(vis,false,sizeof(vis));// } if(ok) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
0 0
- poj2157Maze(bfs,有门,需要先找钥匙)
- 【有问题?先找手册!】php有哪些错误级别
- hdu1104 Remainder bfs找算式是否有解……
- Hone蓝牙钥匙扣 iPhone轻松找钥匙
- 不要在有光的地方找钥匙
- 找朋友 BFS
- 找朋友(bfs)
- 找朋友&& BFS
- 找朋友(bfs)
- bfs 找点点
- 找朋友(bfs)
- 在小站上加了个最土的留言板,先就这样用用吧,做好的还需要找点资料
- 钥匙
- 先有Function 还是先有 Object
- 先有春天还是先有冬天??
- 先有Class还是先有Object?
- 水灾 (BFS-先洪水后寻路)
- 在网上找了一些j2ee的视频教程,有需要的朋友可以看看
- JDBC Driver For SQL2000/2005/2008
- 第4天:散列HASH(一)
- 奋斗不止 自强不息:职场话题之跳槽(二)—如何准备跳槽
- C++操作系统面试题
- Struts2 学习记录(2)
- poj2157Maze(bfs,有门,需要先找钥匙)
- Oracle 字符函数
- ASP.NET WebApi 实现多附件上传
- 字符串型字段的Frequency histogram解读
- C++
- books.xml
- Mouter 1.0.0
- ABAP 小知识01
- 我的浏览器收藏夹分类