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. 小谈React、React Native、React Web

    React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...

  2. java--POI解析excel兼容性问题

    近日,使用POI解析excel,发现2003版本的excel解析与2007版本的excel解析存在问题.特此总结: 1.所需jar包 : 2.java类代码(读取excel文件): public vo ...

  3. sharepoint2010问卷调查(4)-实现问卷的重复答复次数(采用自定义字段类型和JS)

    sharepoint的问卷调查可以设置重复和一次答复.但是设置一次后,调查过的用户再进行答复.会提示如下图: 分析下:该提示用户体验很不好.给用户感觉是系统出问题了.因此网上有人提出用eventhan ...

  4. smarty访问数组中的数据,如果是关联数组直接用点.

    $tpl=new Smarty();//新建一个smarty对象,我使用的是Smarty-3.1.6版本 1.设置smarty模板路径$tpl->setTemplateDir():默认情况下是t ...

  5. 使用Masonry搭建特殊布局时与xib的对比

    之前只有比较浅的接触过Masonry.项目中大多数的布局还是用xib中的AutoLayout与手码的frame计算相结合,相信也会有很多项目和我一样是这两种布局的组合.其实xib各方面用的感觉都挺好, ...

  6. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

  7. Android 常用抓包工具介绍之Charles

    ➠更多技术干货请戳:听云博客 Charles是一款抓包修改工具,相比起TcpDump,charles具有界面简单直观,易于上手,数据请求控制容易,修改简单,抓取数据的开始暂停方便等等优势!前面介绍了如 ...

  8. iOS 开发之路(AES/DES加密实现) 三

    最近接触的这个项目由于以前服务器上用的是DES/CBC/PKCS5Padding加密方式,为了让在iOS上的加密结果与服务器端保持一致,我做了很多尝试,现在分享给大家.PS:现在不推荐用DES了,只是 ...

  9. cocoapods pod setup

    在终端  输入 pod setup  之后 你会发现 一直没有反应    会出现这种情况   如下图   你要耐心等一下. 直到看到Setup completed    就算是成功了.(你的网络要好) ...

  10. ARC下内存泄露问题

    ARC下内存泄露问题 ARC下内存泄露问题,有需要的朋友可以参考下. iOS提供了ARC功能,很大程度上简化了内存管理的代码. 但使用ARC并不代表了不会发生内存泄露,使用不当照样会发生内存泄露. 下 ...