正文

Children of the Candy Corn (阅读理解)2007-08-13 17:20:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/28464.html

分享到:

Children of the Candy Corn
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 29, Accepted users: 23
Problem 10833 : No special judgement
Problem 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

///// best 路径寻找我是用 DP的

#include <stdio.h>
#include <string.h>
#define MAX 60

enum  FACE { LEFT , UP, RIGHT, DOWN };
char gm[MAX][MAX];
typedef struct Node{
    int x, y;
}NODE;
NODE gS,gE;
int nX, nY, nPx,nPy, dest, SaveDest, gmBest[MAX][MAX];

void FindNode(char c, NODE *gN){
    char *p;
    int  i;
    if((p=strchr(gm[1],c))){
        gN->x = p-gm[1]; 
        gN->y = 1;
        *p = '.';
        SaveDest = dest = DOWN;
    }else if((p=strchr(gm[nY],c))){
        gN->x = p-gm[nY]; 
        gN->y = nY;
        *p = '.';
        SaveDest = dest = UP;
    }else
        for(i=1; i<=nY; i++){
            if(gm[i][1]==c){
                gN->x = 1; 
                gN->y = i;
                gm[i][1]='.';
                SaveDest = dest = RIGHT;
            }else if(gm[i][nX]==c){
                gN->x = nX; 
                gN->y = i;
                gm[i][nX]='.';
                SaveDest = dest = LEFT;
            }
        }
}

int SearchLeft(){
    if(nPx == gE.x && nPy == gE.y)
        return 1;
    switch(dest){
        case UP:
            if(gm[nPy][nPx-1]=='#'){
                dest = RIGHT;
                return SearchLeft();
            }
            else{ 
                nPx--;
                dest = LEFT;
                return SearchLeft()+1;
            }
        case RIGHT:
            if(gm[nPy-1][nPx]=='#'){
                dest = DOWN;
                return SearchLeft();
            }
            else {
                nPy--;
                dest = UP;
                return SearchLeft()+1;
            }
        case DOWN:
            if(gm[nPy][nPx+1]=='#'){
                dest = LEFT;
                return SearchLeft();
            }
            else {
                nPx++;
                dest = RIGHT;
                return SearchLeft()+1;
            }
        case LEFT:
            if(gm[nPy+1][nPx]=='#'){
                dest = UP;
                return SearchLeft();
            }
            else {
                nPy++;
                dest = DOWN;
                return SearchLeft()+1;
            }
    }
    return 0;
}

int SearchRight(){
    if(nPx==gE.x && nPy==gE.y)
        return 1;
    switch(dest){
        case UP:
            if(gm[nPy][nPx+1]=='#'){
                dest = LEFT;
                return SearchRight();
            }
            else{ 
                nPx++;
                dest = RIGHT;
                return SearchRight()+1;
            }
        case RIGHT:
            if(gm[nPy+1][nPx]=='#'){
                dest = UP;
                return SearchRight();
            }
            else {
                nPy++;
                dest = DOWN;
                return SearchRight()+1;
            }
        case DOWN:
            if(gm[nPy][nPx-1]=='#'){
                dest = RIGHT;
                return SearchRight();
            }
            else {
                nPx--;
                dest = LEFT;
                return SearchRight()+1;
            }
        case LEFT:
            if(gm[nPy-1][nPx]=='#'){
                dest = DOWN;
                return SearchRight();
            }
            else {
                nPy--;
                dest = UP;
                return SearchRight()+1;
            }
    }
    return 0;
}

void Update(int x, int y, int step, NODE a[],int *i){
    if(gmBest[y][x] && gmBest[y][x]<=step)
        return;
    gmBest[y][x] = step;
    a[*i].x = x;
    a[*i].y = y;
    (*i)++;
}
void SearchBest(int x, int y){
    NODE a[4];
    int  i=0,j;
    if(x==gS.x && y==gS.y)
        return ;
    if(gm[y-1][x]!='#')
        Update(x,y-1,gmBest[y][x]+1,a,&i);
    if(gm[y+1][x]!='#')
        Update(x,y+1,gmBest[y][x]+1,a,&i);
    if(gm[y][x-1]!='#')
        Update(x-1,y,gmBest[y][x]+1,a,&i);
    if(gm[y][x+1]!='#')
        Update(x+1,y,gmBest[y][x]+1,a,&i);
    for(j=0; j<i; j++)
        SearchBest(a[j].x, a[j].y);
}

void InitSearch(){
    dest = SaveDest;
    nPx = gS.x;
    nPy = gS.y;
}

int main(){
    int  n,i,j,left,right;
    char str[MAX];
    scanf("%d",&n);
    for(i=0; i<n; i++){
        scanf("%d %d",&nX, &nY);
        for(j=0; j<nX+2; j++)
            str[j]='#';
        str[j]='\0';
        strcpy(gm[0],str);
        strcpy(gm[nY+1],str);
        for(j=1; j<=nY; j++){
            scanf("%s",str);
            sprintf(gm[j],"#%s#",str);
        }
        FindNode('S',&gS);
        FindNode('E',&gE);
        InitSearch();
        left = SearchLeft();
        InitSearch();
        right= SearchRight();
        InitSearch();
        memset(gmBest,0,sizeof(int)*MAX*MAX);
        SearchBest(gE.x,gE.y);
        printf("%d %d %d\n",left,right,gmBest[gS.y][gS.x]+1);
    }
    return 0;
}

阅读(4351) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册