zoukankan      html  css  js  c++  java
  • POJ 2157 Maze

    Maze
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 3183   Accepted: 996

    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
    #include<stdio.h>
    #include<iostream>
    #include <string.h>
    #include <queue>
    using namespace std;
    typedef struct node
    {
        int x;
        int y;
        node(int a, int b)
        {
            x = a;
            y = b;
        }
        node()
        {
    
        }
    }Map;
    
    char Maze[25][25];
    int Dir[4][2] = {-1,0,1,0,0,-1,0,1};
    int key[10];
    
    void BFS(int sx, int sy, int m, int n)
    {
        queue<Map> Queue;
        Queue.push(Map(sx, sy));
        Map temp;
        Maze[sx][sy] = 'X';
        int Limit = 0;
        while(!Queue.empty() && Limit < 400)
        {
            ++Limit;
            temp = Queue.front();
            Queue.pop();
            if (Maze[temp.x][temp.y] >= 'A' && Maze[temp.x][temp.y] <= 'E')
            {
                if (key[Maze[temp.x][temp.y] - 'A'] == 0)
                {
                    Maze[temp.x][temp.y] = 'X';
                }
                else
                {
                    Queue.push(temp);
                    continue;
                }
            }
            for (int i = 0; i < 4; i++)
            {
                int x = temp.x + Dir[i][0];
                int y = temp.y + Dir[i][1];
                if (x >= 0 && x < m && y >= 0  && y < n && Maze[x][y] != 'X')
                {
                    if (Maze[x][y] == '.')
                    {
                        Maze[x][y] = 'X';
                        Queue.push(Map(x, y));
                    }
                    if (Maze[x][y] >= 'a' && Maze[x][y] <= 'e')
                    {
                        key[Maze[x][y] - 'a']--;
                        Maze[x][y] = 'X';
                        Queue.push(Map(x, y));
                    }
                    if (Maze[x][y] == 'G')
                    {
                        printf("YES
    ");
                        return;
                    }
                    if (Maze[x][y] >= 'A' && Maze[x][y] <= 'E')
                    {
                        Queue.push(Map(x, y));
                    }            
                }
            }
        }
        printf("NO
    ");
    }
    
    int main()
    {
        int m, n;
        int sx, sy;
        while(scanf("%d%d", &m, &n) != EOF)
        {
            if (m == 0 && n == 0)
            {
                break;
            }
            memset(key, 0, sizeof(key));
            for (int i = 0; i < m; i++)
            {
                scanf("%s", Maze[i]);
                for (int j = 0; j < n; j++)
                {
                    if (Maze[i][j] == 'S')
                    {
                        sx = i;
                        sy = j;
                    }
                    else
                    {
                        if (Maze[i][j] >= 'a' && Maze[i][j] <= 'e')
                        {
                            key[Maze[i][j] - 'a']++;
                        }
                    }
                }
            }
            BFS(sx, sy, m, n);
        }
        return 0;
    }
  • 相关阅读:
    003. 爬楼梯
    ZFlie网盘框架说明
    WPF基础:Dispatcher介绍
    WPF自定义控件三:消息提示框
    GO Time 类型方法处理集合
    UserControl 加载动画
    WPF自定义控件二:Border控件与TextBlock控件轮播动画
    WPF自定义控件一:StackPanel 控件轮播
    WPF 图表控件之曲线绘制与移动
    VueApp 自动更新解决plus is not defined问题
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3204303.html
Copyright © 2011-2022 走看看