zoukankan      html  css  js  c++  java
  • POJ 3083, Children of the Candy Corn

    Time Limit: 1000MS  Memory Limit: 65536K
    Total Submissions: 2934  Accepted: 1418


    Description
    The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

    One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)

    As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

     

    Input
    Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.

    Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').

    You may assume that the maze exit is always reachable from the start point.

    Output
    For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

    Sample Input
    2
    8 8
    ########
    #......#
    #.####.#
    #.####.#
    #.####.#
    #.####.#
    #...#..#
    #S#E####
    9 5
    #########
    #.#.#.#.#
    S.......E
    #.#.#.#.#
    #########

    Sample Output
    37 5 5
    17 17 9

    Source
    South Central USA 2006


    // POJ3083.cpp : Defines the entry point for the console application.
    //

    #include 
    <iostream>
    #include 
    <queue>
    using namespace std;

    static int step[4][2= {0-1-100110};
    //toward up 0, left 1, down 2, right 3
    int DFS(char map[41][41], int x, int y, int w, int h, int toward, bool left)
    {
        
    if (map[y][x]=='E'return 1;
        
    int x1, y1, tw;
        
    if (left == true)
        {
            
    for (int i = toward + 1; i >= toward - 2--i)
            {
                tw 
    = (8 + i)&3;
                x1 
    = x + step[tw][0];
                y1 
    = y + step[tw][1];
                
    if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#'
                    
    break;
            };
        }
        
    else
        {
            
    for (int i = toward - 1; i <= toward + 2++i)
            {
                tw 
    = (8 + i)&3;
                x1 
    = x + step[tw][0];
                y1 
    = y + step[tw][1];
                
    if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#'
                    
    break;
            };
        }
        
    return 1 + DFS(map, x1, y1, w, h, tw, left);
    };
    int BFS(char map[41][41], int x, int y, int w, int h)
    {
        
    int cnt[41][41];
        memset(cnt,
    0,sizeof(cnt));
        cnt[y][x] 
    = 1;

        queue
    <pair<int,int>> q;
        q.push(make_pair
    <int,int>(x, y));

        
    int x1,y1;
        
    while (!q.empty())
        {
            pair
    <int,int> cur = q.front();
            q.pop();
            
            
    if (map[cur.second][cur.first] == 'E'return cnt[cur.second][cur.first];
            
    for (int i = 0; i < 4++i)
            {
                x1 
    = cur.first + step[i][0];
                y1 
    = cur.second + step[i][1];
                
    if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#' && cnt[y1][x1] == 0)
                {
                    cnt[y1][x1] 
    = cnt[cur.second][cur.first] + 1;
                    q.push(make_pair
    <int,int>(x1,y1));
                }
            }
        }
        
    return 0;
    }
    int main(int argc, char* argv[])
    {
        
    char map[41][41];
        
    int cases;
        cin 
    >> cases;

        
    for (int c = 0; c < cases; ++c)
        {
            
    int w,h;
            scanf(
    "%d %d\n"&w, &h);
            
    for (int i = 0; i < h; ++i)
                gets(map[i]);

            
    int x, y;
            
    for (int i = 0; i < h; ++i)
            {
                x 
    = distance(&map[i][0],std::find(&map[i][0],&map[i][w],'S'));
                
    if (x < w)
                {
                    y 
    = i;
                    
    break;
                }
            }

            
    int toward = 0;
            
    if (x == w - 1) toward = 1;
            
    else if (y == 0) toward = 2;
            
    else if (x == 0) toward = 3;
            cout 
    << DFS(map, x, y, w, h, toward, true<< " "
        << DFS(map, x, y, w, h, toward, false<< " " << BFS(map, x, y, w, h) << endl;
        }
        
    return 0;
    }
  • 相关阅读:
    AngularJs学习——常用表单指令练习
    JavaScript知识递归实现数组中指定后代元素的查找
    JavaScript知识之判断字符串中出现最多的字符及次数
    CSS3知识之立方体动画效果
    CSS知识之 background-size 用法详细介绍
    CSS知识之 background-position 用法详细介绍
    CSS3知识之filter滤镜效果
    CSS3知识之折角效果
    CSS3知识之阴影box-shadow
    JS利用 Sea.js 实现模块化:拖拽、缩放及范围限制
  • 原文地址:https://www.cnblogs.com/asuran/p/1580104.html
Copyright © 2011-2022 走看看