POJ2157Maze[DFS !]

本文介绍了一种迷宫寻宝问题的解决方案,涉及到门和钥匙的匹配,通过深度优先搜索算法来判断角色是否能够找到宝藏。文章提供了一个C++实现示例。
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;
}

 

转载于:https://www.cnblogs.com/candy99/p/5926003.html

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值