POJ3083——Children of the Candy Corn

文章转自:http://blog.csdn.net/u010660276/article/details/9567783

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

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

题意:给你一个h * w的迷宫,其中有一句话需要注意一下“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.”即始点‘S’和终点‘E’一定都与迷宫的边界相邻,这对做题很关键。然后问你沿着迷宫墙壁的左边走和沿着迷宫墙壁的右边走,各需多少步,然后最少需要多少步。

 

思路:求最少步数没有什么好说当然是用bfs,重要的是求沿着迷宫墙壁走的两种状态。首先考虑,迷宫的起点是与墙壁挨着的,所以当S在迷宫最左边(sy == 0)如果沿着左边走,应该先考虑向上走的情况,如果向上走不通,在考虑像右走的情况,最后考虑向下走的情况;如果沿着右边走,应先考虑向下走的情况,再考虑向右走的情况,再考虑向上走的情况;其他的情况(sx == 0 || sy == w-1||sx == h - 1)时思路是一样的,自己推导吧~于是只要dfs枚举在每一个方向上可能有的各种情况就好了。又写了一遍这道题的代码 着道题我觉得重要的是方向的确立 与方向坐标的顺序 同时注意方向坐标 x,y分别表示的意思。 方向的确立的一种方法就是花一个正方形,若是右优先就是逆时针 ,把正方向的位子设为0 这个位置同时也是方向坐标(0,1)里面的位置,然后他的右边是1 既是上(1,0) 这样以此类推这种方向坐标位置的确立加方法无疑是这道题最精髓的东西。其实规定向左为顺时针或者逆时针是无所谓的,只要下次操作时与其一致就可以了,没必要那么纠结这个事情。 还要注意的事情就是当到底一个新的坐标的时候的时候,在这个新的坐标上也要考虑坐标移动的先后方向,按照以前设置好的方向进行考虑。 
具体代码如下:


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
    int x, y;
    int step;
};
char mp[50][50];
int flag[50][50];
int d1, d2;
int w, h;
int start[2], end[2];
int dirL[5][2] = {{0,-1},{1,0},{0,1},{-1,0}};
int dirR[5][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int dfs( int x, int y, int d, int dir[][2] )
{
    int temp, tx, ty, step;
    if( x==end[0]&&y==end[1] )
        return 1;
    for( int i = 0;i < 4;i++ )
    {
        temp = (d+i)%4;
        tx = x + dir[temp][1];
        ty = y + dir[temp][0];
        if( tx>=0&&tx<h&&ty>=0&&ty<w&&!flag[tx][ty] )
            break;
    }
    step = dfs(tx, ty, (temp+3)%4, dir) + 1;
    return step;
}

int bfs()
{
    memset(flag, 0, sizeof(flag));
    queue<struct node> q;
    while(!q.empty())
        q.pop();
    struct node p;
    p.x = start[0];
    p.y = start[1];
    p.step = 1;
    flag[p.x][p.y] = 1;
    q.push(p);
    while(!q.empty())
    {
        p = q.front();
        q.pop();
        if( p.x==end[0]&&p.y==end[1] )
            return p.step;
        for( int i = 0;i < 4;i++ )
        {
            struct node temp;
            temp.x = p.x + dirL[i][1];
            temp.y = p.y + dirL[i][0];
            if( temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&mp[temp.x][temp.y]!='#'&&!flag[temp.x][temp.y] )
            {
                flag[temp.x][temp.y] = 1;
                temp.step = p.step + 1;
                q.push(temp);
            }
        }
    }
    return 0;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(flag, 0,sizeof(flag));
        cin>>w>>h;
        for( int i = 0;i < h;i++ )
        {
            cin>>mp[i];
            for( int j = 0;j < w;j++ )
            {
                if( mp[i][j]=='#' )
                    flag[i][j] = 1;
                else if( mp[i][j]=='S' )
                {
                    start[0] = i;
                    start[1] = j;
                }
                else if( mp[i][j]=='E' )
                {
                    end[0] = i;
                    end[1] = j;
                }
            }
        }
        if( start[0]==0 )
        {
            d1 = 1;
            d2 = 1;
        }
        else if( start[0]==h-1 )
        {
            d1 = 3;
            d2 = 3;
        }
        else if( start[1]==w-1 )
        {
            d1 = 2;
            d2 = 0;
        }
        else
        {
            d1 = 0;
            d2 = 2;
        }
        cout<<dfs(start[0], start[1], d1, dirL)<<" ";
        cout<<dfs(start[0], start[1], d2, dirR)<<" ";
        cout<<bfs()<<endl;
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值