模拟_模拟迷宫走势(HDU_4740)

http://acm.hdu.edu.cn/showproblem.php?pid=4740

 

  

#include <stdio.h>
#include <string.h>

#define M 1005

int n;
int Dx,Dy,Ds,Gx,Gy,Gs;
int Df[M][M],Gf[M][M];
const int move[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int judEdge(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= n)    return 0;
    return 1;
}

void Dturn()
{
    Ds = (Ds + 1) % 4;
}

void Gturn()
{
    Gs = (Gs + 4 - 1) % 4;
}

int Dmove()
{
    int flag = 1;
    while(1)
    {
        int Dx_ = Dx + move[Ds][0];
        int Dy_ = Dy + move[Ds][1];
        if(judEdge(Dx_,Dy_) && !Df[Dx_][Dy_])
        {
            Dx = Dx_;
            Dy = Dy_;
            Df[Dx][Dy] = 1;
            return 1;
        }
        else
        {
            Dturn();
            if(flag) flag = 0;
            else break;
        }
    }
    return 0;
}

int Gmove()
{
    int flag = 1;
    while(1)
    {
        int Gx_ = Gx + move[Gs][0];
        int Gy_ = Gy + move[Gs][1];
        if(judEdge(Gx_,Gy_) && !Gf[Gx_][Gy_])
        {
            Gx = Gx_;
            Gy = Gy_;
            Gf[Gx][Gy] = 1;
            return 1;
        }
        else
        {
            Gturn();
            if(flag) flag = 0;
            else break;
        }
    }
    return 0;
}


void run()
{
    int Dcm = 1,Gcm = 1;
    while(1)
    {
        if(Dx == Gx && Dy == Gy)
        {
            printf("%d %d\n",Dx,Dy);
            return ;
        }
        if(!Dcm && !Gcm)
        {
            printf("-1\n");
            return ;
        }
        if(Dcm)    Dcm = Dmove();
        if(Gcm)    Gcm = Gmove();
    }
}

int main(int argc, char* argv[])
{
#ifdef __MYLOCAL
    freopen("in.txt","r",stdin);
#endif
    
    while(scanf("%d",&n) && n)
    {
        scanf("%d%d%d",&Dx,&Dy,&Ds);
        scanf("%d%d%d",&Gx,&Gy,&Gs);
        memset(Df,0,sizeof(Df));
        memset(Gf,0,sizeof(Gf));
        Df[Dx][Dy] = Gf[Gx][Gy] = 1;
        run();
    }
    
    return 0;
}

 

转载于:https://www.cnblogs.com/lk1993/p/3354308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值