zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",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;
    }
  • 相关阅读:
    司徒正美--前端招聘与前端卖身的困境
    解密中国互联网
    将网页设置为允许 XMLHttpRequest 跨域访问
    window.location.hash属性介绍
    解密中国互联网
    javascript多种继承方式(函数式,浅复制,深复制,函数绑定和借用)
    javascript类式继承函数最优版
    javascript类式继承最优版
    javascript数组去重
    查找字符串中出现最多的字符和个数?
  • 原文地址:https://www.cnblogs.com/candy99/p/5926003.html
Copyright © 2011-2022 走看看