Maze
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3183   Accepted: 996

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
#include<stdio.h>
#include<iostream>
#include <string.h>
#include <queue>
using namespace std;
typedef struct node
{
int x;
int y;
node(int a, int b)
{
x = a;
y = b;
}
node()
{ }
}Map; char Maze[][];
int Dir[][] = {-,,,,,-,,};
int key[]; void BFS(int sx, int sy, int m, int n)
{
queue<Map> Queue;
Queue.push(Map(sx, sy));
Map temp;
Maze[sx][sy] = 'X';
int Limit = ;
while(!Queue.empty() && Limit < )
{
++Limit;
temp = Queue.front();
Queue.pop();
if (Maze[temp.x][temp.y] >= 'A' && Maze[temp.x][temp.y] <= 'E')
{
if (key[Maze[temp.x][temp.y] - 'A'] == )
{
Maze[temp.x][temp.y] = 'X';
}
else
{
Queue.push(temp);
continue;
}
}
for (int i = ; i < ; i++)
{
int x = temp.x + Dir[i][];
int y = temp.y + Dir[i][];
if (x >= && x < m && y >= && y < n && Maze[x][y] != 'X')
{
if (Maze[x][y] == '.')
{
Maze[x][y] = 'X';
Queue.push(Map(x, y));
}
if (Maze[x][y] >= 'a' && Maze[x][y] <= 'e')
{
key[Maze[x][y] - 'a']--;
Maze[x][y] = 'X';
Queue.push(Map(x, y));
}
if (Maze[x][y] == 'G')
{
printf("YES\n");
return;
}
if (Maze[x][y] >= 'A' && Maze[x][y] <= 'E')
{
Queue.push(Map(x, y));
}
}
}
}
printf("NO\n");
} int main()
{
int m, n;
int sx, sy;
while(scanf("%d%d", &m, &n) != EOF)
{
if (m == && n == )
{
break;
}
memset(key, , sizeof(key));
for (int i = ; i < m; i++)
{
scanf("%s", Maze[i]);
for (int j = ; j < n; j++)
{
if (Maze[i][j] == 'S')
{
sx = i;
sy = j;
}
else
{
if (Maze[i][j] >= 'a' && Maze[i][j] <= 'e')
{
key[Maze[i][j] - 'a']++;
}
}
}
}
BFS(sx, sy, m, n);
}
return ;
}

POJ 2157 Maze的更多相关文章

  1. 搜索 || BFS || POJ 2157 Maze

    走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...

  2. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  3. poj 3026Borg Maze

    http://poj.org/problem?id=3026 #include<cstdio> #include<iostream> #include<cstring&g ...

  4. POJ 2157 How many ways??

    How many ways?? Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origina ...

  5. {POJ}{3897}{Maze Stretching}{二分答案+BFS}

    题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...

  6. poj 3897 Maze Stretching 二分+A*搜索

    题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...

  7. 【bfs】 poj 3984 maze 队列存储

    #include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...

  8. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. 【Web应用-大文件部署】上传超过 2M 的文件到 Azure PHP 网站失败

    问题描述 上传超过 2M 的文件到 Azure PHP 网站失败. 问题分析 由于 PHP 本身默认上传文件的上限是 2M,所以当上传超过2M的文件时会报错. 解决方法 根据以下步骤进行配置: 在 s ...

  2. siege4压测脚本示例

    agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...

  3. asp.net 页面嵌套(非iframe)方法

    前台 <div id="divUrlDetail" runat="server"> </div> 后台 protected void P ...

  4. 如何解决源码安装软件中make时一直重复打印configure信息

    在通过源码安装软件时,会出现执行./configure后再make时总是重复打印configure的信息,无法进入下一阶段的安装. 主要原因是系统当前的时间与实际时间不一致,特别是在虚拟机上经常会出现 ...

  5. php 小坑记录

    1 empty  PHP<=5.5不能用于判断一个表达式的执行结果并且netbeans 和eclipse编辑器识别不出来此错误 含有此用法的 类 和页面将会报错 empty($this-> ...

  6. linux——nmap端口扫描命令

    先安装 nmap :apt-get install nmap 端口扫描命令nmap -sS 172.16.55.100nmap -Pn 172.16.55.100第一组渗透测试指令,用于情报收集. 要 ...

  7. github+hexo+themes搭建简易个性主题博客

    0x00  install Node.js and git 安装Node.js:http://www.runoob.com/nodejs/nodejs-install-setup.html 安装git ...

  8. EOF与feof

    在C语言中,或更精确地说成C标准函数库中表示文件结束符(end of file).在while循环中以EOF作为文件结束标志,这种以EOF作为文件结束标志的文件,必须是文本文件.在文本文件中,数据都是 ...

  9. (16)zabbix history trends历史与趋势数据详解

    1. 保留历史数据 我们可以通过如下方式来设置保留数据的时长:监控项(item)配置里匹配更新监控项(item)设置Housekeeper tasksHousekeeper会定期删除过期的数据.如果数 ...

  10. 关于Linux上的SSH服务无法启动,提示“/var/empty/sshd must be owned by root and not group or world-writable”错误

    首先通过物理终端进入到linux上,手工检查ssh发现没运行# /etc/init.d/sshd statussshd is stopped 手动启动服务,发现报告权限错误.# /etc/init.d ...