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. 手势交互之GestureOverlayView

    一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...

  2. The performance between the 'normal' operation and the 'shift' operation.

    First, I gonna post my test result with some code: //test the peformance of the <normal operation ...

  3. webservice发送数据,取数据的方式

    1.通过调用对方的webservice接口方式,取得对方的数据,并解析(我们取数据) 2.对方调用我们的接口,得到数据.(对方来取) 3.对用对方接口,将我们的数据封装好以后,直接调用对方接口,对方可 ...

  4. swift把汉字转换为拼音,并且截取首字母做索引用

    var transformContents = CFStringCreateMutableCopy(nil, 0, "咋啊的看到回复阿斯顿发货发哦iasdifas")CFStrin ...

  5. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  6. 读书笔记之 - javascript 设计模式 - 组合模式

    组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...

  7. xml程序 个人练习1

    package cn.gdpe.xml2; import java.io.File;import java.io.FileOutputStream;import java.util.List; imp ...

  8. cocos2d-x编译错误问题

    在xcode中创建的cocos2d-x项目,然后添加了一个基类,里面有虚方法,编译时出错,错误如下: Undefined symbols for architecture x86_64: " ...

  9. 《zip命令》-linux命令五分钟系列之九

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  10. 数组Api .map()的使用

    之前并没有过多的使用过这个Api,在此记录下对其的理解,方便以后多多使用. 首先是对map的说明: var mappedArray = array.map(callback[, thisObject] ...