当前位置:编程学习 > C/C++ >>

POJ 3083 Children of the Candy Corn

Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7104   Accepted: 3108
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.      www.zzzyk.com

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
对于这个题,昨天晚上静下心来好好想想发现对于求最短的距离bfs就可以,这是很简单的,但是怎么才能绕墙呢,看了看discuss大部分都是用dfs,我想了想不太会,然后就在那画图,发现这个题其实不用dfs就可以,绕墙到目的地,只会有一条路径,只需要记录刚走过的两个点,判断走的方向 ,对于每种走的方向考虑的情况都是一样的,以沿着左边的墙向上走为例,主要有以下几种情况
1 左边不是墙了,这个时候就需要向左走一个。
2 左边是墙,这个时候需要向前走,这个时候两种情况 (1):前方不是墙直接向前走就可以了
                                                                                        (2):前方是墙,这个时候就要右拐,也分两种情况:
                                                                                                                  《1》 右拐不是墙,直接走可以了。
                                                                                                                   《2》 右拐是墙,这个时候就要退回去了
靠着左边的墙走,会有四种方向,这四种方向考虑的情况都是上面那样,对于向左拐还是右拐,可以开数组,存储一下方向,到时候直接用就行。
这段代码是我刚写出的代码,发现向左和向右处理的都差不多,就是换了换数组,就做了简化处理。两段代码都贴一下吧
[cpp] 
lude <iostream> 
#include <string.h> 
using namespace std; 
int vex1[]={0,0,-1,1},vey1[]={-1,1,0,0}; 
int vex2[]={0,0,1,-1},vey2[]={1,-1,0,0}; 
int vectorx[]={-1,1,0,0},vectory[]={0,0,1,-1}; 
int status[50][50],a[50][50],res1,res2,res3,pos_stax,pos_stay,pos_endx,pos_endy; 
int n,m; 
int main() 

    void deal_left(); 
    void deal_right(); 
    void deal(); 
    int i,j,s,t; 
    char c; 
    cin>>t; 
    while(t--) 
    { 
        cin>>m>>n; 
        for(i=1;i<=n;i++) 
        { 
            for(j=1;j<=m;j++) 
            { 
                cin>>c; 
                if(c=='#') 
                { 
                    a[i][j]=0; 
                }else if(c=='S') 
                { 
                    a[i][j]=0; 
                    pos_stax=i; pos_stay=j; 
                }else if(c=='E') 
                { 
   &n

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,