poj 3083 Children of the Candy Corn——dfs与bfs的混合

Children of the Candy Corn

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

题意: 走迷宫,第一个数输出左优先搜索的路径长度,第二个数输出右优先搜索的路径长度,第三次输出最短路径长度。

题解:

先处理简单的,求最短路径长度,直接一个bfs就出来结果了。我们再来看左优先搜索的路径长度,左优先搜索的顺序是 左 → 上 → 右 → 下 左 \rightarrow上\rightarrow右\rightarrow下 ,右优先搜索的顺序是 右 → 上 → 左 → 下 右\rightarrow上\rightarrow左\rightarrow下 。即左优先搜索从左边顺时针搜索,右优先搜索从右边逆时针搜索。知道原理了,我们接着来把这个原理转换成数学公式。
在这里插入图片描述
  我们假设起点是在最右边,按照左优先搜索的顺序,程序得要先往起点的左边开始搜寻,即按照上图的箭头的从左往右的方向搜索(起点的左边即垂直往下)。我们假设起点的这个方位为 t ,起点每转向一次,t 都加1(加1即代表方向向左改变了一次)。假设程序中的设置转向的坐标是这样的 int dl[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};,对应着图中的点应该首先是向下转,所以对应的移动坐标应该是{1,0},即行加 1,而列的坐标值保持不变。那么我们看到对应的 dl 中的下标索引应该是dl[3],这时将这个值与当前坐标值相加就能实现相对起点位置的左转向。我们要将 t 这个数值和dl的下标联系起来,也就是 t → d [ y ] t\rightarrow d[y] td[y] 这里存在一个函数关系我们要自己找出来(其中y是关于t的函数)。我们这里直接给出关系 ((t - 1 + 3)%4 + i)%4),我们可以直接令 t 等于0。右优先搜索关系式是相同的,不过搜索的顺序不一样(即上图t、t+1、t+2、t+3,在矩形的边上的位置会变动,原理还是一样)。
 
  求出这一层关系我们就可以用dfs来直接求解了。

c++ AC 代码

#include<cstdlib>
#include<cstdio>
#include<queue>
#include<map>
#include<cstring>
#include<algorithm>
#define size 45

char g[size][size];
int d[size][size];
int vis[size][size];
int dx[4] = {0,-1,1,0};
int dy[4] = {1,0,0,-1};

int dl[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int dr[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};

int sx,sy,ex,ey,d1,d2,h,w,ans;

int dfs(int x, int y, int d, int ans, int dp[][2]);
int bfs(int x, int y);

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&w,&h);
        for(int i=0;i<h;i++)
        {
            scanf("%s",g[i]);
            for(int j=0;j<w;j++)
            {
                if(g[i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                }
                else if(g[i][j] == 'E')
                {
                    ex = i;
                    ey = j;
                }
            }
        }
        if(sx == 0) {
            d1 = 3;d2 = 3;
        }
        else if(sx == h - 1)
        {
            d1 = 1;d2 = 1;
        }
        else if(sy == 0)
        {
            d1 = 2;
            d2 = 0;
        }
        else if(sy == w - 1)
        {
            d1 = 0;
            d2 = 2;
        }
        ans = 0;
        printf("%d ",dfs(sx,sy,d1,1,dl));
        ans = 0;
        printf("%d ",dfs(sx,sy,d2,1,dr));
        printf("%d\n",bfs(sx,sy));
    }
    system("pause");
    return 0;
}

int dfs(int x,int y,int d,int ans,int dp[][2])
{
    for(int i=0;i<4;i++)
    {
        int j = ((d + 3)%4 + i)%4;
        int xi = dp[j][0] + x;
        int yi = dp[j][1] + y;
        if(xi == ex && yi == ey) return ans+1;
        if(0 <= xi && xi < h && 0 <= yi && yi < w && g[xi][yi] != '#')
            return dfs(xi,yi,j,ans+1,dp);
    }
}

int bfs(int x,int y)
{
    std::queue<std::pair<int,int> > que;
    std::pair<int,int> co;
    for(int i=0;i<size;i++)
    {
        std::fill(d[i],d[i]+size,0);
        std::fill(vis[i],vis[i]+size,0);
    }
    vis[x][y] = 1;
    d[x][y] = 1;
    que.push(std::make_pair(x,y));
    while(!que.empty())
    {
        std::pair<int,int> p = que.front();
        que.pop();
        if(p.first == ex && p.second == ey) return d[ex][ey];
        for(int i=0;i<4;i++)
        {
            co.first = p.first + dx[i];
            co.second = p.second + dy[i];
            if(0 <= co.first && co.first < h && co.second >= 0 && co.second < w && (g[co.first][co.second] != '#') && !vis[co.first][co.second])
            {
                d[co.first][co.second] = d[p.first][p.second] + 1;
                vis[co.first][co.second] = 1;
                que.push(co);
            }
        }
    }
}

参考自:https://blog.csdn.net/qq_43690454/article/details/97180370
文中多有描述含糊,实属自己也没有搞得太清楚,等搞清楚了再来补充(主要是那个dfs的那个推导的数学算式)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值