HDU 4452 Running Rabbits

简单模拟

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <string>
using namespace std;

int dir[4][2];

struct X
{
    int dir;
    int nowx;
    int nowy;
    int speed;
    int t;
}Tom,Jerry;
int N,K;

void init()
{
    dir[0][0]=-1; dir[0][1]=0;
    dir[1][0]=0; dir[1][1]=-1;
    dir[2][0]=+1; dir[2][1]=0;
    dir[3][0]=0; dir[3][1]=+1;
}

void read()
{
    char s[5];
    scanf("%s",s);
    if(s[0]=='N') Tom.dir=0;
    if(s[0]=='W') Tom.dir=1;
    if(s[0]=='S') Tom.dir=2;
    if(s[0]=='E') Tom.dir=3;
    Tom.nowx=1; Tom.nowy=1;

    scanf("%d%d",&Tom.speed,&Tom.t);

    scanf("%s",s);
    if(s[0]=='N') Jerry.dir=0;
    if(s[0]=='W') Jerry.dir=1;
    if(s[0]=='S') Jerry.dir=2;
    if(s[0]=='E') Jerry.dir=3;
    Jerry.nowx=N; Jerry.nowy=N;

    scanf("%d%d",&Jerry.speed,&Jerry.t);
}

bool meet()
{
    if(Tom.nowx==Jerry.nowx&&Tom.nowy==Jerry.nowy) return 1;
    return 0;
}

void TurnTom()
{
    Tom.dir=(Tom.dir+1)%4;
}

void TurnJerry()
{
    Jerry.dir=(Jerry.dir+1)%4;
}


void Swap()
{
    swap(Tom.dir,Jerry.dir);
}

bool f(int x,int y)
{
    if(x>=1&&x<=N&&y>=1&&y<=N) return 1;
    return 0;
}
void runTom()
{
    for(int i=1;i<=Tom.speed;i++)
    {
        int NewX=Tom.nowx+dir[Tom.dir][0];
        int NewY=Tom.nowy+dir[Tom.dir][1];
        if(f(NewY,NewX))
        {
            Tom.nowx=NewX;
            Tom.nowy=NewY;
        }
        else
        {
            Tom.dir=(Tom.dir+2)%4;
            NewX=Tom.nowx+dir[Tom.dir][0];
            NewY=Tom.nowy+dir[Tom.dir][1];
            Tom.nowx=NewX;
            Tom.nowy=NewY;
        }
    }
}

void runJerry()
{
    for(int i=1;i<=Jerry.speed;i++)
    {
        int NewX=Jerry.nowx+dir[Jerry.dir][0];
        int NewY=Jerry.nowy+dir[Jerry.dir][1];
        if(f(NewY,NewX))
        {
            Jerry.nowx=NewX;
            Jerry.nowy=NewY;
        }
        else
        {
            Jerry.dir=(Jerry.dir+2)%4;
            NewX=Jerry.nowx+dir[Jerry.dir][0];
            NewY=Jerry.nowy+dir[Jerry.dir][1];
            Jerry.nowx=NewX;
            Jerry.nowy=NewY;
        }
    }
}


int main()
{
    init();
    while(~scanf("%d",&N))
    {
        if(!N) break;
        read();
        scanf("%d",&K);
        for(int i=0;i<K;i++)
        {
            if(meet()) Swap();
            else
            {
                if(i%Tom.t==0&&i!=0) TurnTom();
                if(i%Jerry.t==0&&i!=0) TurnJerry();
            }

            runTom();
            runJerry();
          //   printf("--- %d %d\n",Tom.nowx,Tom.nowy);
         //   printf("--- %d %d\n",Jerry.nowx,Jerry.nowy);
          //  printf("----\n");
        }
        printf("%d %d\n",Tom.nowx,Tom.nowy);
        printf("%d %d\n",Jerry.nowx,Jerry.nowy);
    }
    return 0;
}

 

posted @ 2016-01-27 20:02  Fighting_Heart  阅读(132)  评论(0编辑  收藏  举报