Maze
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3818   Accepted: 1208

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

Source

POJ Monthly,Wang Yijie

题意:迷宫,有门有钥匙

1.多个G,不能简单记录终点
2.有的门没钥匙,就是不能进
不需要回溯,一边搜下去,遇到钥匙判断一下这个钥匙凑齐了没有,如果凑齐了则增加从这个钥匙能打开的门开始搜索(用mark表示这个门有没有到达过)
 
//
// main.cpp
// poj2157
//
// Created by Candy on 10/1/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int N=,V=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,sx,sy,flag=;
char a[N][N];
struct doors{
int k,x,y;
}door[N*N];
int cnt=;
int vis[N][N],g[N][N],dx[]={-,,,},dy[]={,,,-};
struct keys{
int has,tot;
}key[N];
int mark[N][N];
void dfs(int x,int y){//printf("dfs %d %d\n",x,y);
vis[x][y]=;
if(a[x][y]=='G'){flag=;return;}
if(flag) return;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(vis[nx][ny]) continue;
if(nx<||nx>n||ny<||ny>m) continue;
if(g[nx][ny]<=&&g[nx][ny]>=){
int num=g[nx][ny];
mark[nx][ny]=;
if(key[num].has<key[num].tot||(!key[num].tot)) continue;
}
if(g[nx][ny]>) {
int num=g[nx][ny]-;
key[num].has++;
if(key[num].has==key[num].tot&&key[num].tot){//no key cannot in
for(int j=;j<=cnt;j++)
if(door[j].k==num&&mark[door[j].x][door[j].y]){
dfs(door[j].x,door[j].y);
}
}
}
dfs(nx,ny);
}
//printf("end %d %d\n",x,y);
}
int main(int argc, const char * argv[]) {
while(scanf("%d%d",&n,&m)&&n){
memset(g,,sizeof(g));
memset(vis,,sizeof(vis));
memset(door,,sizeof(door));
memset(key,,sizeof(key));
memset(mark,,sizeof(mark));
cnt=;
for(int i=;i<=n;i++){
flag=;
scanf("%s",a[i]+);
for(int j=;j<=m;j++){
if(a[i][j]=='S') sx=i,sy=j;
else if(a[i][j]=='X') vis[i][j]=;
else if(a[i][j]>='A'&&a[i][j]<='E'){
g[i][j]=a[i][j]-'A'+;//1...5
door[++cnt].x=i;door[cnt].y=j;door[cnt].k=g[i][j];
}else if(a[i][j]>='a'&&a[i][j]<='e'){
key[a[i][j]-'a'+].tot++;
g[i][j]=a[i][j]-'a'+;//6...10
}
}
}
dfs(sx,sy);
if(flag) puts("YES");
else puts("NO");
//printf("%d %d %d",key[2].has,key[2].tot,door[2].k);
} return ;
}

POJ2157Maze[DFS !]的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

随机推荐

  1. js实现标准无缝滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 封装Nvelocity的渲染方法

    public class CommonHelper { /// <summary> /// 用data数据填充templatename模板,渲染返回html返回 /// </summ ...

  3. Telerik JustDecompile 2014.1.255.0 开发版(.NET反编译神器,免费下载)

    Telerik JustDecompile是Telerik公司推出一个免费的.NET反编译工具,支持插件与Visual Studio 2015~2013集成,还能够创建Visual Studio Pr ...

  4. Snort - manual 笔记(四)

    1.7 Basic Output Snort可以做很多任务, 并且在任务完成后输出很多有用的统计信息. 一些不用说明就可以看懂, 其他的总结在这里, 不过只是一些基本的 1.7.1 Timing St ...

  5. Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer

    Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer 1. Java NIO(New Input/Output)1 1.1. 变更通知(因为每个事件都需要一个监听者 ...

  6. Android 多媒体播放API简介

    本文调用android的媒体播放器实现一些音乐播放操作 项目布局: <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  7. Android NDK目录介绍

    交叉编译 在一个平台上去编译另一个平台上可以执行的本地代码 cpu平台---arm x86 mips 操作系统平台---windows linux mac os 原理 模拟不同平台的特性去编译代码 j ...

  8. OC 中的block存储位置

    以下所有在ARC情况下: 一.block块的存储位置(block块入口地址):可能存放在2个地方:代码区.堆区(程序分5个区,还有常量区.全局区和栈区,对于MRC情况下代码还可能存在栈区.关于分区详细 ...

  9. Charles中如何对https抓包

    前言:下面介绍关于Charles中如何对https抓包 1.在默认没有相关设置HTTPS需要设置相关操作的时候,会出现下面的情况: 2.下面就是设置SSL Proxying,然后443是默可用的端口 ...

  10. Enabling Cross-Origin Requests in ASP.NET Web API 2

    Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...