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实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  2. ubuntu 16.04环境配置

    ubuntu 16:1.源cp /etc/apt/sources.list /etc/apt/sources.list.bkpvi /etc/apt/sources.list-+{    deb ht ...

  3. SpringMVC+Spring+hibernate整合及分页

    1. 新建web project 2. 引入jar, 3. 创建包com.tgb.web.controller, 下面创建包(dao,entity,service, config,spring,hib ...

  4. PHP测试题讲解(20161027)

    注: 关联数组 关联数组,它的每个 ID 键都关联一个值.在存储有关具体命名的值的数据时,使用数值数组不是最好的做法.通过关联数组,我们可以把值作为键,并向它们赋值. 例子 1 在本例中,我们使用一个 ...

  5. vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装

    sudo apt-get purge vim-common sudo apt-get update sudo apt-get upgrade sudo apt-get install vim Just ...

  6. 学习Karma+Jasmine+istanbul+webpack自动化单元测试

    学习Karma+Jasmine+istanbul+webpack自动化单元测试 1-1. 什么是karma?  Karma 是一个基于Node.js的Javascript测试执行过程管理工具.该工具可 ...

  7. 【SPL标准库专题(5)】 Datastructures:SplStack &amp; SplQueue

    这两个类都是继承自SplDoublyLinkedList,分别派生自SplDoublyLinkedList的堆栈模式和队列模式:所以放在一起来介绍: 堆栈SplStack # 类摘要 SplStack ...

  8. Android源代码下载以及异常查找网站推荐

    源代码下载:https://github.com/ 异常查找:http://stackoverflow.com/

  9. Debug 路漫漫-01

    运行到子函数时提示报错:  === 这个断点一步步debug下来是顺利的,但是咋就超出数组范围了呢,这会是什么问题. ——sess肯定超过索引了,那个sess(:,2)的值肯定超过V的行数了. ——由 ...

  10. php-fpm简介

    What is PHP-FPM? PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with ...