hdu 4452 Running Rabbits (模拟)

来源:互联网 发布:杨紫宁丹琳 知乎 编辑:程序博客网 时间:2024/05/16 18:24

Running Rabbits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 388    Accepted Submission(s): 284


Problem Description
      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.
 


 

Input
      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.
 


 

Output
      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
4E 1 1W 1 124E 1 1W 2 154E 2 2W 3 150
 


 

Sample Output
2 23 32 12 43 14 1

题意:有两只兔子在N*N上的区域跑,每只兔子有对应的速度和每隔T小时就会向左转。

有以下几条规定:1.兔子碰到了墙会向相反的方向跑

  2.兔子相遇的时候会交换两只兔子的方向

  3.兔子相遇后先交换方向再进行运动

根据这些规定模拟,就可以了。注意一开始的时候兔子是不会向左转的。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct  node{    char dir;    int v,t;    int x,y;};void mov(node &a,int n){    if(a.dir=='W')    {        if(a.y-a.v<=0)        {            a.dir='E';            a.y=2+a.v-a.y;        }        else        {            a.y=a.y-a.v;        }    }    else if(a.dir=='E')    {        if(a.y+a.v>n)        {            a.dir='W';            a.y=n-(a.v-(n-a.y));        }        else        {            a.y=a.y+a.v;        }    }    else if(a.dir=='N')    {        if(a.x-a.v<=0)        {            a.dir='S';            a.x=2+a.v-a.x;        }        else        {            a.x=a.x-a.v;        }    }    else if(a.dir=='S')    {        if(a.x+a.v>n)        {            a.dir='N';            a.x=n-(a.v-(n-a.x));        }        else        {            a.x=a.x+a.v;        }    }}int tog(node a,node b){    if(a.x==b.x&&a.y==b.y)        return 1;    return 0;}void turn(node &a){    if(a.dir=='W')        a.dir='S';    else if(a.dir=='S')        a.dir='E';    else if(a.dir=='E')        a.dir='N';    else if(a.dir=='N')        a.dir='W';}int main(){    int n,k;    while(cin>>n)    {        if(n==0) break;        node tom,jerry;        cin>>tom.dir>>tom.v>>tom.t;        cin>>jerry.dir>>jerry.v>>jerry.t;        cin>>k;        tom.x=1,tom.y=1;        jerry.x=n,jerry.y=n;        for(int i=1;i<=k;i++)        {            mov(tom,n);            mov(jerry,n);            if(tog(tom,jerry))            {                char cha;                cha=tom.dir;                tom.dir=jerry.dir;                jerry.dir=cha;            }            else            {                if(i%tom.t==0)                    turn(tom);                if(i%jerry.t==0)                    turn(jerry);            }        }        printf("%d %d\n",tom.x,tom.y);        printf("%d %d\n",jerry.x,jerry.y);    }    return 0;}


原创粉丝点击