HDU 4445 Running Rabbits(模拟)

来源:互联网 发布:dns域名系统 编辑:程序博客网 时间:2024/06/02 06:01

Running Rabbits

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


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


题意:这题题目一定得好好读,因为有很多细节,如果全部读懂了就很简单了,就是一道普通的模拟。有两只兔子,Tom跟Jerry给定他们的初始位置、初始方向、转向的周期,比如周期为二那么在2、4、6时刻兔子就会向当前方向的左侧转,如果兔子到达边界时就会反向,如果两只兔子在k时刻相遇(在k-1到k期间相遇不算)那么兔子相互交换
方向。但是有个优先级关系,如果在k时刻两只兔子相遇也需要转向,那么他只要交换方向就可以,然后再判断是否到达边界。

解题思路:就是一个模拟题,把思路理清就好,详细见代码。

<span style="font-size:18px;">#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;struct node{   char c;   int v,time,x,y;}tom,jer;int n,k;void walk(node & a){    if(a.c=='N'){        if(a.v<=a.y-1) a.y=a.y-a.v;        else{            a.c='S';            a.y=1+(a.v-(a.y-1));        }    }    else if(a.c=='S'){        if(a.v<=n-a.y) a.y+=a.v;        else{            a.c='N';            a.y=n-(a.v-(n-a.y));        }    }    else if(a.c=='W')    {        if(a.v<=a.x-1) a.x-=a.v;        else{            a.c='E';            a.x=1+(a.v-(a.x-1));        }    }    else{        if(a.v<=n-a.x) a.x+=a.v;        else{            a.c='W';            a.x=n-(a.v-(n-a.x));        }    }}bool panduan(node a,node b){    if(a.x==b.x && a.y==b.y) return true;    else return false;}void change(node &a){    if(a.c=='E') a.c='N';    else if(a.c=='S') a.c='E';    else if(a.c=='W') a.c='S';    else if(a.c=='N') a.c='W';}int main(){    while(~scanf("%d",&n) &&n)    {        getchar();        scanf("%c %d %d",&tom.c,&tom.v,&tom.time);        getchar();        scanf("%c %d %d",&jer.c,&jer.v,&jer.time);        scanf("%d",&k);        tom.x=1,tom.y=1;        jer.x=n,jer.y=n;        for(int i=1;i<=k;i++){            walk(tom);            walk(jer);            if(panduan(tom,jer)){                char s=tom.c;                tom.c=jer.c;                jer.c=s;            }            else{                if(i%tom.time==0){                change(tom);                }                if(i%jer.time==0){                change(jer);              }            }        //cout << tom.c << " " << jer.c << endl;        //printf("%d %d\n%d %d\n",tom.y,tom.x,jer.y,jer.x); ///        //cout <<endl;        }        printf("%d %d\n%d %d\n",tom.y,tom.x,jer.y,jer.x);    }    return 0;}</span>


0 0
原创粉丝点击