1159:Maze

总时间限制:

2000ms

内存限制:

65536kB

描述

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.

输入

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.

输出

For each test case, in one line output "YES" if Acm can find the treasure, or "NO" otherwise.

样例输入

4 4

S.X.

a.X.

..XG

....

3 4

S.Xa

.aXB

b.AG

0 0

样例输出

YES

NO

来源

POJ Monthly,Wang Yijie

【思路】

BFS。

题意:

不断循环BFS,在每次BFS中:统计可以得到的钥匙,对于可以访问到的门检查是否钥匙齐全,如果是则入队继续BFS。直到到达G或者没有可以进入的门则结束循环。

【代码】

 #include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxn = +;
const int dx[]={-,,,};
const int dy[]={,,,-};
struct Node{
int x,y;
};
struct door{
int num,sum;
}door[maxn]; char G[maxn][maxn];
int n,m,ex,ey;
queue<Node> q; int vis[maxn][maxn],vis_door[maxn];
bool inside(int x,int y) {
return x>= && x<=n && y>= && y<=m;
}
void BFS() {
bool f=true;
while(f)
{
while(!q.empty()) {
Node u=q.front(); q.pop();
int x=u.x,y=u.y;
if(x==ex && y==ey) {
printf("YES\n");
return ;
}
FOR(i,,) {
int xx=x+dx[i],yy=y+dy[i];
if(inside(xx,yy) && G[xx][yy]!='X') {
if(vis[xx][yy]) continue;
if(G[xx][yy]>='A'&&G[xx][yy]<='E') {
vis_door[G[xx][yy]-'A']=;
continue;
}
if(G[xx][yy]>='a'&&G[xx][yy]<='e') {
door[G[xx][yy]-'a'].sum++;
}
vis[xx][yy]=;
q.push((Node){xx,yy});
}
}
}
f=false;
FOR(i,,n) FOR(j,,m)
if(G[i][j]>='A' && G[i][j]<='E' && !vis[i][j])
{
int c=G[i][j]-'A';
if(vis_door[c]&& door[c].num> && (door[c].num==door[c].sum))
{
vis[i][j]=;
q.push((Node){i,j});
f=true;
}
}
}
printf("NO\n");
} void init() {
while(!q.empty()) q.pop();
memset(vis,,sizeof(vis));
memset(vis_door,,sizeof(vis_door));
for(int i=;i<;i++) door[i].num=door[i].sum=;
}
void read() {
FOR(i,,n) FOR(j,,m)
{
cin>>G[i][j];
if(G[i][j]=='S')
{
vis[i][j]=;
q.push((Node){i,j});
}
if(G[i][j]=='G') ex=i,ey=j;
if(G[i][j]>='a' && G[i][j]<='e') //统计钥匙数目
door[G[i][j]-'a'].num++;
}
} int main() {
while(cin>>n>>m && (n&&m))
{
init();
read();
BFS();
}
return ;
}

NOI题库1159 Maze的更多相关文章

  1. NOI题库刷题日志 (贪心篇题解)

    这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制:  1000ms  内存限制:  65536kB 描述 在一个平面上,如果有两个点( ...

  2. NOI题库 1768最大子矩阵 题解

    NOI题库 1768最大子矩阵  题解     总时间限制: 1000ms 内存限制: 65536kB   描述   已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大 ...

  3. NOI题库 09:图像旋转翻转变换

    NOI题库开始的题,也是略水,当然也是大水,所以彼此彼此 09:图像旋转翻转变换 总时间限制: 1000ms 内存限制: 65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作 ...

  4. NOI题库-小学奥赛QwQ

    今天Loli教育我们让我们来看看NOI题库的奥赛部分,不过,为何是小学的( ⊙ o ⊙ )啊!感觉智商被各种侮辱. 余数相同问题: 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为 ...

  5. noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35

    T31 字符串P型编码 描述 给定一个完全由数字字符('0','1','2',-,'9')构成的字符串str,请写出str的p型编码串.例如:字符串122344111可被描述为"1个1.2个 ...

  6. NOI题库192 生日蛋糕

    192:生日蛋糕 总时间限制: 5000ms 内存限制: 65536kB 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i ...

  7. NOI 题库 9272 题解

    9272   偶数个数字3 描述 在所有的N位数中,有多少个数中有偶数个数字3? 输入 一行给出数字N,N<=1000 输出 如题 样例输入 2 样例输出 73 Solution : 令f ( ...

  8. NOI 题库 7084

    7084  迷宫问题 描述 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, ...

  9. noi题库(noi.openjudge.cn) 1.5编程基础之循环控制T36——T45

    T36 计算多项式的值 描述 假定多项式的形式为xn+xn-1+-+x2+x+1,请计算给定单精度浮点数x和正整数n值的情况下这个多项式的值. 输入 输入仅一行,包括x和n,用单个空格隔开.x在flo ...

随机推荐

  1. (转)ecshop产品详情页显示不清晰

    详情页面的商品图片的设置方法 后台商店设置-显示设置-显示设置(就是这里,商品图片宽度和高度设置的大点就行了,放大镜效果也清晰了) 按照您详情页面图片的实际显示大小来添写. 商品管理-图片批量处理,这 ...

  2. HBuilder使用感受

    最近公司在考虑搞HTML5和后台交互的架构,我于是便下载了HBuilder使用,这里分享下我偶的使用感受. 一.首先,下载下来是一个压缩包,解压后是可以直接使用的,这让我对它的第一感觉很好.不用安装, ...

  3. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  4. NRPE: Unable to read output 问题处理总结

    自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...

  5. 【转】iOS使用NSMutableAttributedString实现富文本

    iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...

  6. JavaScript Boolean(布尔) 对象

    创建 Boolean 对象 Boolean 对象代表两个值:"true" 或者 "false" 下面的代码定义了一个名为 myBoolean 的布尔对象: va ...

  7. 用response输出一个验证码

    package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...

  8. PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么?

    PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么? 代码如下: <?php $arr = array('one','two','three'); fo ...

  9. 抽象数据类型Triplet的C语言实现

    #include <stdio.h> #include <stdlib.h> #define ERROR 0 #define OK 1 typedef int Status; ...

  10. javabean 简介

    javabean其实包含多个方面的含义.   Java语言开发的可重用组件 优点:1,代码简洁.2,HTML与Java分离,好维护.3,将常用程序写成可重用组件,避免重复.   特点:1,所有类放在同 ...