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. [转]maven创建自定义的archetype

    创建自己的archetype一般有两种方式,比较简单的就是create from project 1.首先使用eclipse创建一个新的maven project,然后把配置好的一些公用的东西放到相应 ...

  2. 如何保护在Autodesk应用程序商店的应用不被盗版 - 1

    Autodesk应用程序商店如火如荼,但来自中国的应用却还是寥寥无几.大家在担心什么呢?可能其中一个因素就是担心自己的应用上线后被盗版的问题.对应用的版权保护和授权管理是每个应用开发者都应该认真考虑的 ...

  3. Android 进程生命周期 Process Lifecycle

    Android 进程生命周期 Process Lifecycle 进程的生命周期 Android系统会尽力保持应用的进程,但是有时为了给新的进程和更重要的进程回收一些内存空间,它会移除一些旧的进程. ...

  4. tomcat <context path>的意义及作用

    context path 是在tomcat 要支持多个应用时对每个应用的docBase做区别时的区分符. 打个比方假如你有两个请求:一个为 http:localhost:8080/test1/hell ...

  5. iOS Unicode和汉字互转

    //unicode转中文 NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEn ...

  6. 新萝卜家园GHOST WIN7系统32,64位极速装机特别版

    系统来自:系统妈:http://www.xitongma.com 新萝卜家园GHOST win7系统32位极速装机特别版 V2016年3月 系统概述 新萝卜家园ghost win7系统32位极速装机特 ...

  7. 【代码笔记】iOS-给背景图加上移动的手势

    一,工程图. 二,效果图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  8. IOS 音效

    IOS 音效 音效我们也可以成为短音频通常在程序中播放时间为1~2秒. 在应用程序中起到点缀效果,提升整体用户体验 音效文件只需要加载一次 示例代码: // // ViewController.m / ...

  9. Strange Problem O(∩_∩)O~

    题目描述: 古代某个狱卒某天闲着没事想和两个罪犯玩个游戏,他找了个国际象棋盘,每个格子放上一个硬币,硬币长得都一样,正反都是狱卒自己决定. 之后他只让A罪犯观看棋盘,并随便指一个硬币告诉A罪犯,只要B ...

  10. 你知道C#中的Lambda表达式的演化过程吗

    你知道C#中的Lambda表达式的演化过程吗? 阅读目录 委托的使用 匿名方法 Func和Action Lambda的诞生 那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的东西 ...