HDU - 4452 Running Rabbits

传送门

问题描述:

Rabbit Tom and rabbit Jerry are running in a field. The field is an N×N grid. Tom starts from the up-left cell and Jerry starts from the down-right cell. The coordinate of the up-left cell is (1,1) and the coordinate of the down-right cell is (N,N)。A 4×4 field and some coordinates of its cells are shown below:

The rabbits can run in four directions (north, south, west and east) and they run at certain speed measured by cells per hour. The rabbits can’t get outside of the field. If a rabbit can’t run ahead any more, it will turn around and keep running. For example, in a 5×5 grid, if a rabbit is heading west with a speed of 3 cells per hour, and it is in the (3, 2) cell now, then one hour later it will get to cell (3,3) and keep heading east. For example again, if a rabbit is in the (1,3) cell and it is heading north by speed 2,then a hour latter it will get to (3,3). The rabbits start running at 0 o’clock. If two rabbits meet in the same cell at k o’clock sharp( k can be any positive integer ), Tom will change his direction into Jerry’s direction, and Jerry also will change his direction into Tom’s original direction. This direction changing is before the judging of whether they should turn around.
The rabbits will turn left every certain hours. For example, if Tom turns left every 2 hours, then he will turn left at 2 o’clock , 4 o’clock, 6 o’clock…etc. But if a rabbit is just about to turn left when two rabbit meet, he will forget to turn this time. Given the initial speed and directions of the two rabbits, you should figure out where are they after some time.

输入说明:

There are several test cases.
For each test case:
The first line is an integer N, meaning that the field is an N×N grid( 2≤N≤20).
The second line describes the situation of Tom. It is in format “c s t”。c is a letter indicating the initial running direction of Tom, and it can be ‘W’,‘E’,‘N’ or ‘S’ standing for west, east, north or south. s is Tom’s speed( 1≤s<N). t means that Tom should turn left every t hours( 1≤ t ≤1000).
The third line is about Jerry and it’s in the same format as the second line.
The last line is an integer K meaning that you should calculate the position of Tom and Jerry at K o’clock( 1 ≤ K ≤ 200).
The input ends with N = 0.

输出说明:

For each test case, print Tom’s position at K o’clock in a line, and then print Jerry’s position in another line. The position is described by cell coordinate.

SAMPLE INPUT:

4
E 1 1
W 1 1
2
4
E 1 1
W 2 1
5
4
E 2 2
W 3 1
5
0

SAMPLEOUTPUT:

2 2
3 3
2 1
2 4
3 1
4 1

思路:

一道模拟的题,题意就是给一个n*n的矩阵,然后一只兔子在左上角,坐标是(1,1),另一只在右下角,坐标是(n,n),然后告诉我们这两只兔子各自的初始运动方向,运动速度,转向的间隔(向左转),问我们k时刻这两只兔子在什么地方。代码实现是要模拟一下兔子的运动过程,重要点有两只兔子相遇时继承对方的运动方向,以及不发生转向;每过一个周期后,兔子运动方向的变化;前进到边界时,运动方向的倒转(W变为E,S变为N)建立一个二维数组表示所有的格子,再用一个二维数组去定义兔子的四种运动方向(便于计算兔子在矩阵中的运动)

AC代码:

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]= {0,1,0,-1,1,0,-1,0}; 
int n;
void gogoing(char &c,int &s,int &x,int &y,int &tim)//兔子运动的模拟
{
    int i;
    for(i=0;i<s;i++)
    {
        if(c=='E')
        {
            if(y>=n)
            {
                y-=dir[0][1];
                c='W';
            }
            else
                y+=dir[0][1];
        }
        else if(c=='W')
        {
            if(y<=1)
            {
                y-=dir[1][1];
                c='E';
            }
            else
                y+=dir[1][1];
        }
        else if(c=='S')
        {
            if(x>=n)
            {
                x-=dir[2][0];
                c='N';
            }
            else
                x+=dir[2][0];
        }
        else if(c=='N')
        {
            if(x<=1)
            {
                x-=dir[3][0];
                c='S';
            }
            else
                x+=dir[3][0];
        }
    }
    tim++;
}
void turn(char &c)//向左转向的模拟
{
    if(c=='E')
        {
            c='N';
        }
    else if(c=='W')
        {
            c='S';
        }
    else if(c=='S')
        {
            c='E';
        }
    else if(c=='N')
        {
            c='W';
        }
}
int main()
{
    int s1,s2,t1,t2,x1,x2,y1,y2,k,time1,time2;
    char c1[2],c2[2],tmp;
    while(scanf("%d",&n),n)
    {
        cin>>c1>>s1>>t1;
        cin>>c2>>s2>>t2;
        cin>>k;
        x1=1,y1=1;
        x2=n,y2=n;
        time1=time2=0;
        while(k--)
        {
            gogoing(c1[0],s1,x1,y1,time1);
            gogoing(c2[0],s2,x2,y2,time2);
            if(x1==x2&&y1==y2)//两只兔子相遇时继承对方的方向
            {//相遇是比转向更加高级的操作,优先级比转向高
                tmp=c1[0];
                c1[0]=c2[0];
                c2[0]=tmp;
            }
            else
            {
                if(time1%t1==0)
                    {
                        turn(c1[0]);
                    }
                if(time2%t2==0)
                    {
                        turn(c2[0]);
                    }
            }
        }
        cout<<x1<<' '<<y1<<endl<<x2<<' '<<y2<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值