HDU Running Rabbits(模拟)

/*
模拟题,做的比较有感觉
以前不太擅长做模拟题,因为总是模拟中找公式,所以往往会复杂很多
①处,原来想直接用公式求出下一步坐标,结果太麻烦。
后来直接用一个for()循环实现,简单很多
*/
#include <cstdio>
int N;
void fgo(int &d, int &x, int &y, int s)
{
    int i;
    switch(d)
    {
        case 1:
        case 3:
        for(i = 0; i < s; ++ i)//①
        {
            y += (d == 1 ? -1 : 1);
            if(y < 1)
            {
                y = 2;
                d = 3;
            }
            else if(y > N)
            {
                y = N - 1;
                d = 1;
            }
        }
        break;
        case 0:
        case 2:
        for(i = 0; i < s; ++ i)
        {
            x += (d == 0 ? -1 : 1);
            if(x < 1)
            {
                x = 2;
                d = 2;
            }
            else if(x > N)
            {
                x = N - 1;
                d = 0;
            }
        }
        break;
    }
}

void convert(char c, int &d)
{
    switch(c)
    {
        case 'N':
        d = 0;
        break;
        case 'W':
        d = 1;
        break;
        case 'S':
        d = 2;
        break;
        case 'E':
        d = 3;
        break;
    }
}

int main()
{
    while(scanf("%d", &N) != EOF)
    {
        if(!N) break;
        char c1[5], c2[5];
        int s1, t1, s2, t2;
        scanf("%s%d%d%s%d%d", c1, &s1, &t1, c2, &s2, &t2);
        int k;
        scanf("%d", &k);
        int i;
        int x1, y1, x2, y2;
        x1 = y1 = 1;
        x2 = y2 = N;
        int d1, d2;
        convert(c1[0], d1);
        convert(c2[0], d2);
        for(i = 1; i <= k; ++ i)
        {
            fgo(d1, x1, y1, s1);
            fgo(d2, x2, y2, s2);
            if(x1 == x2 && y1 == y2)
            {
                int temp = d1;
                d1 = d2;
                d2 = temp;
                continue;
            }
            if(i % t1 == 0)
            d1 = (d1 + 1) % 4;
            if(i % t2 == 0)
            d2 = (d2 + 1) % 4;
        }
        printf("%d %d\n%d %d\n", x1, y1, x2, y2);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值