poj3083Children of the Candy Corn(dfs)

题目描述:
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12146 Accepted: 5212
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
这里写图片描述
Sample Output
37 5 5
17 17 9

题目大意:
这道题大意就是走迷宫,有两种方式,第一种是贴着左边的墙壁走,另外一种是贴着右边的墙壁走,最后把这两种方式以及最短方式的走的格子数输出出来(包括起点和终点)

题目分析:
要是明白了题目,其实就是两次dfs就够了,关键在于方向的处理上,比如贴着左边的墙壁走,你就要知道什么是他的左边,这就要求知道当前的方向,所以方向这块要进行一个模拟,如果左边有墙,前面没墙,那么就要向前走,如果左边没墙,那么就往左转,然后往前走一格,如果左边和前面都是墙,那么就右转,直到前面没有墙了,这样一直搜到终点就好了,至于对短路径不必重新搜一遍,只需要用一个跟地图一样大小的数组来存储从起点到当前位置的最短路径就好了。接下来看代码应该就明白了。

代码:

#include "stdio.h"
#define INF 0x3f3f3f3f
using namespace std;

char maze[50][50];
int ca,w,h;
int sx,sy,gx,gy;
int d[50][50];
int dxl[4]={1,0,-1,0};//每个点左边的坐标跟dir的关系
int dyl[4]={0,-1,0,1};
int dxr[4]={-1,0,1,0};//每个点右边的坐标跟dir的关系
int dyr[4]={0,1,0,-1};
int dx[4]={0,-1,0,1};//每个点前边的坐标跟dir的关系
int dy[4]={-1,0,1,0};
void left_dfs(int x,int y,int sum,int dir,int len)
{   //sum来传递累计走的步数,而len表示当前的最短路径
    //dir表示方向,0,1,2,3分别为左上右下
    len++;
    sum++;
    if(len<d[x][y]){
        d[x][y]=len;
    }
    if(x==gx&&y==gy){
        printf("%d ",sum);
        return;
    }
    int lx=x+dxl[dir],ly=y+dyl[dir];//左边的坐标
    int fx=x+dx[dir],fy=y+dy[dir];//前面的坐标
    while(maze[lx][ly]=='#'&&maze[fx][fy]=='#'){
        dir=(dir+1)%4;    //左边和前面都是墙
        lx=x+dxl[dir],ly=y+dyl[dir];
        fx=x+dx[dir],fy=y+dy[dir];
    }
    if(maze[lx][ly]=='#'&&maze[fx][fy]=='.'){//左边有墙,前面没墙
        left_dfs(fx,fy,sum,dir,d[x][y]);
    }else if(maze[lx][ly]='.'){  //做边没墙
        dir=(dir+3)%4;
        left_dfs(lx,ly,sum,dir,d[x][y]);
    }
}

void right_dfs(int x,int y,int sum,int dir,int len)
{
    len++;
    sum++;
    if(len<d[x][y]){
        d[x][y]=len;
    }
    if(x==gx&&y==gy){
        printf("%d ",sum);
        return;
    }
    int rx=x+dxr[dir],ry=y+dyr[dir];
    int fx=x+dx[dir],fy=y+dy[dir];
    while(maze[rx][ry]=='#'&&maze[fx][fy]=='#'){
        dir=(dir+3)%4;
        rx=x+dxr[dir],ry=y+dyr[dir];
        fx=x+dx[dir],fy=y+dy[dir];
    }
    if(maze[rx][ry]=='#'&&maze[fx][fy]=='.'){
        right_dfs(fx,fy,sum,dir,d[x][y]);
    }else if(maze[rx][ry]='.'){
        dir=(dir+1)%4;
        right_dfs(rx,ry,sum,dir,d[x][y]);
    }
}

int main()
{
    int i,j;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d",&w,&h);        
        for(i=0;i<h;i++)
        {
            getchar();
            for(j=0;j<w;j++)
            {
                d[i][j]=INF;
                scanf("%c",&maze[i][j]);
                if(maze[i][j]=='S'){
                    sx=i;
                    sy=j;
                    maze[i][j]='.';
                }
                if(maze[i][j]=='E'){
                    gx=i;
                    gy=j;
                    maze[i][j]='.';
                }
            }
        }
        int dir;
        if(sx==0){
            dir=3;
        }else if(sx==h-1){
            dir=1;
        }else if(sy==0){
            dir=2;
        }else{
            dir=0;
        }

        left_dfs(sx,sy,0,dir,0);
        right_dfs(sx,sy,0,dir,0);
        printf("%d\n",d[gx][gy]);
    }
    return 0;
}

你可能感兴趣的:(poj,DFS)