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;
,V=1e6+;
inline int read(){
    ,f=;
    ;c=getchar();}
    +c-';c=getchar();}
    return x;
}
;
char a[N][N];
struct doors{
    int k,x,y;
}door[N*N];
;
]={-,,,},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]=;
    ;return;}
    if(flag) return;
    ;i<;i++){
        int nx=x+dx[i],ny=y+dy[i];
        if(vis[nx][ny]) continue;
        ||nx>n||ny<||ny>m) continue;
        &&g[nx][ny]>=){
            int num=g[nx][ny];
            mark[nx][ny]=;
            if(key[num].has<key[num].tot||(!key[num].tot)) continue;
        }
        ) {
            ;
            key[num].has++;
            if(key[num].has==key[num].tot&&key[num].tot){//no key cannot in
                ;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=;
        ;i<=n;i++){
            flag=;
            scanf();
            ;j<=m;j++){
                if(a[i][j]=='S') sx=i,sy=j;
                ;
                else if(a[i][j]>='A'&&a[i][j]<='E'){
                    g[i][j]=a[i][j]-;//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]-].tot++;
                    g[i][j]=a[i][j]-;//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);
    }

    ;
}

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. display:table-cell介绍

    一.display:table-cell属性简述 display:table-cell属性指让标签元素以表格单元格的形式呈现,类似于td标签.目前IE8+以及其他现代浏览器都是支持此属性的,但是IE6 ...

  2. 泛函编程(28)-粗俗浅解:Functor, Applicative, Monad

    经过了一段时间的泛函编程讨论,始终没能实实在在的明确到底泛函编程有什么区别和特点:我是指在现实编程的情况下所谓的泛函编程到底如何特别.我们已经习惯了传统的行令式编程(imperative progra ...

  3. :selected

    描述: 查找所有选中的选项元素 HTML 代码: <select> <option value="1">Flowers</option> < ...

  4. R----DT包介绍学习

    DT包:查看矩阵或数据框的内容 12 library(DT)datatable(iris, options = list(pageLength = 5)) DT包提供大量UI定制功能,即修改展示的HT ...

  5. JS截取字符串方法

    function textSubstr(str,sub_length){ str = str.trim(); var temp1 = str.replace(/[^\x00-\xff]/g," ...

  6. 关于Repeater中使用DorpWownList的问题

    关于Repeater中使用DorpWownList的问题 前台: <asp:Repeater ID="Repeater1" runat="server" ...

  7. PDF数据防扩散系统方案

    在企业信息化过程中.大量的企业重要图纸和资料都是以电子文件的方式存在.为了避免内部关键数据的外泄,採取了多种方式:设计部门的门禁管制.防火墙.禁止计算机的USB接口等等. 可是泄密问题还是时有发生,原 ...

  8. 八、oracle 分页

    oracle的分页一共有三种方式 方法一 根据rowid来分 SELECT * FROM EMP WHERE ROWID IN (SELECT RID FROM (SELECT ROWNUM RN, ...

  9. 51nod 1770 数数字

    1770 数数字 基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题  收藏  关注 统计一下 aaa ⋯ aaan个a × b 的结果里面 ...

  10. 06 Activity 4中启动模式

    前言:改变Activity的启动模式可以清单文件AndroidManifest的Activity标签添加属性android:launchMode="standard"中修改如下图: ...