NOI题库1159 Maze
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的更多相关文章
- NOI题库刷题日志 (贪心篇题解)
这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个平面上,如果有两个点( ...
- NOI题库 1768最大子矩阵 题解
NOI题库 1768最大子矩阵 题解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大 ...
- NOI题库 09:图像旋转翻转变换
NOI题库开始的题,也是略水,当然也是大水,所以彼此彼此 09:图像旋转翻转变换 总时间限制: 1000ms 内存限制: 65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作 ...
- NOI题库-小学奥赛QwQ
今天Loli教育我们让我们来看看NOI题库的奥赛部分,不过,为何是小学的( ⊙ o ⊙ )啊!感觉智商被各种侮辱. 余数相同问题: 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为 ...
- noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35
T31 字符串P型编码 描述 给定一个完全由数字字符('0','1','2',-,'9')构成的字符串str,请写出str的p型编码串.例如:字符串122344111可被描述为"1个1.2个 ...
- NOI题库192 生日蛋糕
192:生日蛋糕 总时间限制: 5000ms 内存限制: 65536kB 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i ...
- NOI 题库 9272 题解
9272 偶数个数字3 描述 在所有的N位数中,有多少个数中有偶数个数字3? 输入 一行给出数字N,N<=1000 输出 如题 样例输入 2 样例输出 73 Solution : 令f ( ...
- 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, ...
- noi题库(noi.openjudge.cn) 1.5编程基础之循环控制T36——T45
T36 计算多项式的值 描述 假定多项式的形式为xn+xn-1+-+x2+x+1,请计算给定单精度浮点数x和正整数n值的情况下这个多项式的值. 输入 输入仅一行,包括x和n,用单个空格隔开.x在flo ...
随机推荐
- 玩javaweb的web.xml编译路径
有时候能够碰到这样的情况 缓存就是 清不掉 那就可以去寻找编译路径了 <Context docBase="E:\java-workspace\eigyo_com405" pa ...
- EntityFramework在root目录web.config中的配置设置
未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...
- php+支付宝整合
CREATE TABLE IF NOT EXISTS `alipay_order` ( `id` ) unsigned NOT NULL auto_increment, `orderid` ) NOT ...
- Xcode的控制台调试命令
XCode4.0以后,编译器换成了LLVM 编译器 2.0 与以前相比,更加强大:1.LLVM 编译器是下一带开源的编译技术.完全支持C, Objective-C, 和 C++.2.LLVM 速度比 ...
- 安卓开发service
如果把Activity比喻为前台程序,那么service可以看做是一个后台程序.Service跟Activity一样也由Intent调用. 在工程里想要添加一个Service,先新建继承Service ...
- gulp-htmlmin可以压缩html的gulp插件
通过一条命令用Npm安装gulp-htmlmin: npm install gulp-htmlmin --save-dev 安装完毕后,打开gulpfile.js文件,我们里面编写一个task用来专门 ...
- Linux命令:chmod命令
chmod命令:改变文件或目录的存取权限 #权限代号 -r 文件被读取 4 -w 文件被写入 2 -x 文件被执行 1 #权限范围 -u 文件所有者 -g 文件所有者所在组 -o 其他 -a 全部 # ...
- IoC模式(控制反转)(转)
转自:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html,写的很好,用C#代码解释控制反转,然后更进一步,提到依赖注入是控制反转的 ...
- Git配置安装使用教程操作github上传克隆数据
Git是何方神圣? Git是用C语言开发的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态).另一个状态可以是不同的文件,也可以是不同的文件内容 ...
- WinPcap编程(一)
0. 按着文档顺序写的. 开发环境:win10+VS2013. 配置WinPcap环境就不多说.直接给网址:http://blog.sina.com.cn/s/blog_57432f380101qh3 ...