M - Children of the Candy Corn (第一次写左右优先的搜索,自己独立写的可能不够优化)

119 篇文章 1 订阅
113 篇文章 1 订阅

M - Children of the Candy Corn

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>
#include<queue>
using namespace std;
char map[50][50];
int go[8][4][2]= {0,-1,-1,0,0,1,1,0,//shang(-1,0)   前四个是左优先的走向
                  -1,0,0,1,1,0,0,-1,//you(0,1)
                  0,1,1,0,0,-1,-1,0,//xia(1,0)
                  1,0,0,-1,-1,0,0,1,//zuo(0,-1)
                  0,1,-1,0,0,-1,1,0,//shang(-1,0)  后四个是右优先的走向
                  1,0,0,1,-1,0,0,-1,//you(0,1)
                  0,-1,1,0,0,1,-1,0,//xia(1,0)
                  -1,0,0,-1,1,0,0,1//zuo(0,-1)
                 };
int xia[50][50];
int a,b,x_e,y_e,x_s,y_s;
int fang1(int i,int j)//对每一步的判断  左优先的
{
    if(i==0&&j==-1)
        return 3;
    if(i==0&&j==1)
        return 1;
    if(i==1&&j==0)
        return 2;
    if(i==-1&&j==0)
        return 0;
}
int fang2(int i,int j)//对每一步的判断  右优先的
{
    if(i==0&&j==-1)
        return 7;
    if(i==0&&j==1)
        return 5;
    if(i==1&&j==0)
        return 6;
    if(i==-1&&j==0)
        return 4;
}
int dfs(int i,int j,int x1,int y1,int step,int f)//深搜 对左右优先的搜索  f是判断:左优先还是右优先
{
    int h;
    if(f==1)         //左优先的
        h=fang1(i,j);
    if(f==2)         //右优先的
        h=fang2(i,j);
    if(x1==x_e&&y1==y_e)
        return step;
    for(int k=0;k<4;k++)
    {
        int xx=x1+go[h][k][0],yy=y1+go[h][k][1];
        if(xx>=0&&xx<a&&yy>=0&&yy<b&&map[xx][yy]!='#')
        {
            dfs(go[h][k][0],go[h][k][1],xx,yy,step+1,f);
            break;//只用搜一步就行,不用搜剩下的方向
        }
    }
}
struct node
{
    int x,y,step;
};
queue<node>q;
int bfs()    //广搜,搜最短路径
{
    node n1,n2;
    n1.x=x_s,n1.y=y_s,n1.step=1;
    xia[x_s][y_s]=1;
    q.push(n1);
    while(!q.empty())
    {
        n1=q.front();
        if(n1.x==x_e&&n1.y==y_e)
            break;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=n1.x+go[0][i][0],yy=n1.y+go[0][i][1];
            if(xx>=0&&xx<a&&yy>=0&&yy<b&&xia[xx][yy]==0&&map[xx][yy]!='#')
            {
                xia[xx][yy]=1;
                n2.x=xx,n2.y=yy,n2.step=n1.step+1;
                q.push(n2);
            }
        }
    }
    while(!q.empty())
        q.pop();
    return n1.step;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        memset(xia,0,sizeof(xia));
        int i,j;
        scanf("%d%d",&b,&a);
        for(i=0; i<a; i++)
        {
            scanf("%s",map[i]);
            for(j=0; j<b; j++)
            {
                if(map[i][j]=='E')
                    x_e=i,y_e=j;
                if(map[i][j]=='S')
                    x_s=i,y_s=j;
            }
        }
        int zuo=dfs(0,-1,x_s,y_s,1,1);//左优先的搜索
        int you=dfs(0,1,x_s,y_s,1,2);//右优先的搜索
        int zui=bfs();//最短步的搜索
        printf("%d %d %d\012",zuo,you,zui);
    }
    return 0;
}

有解释:点击打开链接

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[44][44];
char s[44][44];
int n,m,ans;//cont记录步数,flag判断是否已找到结果,ans为最短的步数
int dir[][2]={{0,-1},{-1,0},{0,1},{1,0}};//顺时针
struct node
{
    int x,y,step;
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='#')
        return 1;
    return 0;
}
int dfs(int x1,int y1,int x2,int y2,int d,int step)
{
    if(x1==x2&&y1==y2)
        return step;
    d=(d+3)%4;//在上一个方向的基础上,左转90度到优先位置
    for(int i=d;i<d+4;i++)
    {
        int xx=x1+dir[i%4][0],yy=y1+dir[i%4][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]!='#')
        {
            dfs(xx,yy,x2,y2,i,step+1);
            break;
        }
    }
}
void bfs(int x1,int y1)
{
    memset(vis,0,sizeof(vis));
    node now,next;
    queue<node>q;
    now.x=x1,now.y=y1,now.step=1;
    vis[x1][y1]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        if(s[now.x][now.y]=='E')
        {
            printf("%d\n",now.step);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            int x=now.x+dir[i][0],y=now.y+dir[i][1];
            if(judge(x,y))
            {
                vis[x][y]=1;
                next.x=x,next.y=y,next.step=now.step+1;
                q.push(next);
            }
        }
        q.pop();
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        int i,j,x1,y1,x2,y2;
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='S')
                    x1=i,y1=j;
                else if(s[i][j]=='E')
                    x2=i,y2=j;
            }
        }
        printf("%d ",dfs(x1,y1,x2,y2,0,1));
        printf("%d ",dfs(x2,y2,x1,y1,0,1));
        bfs(x1,y1);
    }
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值