• 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;
    }
  • 相关阅读:
    Batch
    Batch
    《mysql必知必会》学习_第17章
    指针实现时间复杂度为O(n*logN)的排序算法(归并排序算法)
    《自控力》读书笔记
    Leetcode 49. Group Anagrams (242.Valid Anagram)
    Leetcode43. Multiply Strings
    Leetcode 233. Number of Digit One
    Python 格式化输出占位符替换
    Python字符串拼接
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3204303.html
Copyright © 2020-2023  润新知