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. HTML中input标签的alt属性和title属性的比较

    经常用到这两个属性,但是一直没有总结他们的区别.现在我对他们两个的用法做一下总结: 相同点:他们都会飘出一个小浮层,显示文本内容. 不同点: 1.alt只能是元素的属性,而title即可以是元素的属性 ...

  2. JavaSript模块规范 - AMD规范与CMD规范介绍 (转载lovenyf.blog.chinaunix.net)

    JavaSript模块化   在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?       模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...

  3. 刷新ALV定位到当前记录行

    如果使用"REFRESH_TABLE_DISPLAY"刷新ALV后,记录会跳到第一行,以下代码可以使记录仍然定位在当前行 DATA ls_stable TYPE lvc_s_stb ...

  4. sharepoint项目遇到的WebDAV和HTTP PUT的安全隐患解决办法

    最近一个项目,客户进行了安全检测,检测出如下安全隐患,其实这些隐患全是IIS设置的事情   许多人误认为SharePoint是在使用由IIS提供的WebDAV功能. 实际上, SharePoint在S ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q9-Q12)

    Question 9 You are designing an external binary large object (BLOB) store provider by implementing t ...

  6. 对抽屉效果几大github第三方库的调研

    在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库ECS评价不是很好.现对当下比较火的几大热门抽屉效果的第三方库进行了调研.代码全部选自github 如果你 ...

  7. [翻译] GONMarkupParser

    GONMarkupParser https://github.com/nicolasgoutaland/GONMarkupParser NSString *inputText = @"Sim ...

  8. Android忘记密码功能实现

    连续好几天学习都没有什么进展,然而在今天这个烂漫的日子.突然有了学习的动力.想起来前几日老师给布置的android忘记密码的功能实现.今天也有了想法.就是按照老师的建议,简单的回答一个问题,实现此功能 ...

  9. GET和POST请求

    GET与POST请求 简介 GET请求解释及语法格式 POST请求简介及语法 GET请求代码 POST请求代码 GET请求解释及语法格式: 网络请求默认是get 网络请求有很多种:GET查 POST改 ...

  10. Android中应用程序清除data/data,清除cache,超详细

    清除data,清除cache,其实在Android原生Setting里面有这个功能的. 需求是把这个功能做到自己的App里面,并计算出cache和data的size. 所以参考了一下Setting的源 ...