poj 2157 Maze(迷宫)(BFS)

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

题解:题目大意是有一个迷宫,迷宫里有最多五种门和对应的钥匙,如果把迷宫中的某种钥匙全部拿到就可以打开对应的门。
这道题数据范围是n,m<=20,所以dfs和bfs都能做,我这里只打了bfs(迷宫问题当然要用bfs╭(╯^╰)╮),思路是,先从起点bfs一遍,如果能到终点就直接输出YES,否则查看拿到的钥匙,把能打开的门全部打开(在地图上标记为可以走的路),然后再跑bfs,直到没有搜到终点并且没有搜到新的钥匙,这时退出循环输出NO
PS:这题我刚开始以为只有五个门,后来才知道是有五种门(我英语不好QAQ),每种门可以有好多个,只要你拿到了这种门的全部钥匙就都可以打开。话说这题我存图好像比bfs函数长QAQ

代码如下

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int door[10][10][1000],key[100][100],num[10],num2[10];
int u[5]={
  0,1,0,-1,0};
int v[5]={
  0,0,1,
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值