poj2157&&OpenJudge1159 Maze
来源:互联网 发布:怎么样学数据库 编辑:程序博客网 时间:2024/06/03 09:13
问题描述
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
题目简介
有一个迷宫,其中有多扇门(最多五扇)和多个钥匙,一扇门可能有多个钥匙,必须找到所有对应钥匙才能打开该门,已知起点和终点,问是否能从起点达到终点。
//时限2s真的没必要,0s解决
坑点
①多组数据
②多把钥匙
③多扇门
④有的门可能没有给出钥匙
思路
//这道题花了我大约三天时间。题目很简洁,思路也很清晰,就是不好实现。
①初始化(我因为没有每次将结构体数组清空而WA了n次)
②读入数据并预处理
③dfs,到达每一个点都要将四周未开启的门和所有钥匙找出来,将钥匙在地图中删除,并尝试开启对应的门,若开启则直接跳到新开的门处继续搜索。
代码实现
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int m,n,i,j,x,y,x1,y1;bool OK;char c[21][21];bool b[21][21];struct aa{ int num,x,y;}a[6],t;bool found[6];//true:a[i]为能达到的暂不能探索的门 void getempty(){ for(int i=1;i<=5;i++) a[i]=t; memset(b,0,sizeof(b)); memset(c,'\0',sizeof(c)); memset(found,0,sizeof(found));}void init(int x,int y){ if(x-1>0&&c[x-1][y]>='A'&&c[x-1][y]<='E') found[c[x-1][y]-'A'+1]=1; if(x+1<=m&&c[x+1][y]>='A'&&c[x+1][y]<='E') found[c[x+1][y]-'A'+1]=1; if(y-1>0&&c[x][y-1]>='A'&&c[x][y-1]<='E') found[c[x][y-1]-'A'+1]=1; if(y+1<=n&&c[x][y+1]>='A'&&c[x][y+1]<='E') found[c[x][y+1]-'A'+1]=1; if(x-1>0&&c[x-1][y]>='a'&&c[x-1][y]<='e') a[c[x-1][y]-'a'+1].num--,c[x-1][y]='.'; if(x+1<=m&&c[x+1][y]>='a'&&c[x+1][y]<='e') a[c[x+1][y]-'a'+1].num--,c[x+1][y]='.'; if(y-1>0&&c[x][y-1]>='a'&&c[x][y-1]<='e') a[c[x][y-1]-'a'+1].num--,c[x][y-1]='.'; if(y+1<=n&&c[x][y+1]>='a'&&c[x][y+1]<='e') a[c[x][y+1]-'a'+1].num--,c[x][y+1]='.';}void dfs(int x,int y){ init(x,y); if(x==x1&&y==y1) { OK=1; return; } if(c[x][y]>='A'&&c[x][y]<='E'&&a[c[x][y]-'A'+1].num!=0) return; if(!OK) { for(int i=1;i<=5;i++) if(a[i].num==0&&c[a[i].x][a[i].y]!='.') c[a[i].x][a[i].y]='.',b[a[i].x][a[i].y]=1; for(int i=1;i<=5;i++) if(a[i].num==0&&found[i]) {found[i]=0,dfs(a[i].x,a[i].y);if(OK) return;} if(OK) return; b[x][y]=0; if(x-1>0&&b[x-1][y]) { dfs(x-1,y); if(OK) return; } if(x+1<=m&&b[x+1][y]) { dfs(x+1,y); if(OK) return; } if(y-1>0&&b[x][y-1]) { dfs(x,y-1); if(OK) return; } if(y+1<=n&&b[x][y+1]) { dfs(x,y+1); if(OK) return; } }}int main(){ while(1) { cin>>m>>n; if(m==0&&n==0) break; getempty(); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { cin>>c[i][j]; if(c[i][j]=='S') x=i,y=j; else if(c[i][j]=='G') x1=i,y1=j; else if(c[i][j]>='A'&&c[i][j]<='E') a[c[i][j]-'A'+1].x=i,a[c[i][j]-'A'+1].y=j; else if(c[i][j]>='a'&&c[i][j]<='e') a[c[i][j]-'a'+1].num++; if(c[i][j]!='X'&&!(c[i][j]>='A'&&c[i][j]<='E')) b[i][j]=1; } } OK=0; for(i=1;i<=5;i++) if(a[i].num==0) a[i].num=1e9; dfs(x,y); if(OK) printf("YES\n"); else printf("NO\n"); } return 0;}
- poj2157&&OpenJudge1159 Maze
- 【poj2157】Maze
- 【POJ2157】Maze
- poj2157 BFS
- poj2157 深搜 基础
- Maze
- maze
- Maze
- maze
- Maze
- maze
- Maze
- Maze
- Maze
- Maze Traversa
- hdu4035.Maze
- POJ1111(Maze)
- Borg Maze
- github 入门较好的 参考资料
- 10 简化几何计算
- What is Operating System
- HDU2087
- POJ 3616 (dp)
- poj2157&&OpenJudge1159 Maze
- 【洛谷 P2002】消息扩散 tarjan
- 远程打码平台是如何进行验证码的识别的?
- linux下查看telnet服务器和域名系统的端口号
- 如何搭建个人网站(二)项目发布
- opengl三维观察
- 实验十 网络图片查看器
- hdu 5563 five-pointed star(几何)
- bzoj 1370: [Baltic2003]Gang团伙 并查集