模拟_模拟迷宫走势(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;
}

 

posted on 2013-10-06 17:20  lk1993  阅读(291)  评论(0编辑  收藏  举报