POJ2157Maze[DFS !]

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

 

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值