hdu 4452 Running Rabbits(模拟水题)

来源:互联网 发布:微信公众号开发java 编辑:程序博客网 时间:2024/05/16 15:53

Running Rabbits

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


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
 

Source
2012 Asia JinHua Regional Contest
 

Recommend
zhuyuanchen520
 

题意:一个N*N的正方形田野分成1*1的格子,有两只兔子,Tom在左上角(1, 1),Jerry在右下角(N, N),它们可以沿东南西北4个方向走,但不可出格,各有各的速度,相遇时,两只兔子交换移动方向,碰边界时反向,各自还有自己的转左周期,周期一到即转左(但此时两只兔子相遇就不执行此周期转左),问K小时后两只兔子的坐标。

同样的模拟题,不同的人用的方法也有差异。

#include <iostream>#include<string.h>#include<stdio.h>using namespace std;int dx[4]={1,0,-1,0};//对应四个方向E,N,W,S。int dy[4]={0,-1,0,1};int xt,yt,tt,dt,spt,xj,yj,tj,dj,spj,n,k;//Tom横纵坐标,转向间隔,运动方向,运动速度。Jerry的信息int dre(char co)//建立方向映射{    switch(co)    {        case 'E':return 0;        case 'N':return 1;        case 'W':return 2;        case 'S':return 3;    }    return 0;}void out(int* x,int* y,int* dr)//越界判断{    if(*x>n)        *x=n-(*x-n);    else if(*x<1)        *x=2-*x;    else if(*y>n)        *y=n-(*y-n);    else if(*y<1)        *y=2-*y;    else        return;    *dr=(*dr+2)%4;//反向}int main(){    char com[10];    int t,i;    while(scanf("%d",&n),n)    {        scanf("%s%d%d",com,&spt,&tt);        dt=dre(com[0]);        scanf("%s%d%d",com,&spj,&tj);        dj=dre(com[0]);        scanf("%d",&k);        xt=yt=1;        xj=yj=n;        for(i=1;i<=k;i++)        {            xt+=dx[dt]*spt;            yt+=dy[dt]*spt;            out(&xt,&yt,&dt);            xj+=dx[dj]*spj;            yj+=dy[dj]*spj;            out(&xj,&yj,&dj);            if(xt!=xj||yt!=yj)            {                if(i%tt==0)//到了转向间隔转向                  dt=(dt+1)%4;                if(i%tj==0)                  dj=(dj+1)%4;            }            else//相遇            {                t=dt;                dt=dj;                dj=t;            }        }        printf("%d %d\n",yt,xt);        printf("%d %d\n",yj,xj);    }    return 0;}