POJ 3083 Children of the Candy Corn

一个模拟+bfs就可以水过但是代码量稍微有点大啊、、、不多说了啊满满的都是泪啊,由于bfs标记的错误,RE了十次啊!(bfs每次入队都要进行标记啊!!一定要记住啊!!!)后来又是跳出循环break的位置写错了啊,又是WA啊,写了前前后后八九个小时啊!不停地出错啊、、、菜鸟伤不起啊,智商真是着急啊、、、

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

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

#include <stdio.h>
#include <string.h>
#define MAX 110
#define MAXN 1100000
struct node
{
    int x1, y1;
} bfs[MAXN];
int num[MAXN];
int map[MAX][MAX], dp[MAX][MAX], f[MAX][MAX];
char s[MAX][MAX];
int main()
{
    int i, j, t, tt, n, m, k, x, y, a, b, p, q;
    int lcnt, rcnt, cnt;
    scanf("%d",&k);
    while(k--)
    {
        memset(dp , -1 , sizeof(dp));
        memset(f , -1 , sizeof(dp));
        memset(map , -1 , sizeof(map));
        lcnt = 1;
        rcnt = 1;
        cnt = 0;
        scanf("%d %d",&m, &n);
        for(i = 1; i <= n; i++)
            scanf("%s",s[i]);
        for(i = 1; i <= n; i++)
            for(j = 0; j < m; j++)
            {
                if(s[i][j] == 'S')
                {
                    map[i][j+1] = 0;
                    x = i;
                    y = j+1;
                }
                else if(s[i][j] == '.')
                    map[i][j+1] = 0;
                else if(s[i][j] == '#')
                    map[i][j+1] = 1;
                else if(s[i][j] == 'E')
                {
                    map[i][j+1] = 0;
                    a = i;
                    b = j+1;
                }
                dp[i][j+1] = 0;
            }
        p = x;
        q = y;
        if(y == 1)
            f[x][y] = 2;
        else if(y == m)
            f[x][y] = 4;
        else if(x == 1)
            f[x][y] = 3;
        else if(x == n)
            f[x][y] = 1;
        while(1)
        {
            if(f[x][y] == 1)
            {
                if(map[x][y-1] == 0)
                {
                    y -= 1;
                    f[x][y] = 4;
                    lcnt++;
                }
                else
                {
                    if(map[x-1][y] == 0)
                    {
                        x -= 1;
                        f[x][y] = 1;
                        lcnt++;
                    }
                    else
                        f[x][y] = 2;//向右转
                }
            }
            else if(f[x][y] == 2)
            {
                if(map[x-1][y] == 0)
                {
                    x -= 1;
                    f[x][y] = 1;
                    lcnt++;
                }
                else
                {
                    if(map[x][y+1] == 0)
                    {
                        y += 1;
                        f[x][y] = 2;
                        lcnt ++;
                    }
                    else
                        f[x][y] = 3;
                }
            }
            else if(f[x][y] == 3)
            {
                if(map[x][y+1] == 0)
                {
                    y += 1;
                    f[x][y] = 2;
                    lcnt++;
                }
                else
                {
                    if(map[x+1][y] == 0)
                    {
                        x += 1;
                        f[x][y] = 3;
                        lcnt++;
                    }
                    else
                        f[x][y] = 4;
                }
            }
            else if(f[x][y] == 4)
            {
                if(map[x+1][y] == 0)
                {
                    x += 1;
                    f[x][y] = 3;
                    lcnt++;
                }
                else
                {
                    if(map[x][y-1] == 0)
                    {
                        y -= 1;
                        f[x][y] = 4;
                        lcnt++;
                    }
                    else
                        f[x][y] = 1;
                }
            }
            if(a == x && b == y)
                break;
        }
        x = p, y = q;
        while(1)
        {
            if(f[x][y] == 1)
            {
                if(map[x][y+1] == 0)
                {
                    y += 1;
                    f[x][y] = 2;
                    rcnt++;
                }
                else
                {
                    if(map[x-1][y] == 0)
                    {
                        x -= 1;
                        f[x][y] = 1;
                        rcnt++;
                    }
                    else
                        f[x][y] = 4;//向左转
                }
            }
            else if(f[x][y] == 2)
            {
                if(map[x+1][y] == 0)
                {
                    x += 1;
                    f[x][y] = 3;
                    rcnt++;
                }
                else
                {
                    if(map[x][y+1] == 0)
                    {
                        y += 1;
                        f[x][y] = 2;
                        rcnt ++;
                    }
                    else
                        f[x][y] = 1;
                }
            }
            else if(f[x][y] == 3)
            {
                if(map[x][y-1] == 0)
                {
                    y -= 1;
                    f[x][y] = 4;
                    rcnt++;
                }
                else
                {
                    if(map[x+1][y] == 0)
                    {
                        x += 1;
                        f[x][y] = 3;
                        rcnt++;
                    }
                    else
                        f[x][y] = 2;
                }
            }
            else if(f[x][y] == 4)
            {
                if(map[x-1][y] == 0)
                {
                    x -= 1;
                    f[x][y] = 1;
                    rcnt++;
                }
                else
                {
                    if(map[x][y-1] == 0)
                    {
                        y -= 1;
                        f[x][y] = 4;
                        rcnt++;
                    }
                    else
                        f[x][y] = 3;
                }
            }
            if(x == a && y == b)
                break;
        }
        t = 0 ;
        tt = 0;
        bfs[t].x1 = p;
        bfs[t].y1 = q;
        num[t] = 1;
        dp[bfs[t].x1][bfs[t].y1] = 1;
        while(1)
        {

            if(map[bfs[t].x1][bfs[t].y1-1] == 0 && dp[bfs[t].x1][bfs[t].y1-1] == 0)//左边
            {
                tt++;
                bfs[tt].x1 = bfs[t].x1;
                bfs[tt].y1 = bfs[t].y1-1;
                num[tt] = num[t]+1;
                if(bfs[tt].x1 == a && bfs[tt].y1 == b)
                    break;
                dp[bfs[tt].x1][bfs[tt].y1] = 1;
            }
            if(map[bfs[t].x1][bfs[t].y1+1] == 0 && dp[bfs[t].x1][bfs[t].y1+1] == 0)//右边
            {
                tt++;
                bfs[tt].x1 = bfs[t].x1;
                bfs[tt].y1 = bfs[t].y1+1;
                num[tt] = num[t]+1;
                if(bfs[tt].x1 == a && bfs[tt].y1 == b)
                    break;
                dp[bfs[tt].x1][bfs[tt].y1] = 1;
            }
            if(map[bfs[t].x1-1][bfs[t].y1] == 0 && dp[bfs[t].x1-1][bfs[t].y1] == 0)//上边
            {
                tt++;
                bfs[tt].x1 = bfs[t].x1-1;
                bfs[tt].y1 = bfs[t].y1;
                num[tt] = num[t]+1;
                if(bfs[tt].x1 == a && bfs[tt].y1 == b)
                    break;
                dp[bfs[tt].x1][bfs[tt].y1] = 1;
            }
            if(map[bfs[t].x1+1][bfs[t].y1] == 0 && dp[bfs[t].x1+1][bfs[t].y1] == 0)//下边
            {
                tt++;
                bfs[tt].x1 = bfs[t].x1+1;
                bfs[tt].y1 = bfs[t].y1;
                num[tt] = num[t]+1;
                if(bfs[tt].x1 == a && bfs[tt].y1 == b)
                    break;
                dp[bfs[tt].x1][bfs[tt].y1] = 1;
            }
            t++;
        }
        printf("%d %d %d\n",lcnt, rcnt, num[tt]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值