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. python搭建ftp服务器

    1 # coding: utf-8 import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handler ...

  2. Java生成-zipf分布的数据集(自定义倾斜度,用作spark data skew测试)

    1.代码 import java.io.Serializable; import java.util.NavigableMap; import java.util.Random; import jav ...

  3. Spring MVC系列[2]——参数传递及重定向

    1.目录结构 2.代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  4. NBUT 1119 Patchouli's Books (STL应用)

    题意: 输入一个序列,每个数字小于16,序列元素个数小于9. 要求将这个序列所有可能出现的顺序输出,而且要字典序. 思路: 先排序,输出该升序序列,再用next_permutation进行转变即可,它 ...

  5. VS2010中C++ 出现fatal error LNK1169: 找到一个或多个多重定义的符号

    一般是函数重定义造成的 例如定义了两个 sum(x,y)函数

  6. java多线程---ReentrantLock源码分析

    ReentrantLock源码分析 基础知识复习 synchronized和lock的区别 synchronized是非公平锁,无法保证线程按照申请锁的顺序获得锁,而Lock锁提供了可选参数,可以配置 ...

  7. Django auth权限

    创建超级管理员命令 python manage.py createsuperuser --username hello 检查和校验用户 from django.contrib import auth ...

  8. 【Git版本控制】Git使用教程

    1.Git的综述 SVN是集中式版本控制系统,版本库集中放在中央服务器上,而干活时用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器.集 ...

  9. CentOS6、7安装MySQL5.7全教程

    CentOS6.7安装MySQL5.7全教程 做开发总得用到数据吧,Linux作为服务器,总得有一个数据库来存储测试用的数据,所以呢,这里附上CentOS6.7安装MySQL5.7的教程喔~ 用到的工 ...

  10. Linux用户身份(命令详解与补正)

    基于Red Hat Enterprise Linux 7.5 Linux中的root就是存在于所有类UNIX系统中的超级用户,持有最高管理权限,能添加/删除用户.开关机.关闭或开启硬件或者系统服务等, ...