POJ 3083 - Children of the Candy Corn

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9157 Accepted: 3969

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

题意就是 一个迷宫,从S到E,先按照 优先走转左的方案走,再按照 优先转右的方案走,最后 求最短路。

打印三种答案。


宽搜和深搜的初等题目,不难但就是坑,我让这4个方向,一天弄的晕头转向的,方向感不好的渣渣啊

也不会调递归的程序,坑


深搜在本题,不再是传统深度搜索了,而是左优先搜索、 右优先搜索,本题的亮点,但是本题的坑



左优先搜索 是 当前面向的方向   先走左手处,再按顺时针走向依次访问

右优先搜索 是 当前面向的方向  先走右手处,再按逆时针走向依次访问

以左搜为例:

 假设面向(上 Up),那么首先访问的点是( Left),->左手边点

         面向 (右Right),那么首先访问的点是(上Up)->左手边点

   这个题没什么特别的写法,看了网上题解都差不多,200来行

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define left  1             // 此处建议最好用英语单词来代表方向
#define up    2             //因为这个题的方向太繁琐了,用数字一会就蒙了
#define right 3
#define down  4
const int INF = 65535;
const int Size = 1<<15;
const int N = 45;
using namespace std;
int mp[N][N],vis[N][N];
int sx,sy,w,h,le,ri;
int flag1,flag2,sum;
struct node
{
    int x,y,ans;
}q[Size];
int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
char b[N][N];
void DFS1(int x,int y,int d,int to)
{
    if(flag1 == 1) //刚开始没写这个!!!DFS停不了
        return;   
    if(b[x][y]=='E')
    {
        flag1 = 1;
        le = d;
        return ;
    }

     if(to == up)
   {
        if(mp[x][y-1]) // 左
            DFS1(x,y-1,d+1,left);
        if(mp[x-1][y])  // 上
            DFS1(x-1,y,d+1,up);
        if(mp[x][y+1])  // 右
            DFS1(x,y+1,d+1,right);
        if(mp[x+1][y])  //下
            DFS1(x+1,y,d+1,down);
    }
    else if(to == left)
    {
        if(mp[x+1][y]) //面向左 ,但是左手边是 下,所以先走下
            DFS1(x+1,y,d+1,down);
        if(mp[x][y-1])
           DFS1(x,y-1,d+1,left);
        if(mp[x-1][y])
            DFS1(x-1,y,d+1,up);
        if(mp[x][y+1])
            DFS1(x,y+1,d+1,right);
    }
    else if(to == right)
    {
        if(mp[x-1][y])
            DFS1(x-1,y,d+1,up);
        if(mp[x][y+1])
            DFS1(x,y+1,d+1,right);
        if(mp[x+1][y])
            DFS1(x+1,y,d+1,down);
        if(mp[x][y-1])
            DFS1(x,y-1,d+1,left);
    }
    else
    {
        if(mp[x][y+1])
            DFS1(x,y+1,d+1,right);
        if(mp[x+1][y])
            DFS1(x+1,y,d+1,down);
        if(mp[x][y-1])
            DFS1(x,y-1,d+1,left);
        if(mp[x-1][y])
            DFS1(x-1,y,d+1,up);
    }
}

void DFS2(int x,int y,int d,int to)//右优先搜索
{
    if(flag2 == 1)
        return;
    if(b[x][y]=='E')
    {
        flag2 = 1;
        ri = d;
        return;
    }

     if(to == up)
    {
        if(mp[x][y+1])
            DFS2(x,y+1,d+1,right);
        if(mp[x-1][y])
             DFS2(x-1,y,d+1,up);
        if(mp[x][y-1])
             DFS2(x,y-1,d+1,left);
        if(mp[x+1][y])
             DFS2(x+1,y,d+1,down);
    }else if(to == left)
    {
        if(mp[x-1][y])
            DFS2(x-1,y,d+1,up);
        if(mp[x][y-1])
             DFS2(x,y-1,d+1,left);
        if(mp[x+1][y])
             DFS2(x+1,y,d+1,down);
        if(mp[x][y+1])
             DFS2(x,y+1,d+1,right);
    }
    else if(to == right)
    {
        if(mp[x+1][y])
             DFS2(x+1,y,d+1,down);
        if(mp[x][y+1])
             DFS2(x,y+1,d+1,right);
        if(mp[x-1][y])
             DFS2(x-1,y,d+1,up);
        if(mp[x][y-1])
             DFS2(x,y-1,d+1,left);
    }
    else
    {
        if(mp[x][y-1])
             DFS2(x,y-1,d+1,left);
        if(mp[x+1][y])
             DFS2(x+1,y,d+1,down);
        if(mp[x][y+1])
             DFS2(x,y+1,d+1,right);
        if(mp[x-1][y])
             DFS2(x-1,y,d+1,up);
    }
}
void BFS(int x,int y)//宽搜,注意手残
{
    int s=0,e=0;
    node t,f;
    f.x = x;
    f.y = y;
    f.ans = 0;
    vis[f.x][f.y] = 1;
    q[e++] = f;
    while(s < e)
    {
        t = q[s++];
        if(b[t.x][t.y]=='E')
        {
            sum = t.ans;
            return ;
        }
        for(int i = 0;i<4;i++)
        {
            f.x = t.x + mv[i][0];
            f.y = t.y + mv[i][1];
            if(!vis[f.x][f.y] && 0<=f.x && f.x<h && 0<=f.y && f.y<w && mp[f.x][f.y]!=0)
            {
                f.ans = t.ans + 1;
                q[e++] = f;
                vis[f.x][f.y] = 1;
            }
        }
    }
}
void init()
{
    flag1 = 0;
    flag2 = 0;
    memset(mp,0,sizeof(mp));
    memset(vis,0,sizeof(vis));
}
int main()
{
    int t;
    char a[N];
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&w,&h);
        for(int i = 0;i < h;i++)
        {
            scanf("%s",a);
            strcpy(b[i],a);
            for(int j = 0;j < w;j++)
            {
                if(a[j] == '.' || a[j] == 'E')
                    mp[i][j] = 1;
                if(a[j] == 'S')
                {
                    mp[i][j] = 1;
                    sx = i;
                    sy = j;
                }
            }
        }
        DFS1(sx,sy,0,up); // 假设初始化 都面向上,初始化随意,爱面向哪面向哪
        DFS2(sx,sy,0,up); 
        BFS(sx,sy);
        printf("%d %d %d\n",le+1,ri+1,sum+1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值