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. mysql -数据库(备份与恢复)

    1,备份某个数据库(以db_abc为例) 1)通过 cmd 切换到mysql 安装目录下的'bin'目录,然后执行'mysqldump -uroot -p db_abc > db_abc_bak ...

  2. iOS打包ipa安装包的流程

    应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...

  3. 【转】iOS开发网络篇—发送json数据给服务器以及多值参数

    原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...

  4. VisualStudio2013&VS2015内置SQLServer入门 (三)

    关于LocalDB的部署(publish): 使用本机做服务器(目测不可行) 双击项目的Properties-->Publish-->Application Files,你会发现没有.md ...

  5. Debug your C# project more efficiently

    I am a very beginner working with C# and Visual Studio 2013. When I debug my project, I always reope ...

  6. JSP标准标签库的安装以及自定义标签的创建

    JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...

  7. ccf集合竞价

    我不懂为什么是错误.然后零分.贴出测试. 然后即使注释掉while循环中的break部分,也是如此. #include<iostream> #include<iomanip> ...

  8. SGU 195. New Year Bonus Grant

    时间限制:0.75s 空间限制:4M 题意: 在一颗树(最多500000个节点)中,可以对节点染色,但是一个节点染了色后,它的父节点和兄弟节点都不能再染了,求最大的染色节点数,并输出所有染色节点. S ...

  9. 【BZOJ2648】【kd_tree】SJY摆棋子

    Description   这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋 ...

  10. 使用BeanUtils组件

    使用BeanUtils组件 前提 1:导入commons-beanutils-1.8.3.jar        //根据  本人使用的是1.8.3的版本 2:导入日志包      //就是loggin ...