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. System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.'

    下午在调试的时候报错数据库连接就报错我就很纳闷后面用原来的代码写发现还是报错 System.TypeInitializationException: 'The type initializer for ...

  2. ubuntu中使用eclipse开发android,logcat显示问题

    〜/工作区/ .metadata / .plugins / org.eclipse.core.runtime / .settings / com.android.ide.eclipse.ddms.pr ...

  3. Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库

    Note:这篇文章是基于Android Studio 3.01版本的,NDK是R16. step1:创建一个包含C++的项目 其他默认就可以了. C++ Standard 指定编译库的环境,其中Too ...

  4. Gitlab User Guide

    Installation Configuration Set user name and email Add SSH keys Repository Create New Repository Clo ...

  5. UVA1001 Say Cheese (dijkstra)

    如果没有洞,那么任意两点的最短距离就是直线距离,洞里是瞬间的,所以看成一个点就行了(其实点也可以当作半径为0的洞来处理),洞到洞的最短距离都是圆心距离减去半径.剩下的就是求单源最短路径,是完全图,用不 ...

  6. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  7. Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint

    (Panda, dog and human repeat comparison):与其他动物比较重复序列 我们使用Repbase 库(重复序列库)+已知的转录原件序列+识别软件,评估出转录原件占比,并 ...

  8. javase(10)_多线程基础

    一.排队等待 1.下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件 ...

  9. 个人总结NDIS中NDIS_PACKET,NDIS_BUFFER的关系

    // // NDIS_PACKET结构的定义 // typedef struct _NDIS_PACKET { NDIS_PACKET_PRIVATE Private; //这个其实是一个链表结构,P ...

  10. cookies,sessionStorage和localStorage的相同点和不同点?

    相同点:都存储在客户端. 不同点: 1.存储大小: cookies数据大小不能超过4k sessionStorage和localStorage虽然也有存储大小的限制,但比cookies大得多,可以达到 ...