HDU2364

看了别人的解,然后自己尝试解,感觉还非常不熟悉。

/*************************************************************************
    > File Name: main.cpp
    > Author:Eagles 
    > Mail:None 
    > Created Time: 2018年08月26日 星期日 14时42分16秒
    > Description:HDU2364 
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int sx,sy;
int h,w;
#define N 88
char maze[N][N];
bool vis[N][N][4];
struct node
{
    int dx;
    int dy;
}dir[4];//0,1为左右,2,3为上下

struct the_dot
{
    int x,y,dir;
    int step;
}dot;

bool check(int x1, int y1, int x2, int y2)//判断是否可以拐弯
{
   if ((maze[x1][y1]=='#')&&maze[x2][y2]=='#')
    {
        return false;
    }
    return true;
}

bool judge(int x, int y, int dir)//判断是否合法、是否访问过
{
    return (x>=0&&x<h&&y>=0&&y<w&&!vis[x][y][dir]&&maze[x][y]!='#');
}

void bfs()
{
    memset(vis,false,sizeof(vis));

    queue<the_dot>Q;
    dot.x=sx;
    dot.y=sy;
    dot.step=0;
    dot.dir=-1;

    for (int i=0; i<4; i++)
        vis[sx][sy][i]=true;

    Q.push(dot);

    the_dot cur;
    the_dot next;

    while (!Q.empty())
    {
        int flag=0;
        cur=Q.front();

        Q.pop();
        if (cur.x==0||cur.x==h-1||cur.y==0||cur.y==w-1)
        {
            printf("%d\n",cur.step);
            return;
        }

        int x1,y1,x2,y2;

        if (cur.dir==0||cur.dir==1)//如果在左右走
        {
            x1=cur.x+dir[2].dx;
            y1=cur.y+dir[2].dy;
            x2=cur.x+dir[3].dx;
            y2=cur.y+dir[3].dy;

            if (check(x1,y1,x2,y2))
            {
                for (int i=2; i<4; i++)
                {
                    next.x=cur.x+dir[i].dx;
                    next.y=cur.y+dir[i].dy;
                    next.dir=i;
                    next.step=cur.step+1;

                    if (judge(next.x,next.y,next.dir))
                    {
                        vis[next.x][next.y][next.dir]=true;
                        Q.push(next);
                    }
                }

            }
            else
                flag=1;
        }
        else if (cur.dir==2||cur.dir==3)//如果上下走
        {
            x1=cur.x+dir[0].dx;
            y1=cur.y+dir[0].dy;
            x2=cur.x+dir[1].dx;
            y2=cur.y+dir[1].dy;

            if (check(x1,y1,x2,y2))
            {
                for (int i=0; i<2; i++)
                {
                    next.x=cur.x+dir[i].dx;
                    next.y=cur.y+dir[i].dy;
                    next.dir=i;
                    next.step=cur.step+1;

                    if (judge(next.x,next.y,next.dir))
                    {
                        vis[next.x][next.y][next.dir]=true;
                        Q.push(next);
                    }
                }

            }
            else
                flag=1;
        }
        else if (cur.dir==-1)
        {
            for (int i=0; i<4; i++)
            {
                next.x=cur.x+dir[i].dx;
                next.y=cur.y+dir[i].dy;
                next.step=cur.step+1;
                next.dir=i;
                if (judge(next.x,next.y,next.dir))
                {
                    vis[next.x][next.y][next.dir]=true;
                    Q.push(next);
                }
            }
        }

        if (flag==1)
        {
            next.x=cur.x+dir[cur.dir].dx;
            next.y=cur.y+dir[cur.dir].dy;
            next.dir=cur.dir;
            next.step=cur.step+1;
            if (judge(next.x,next.y,next.dir))
            {
                vis[next.x][next.y][next.dir]=true;
                Q.push(next);
            }
        }

    }
    printf("-1\n");
    }
int main()
{
    dir[0].dx=-1;
    dir[0].dy=0;
    dir[1].dx=1;
    dir[1].dy=0;
    dir[2].dx=0;
    dir[2].dy=-1;
    dir[3].dx=0;
    dir[3].dy=1;

    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&h,&w);
        getchar();
        for (int i=0; i<h; i++)
        {
            for (int j=0; j<w; j++)
            {
                scanf("%c",&maze[i][j]);
                if (maze[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
            }    
            getchar();
        }
        bfs();
    }
    return 0;
}

还要努力……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值