Running Rabbits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 607 Accepted Submission (s): 432
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 ctions (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 ction, and Jerry also will change his direction into Tom's original direction. this direction changing is before the judging of whether they shocould 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 shoshould 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 ction 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 shoshould 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
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
Sample Output
2 2
3 3
2 1
2 4
3 1
4 1
Source
2012 Asia JinHua Regional Contest
Recommend
Zhuyuanchen520
Question: two rabbits. Given a grid of N * N. Initially, rabbit 1 is at (), and rabbit 2 is at (n, n ). The rabbit turns left at intervals T.
1. If the two rabbits just jumped to the same frame and the rabbit arrived at the turning time, they would not turn left as required, but they would switch the beating direction.
2. If the rabbit encounters a wall and cannot move forward, it is switched to the opposite direction. For example, the South is changed to the north, and the East is changed to the west.
The input includes the first line, n, indicating the size of the grid.
In the second row, the initial movement direction of rabbit 1, S = speed (several cells per hour), and T = the time at which the direction is converted.
Row 3: Rabbit 2 ....
Row 4, TIME (the location of the last two rabbits)
Analysis: 1. If the while loop is used, the running and change ction rules are followed until the TIME is reached. Write two functions go () and change (). Each operation is separated by hour.
After finishing the process, you can determine whether you have met another rabbit and whether it has reached the Turning time.
2. At TIME, the coordinates are output.
3. go () function: Use a for loop to take a step until the number of steps that can be taken in one hour is completed. If a wall cannot be moved, the system changes to backward motion. Tim1 and tim2 are used for timing.
In this way, you can use the remainder operation in the main function to determine whether the Turning time has been reached. All parameters are referenced.
4. change () is a simple left turn operation function.
Feeling: simulate it. I cried...
In fact, all of them have been analyzed, but I have made a mistake. What I think is that every time I take a step and separate the operations, I will judge whether the two rabbits will touch each other and exchange the movement direction;
Determine whether a wall is hit or not. Then we need to take into account whether there is a specified rotation TIME, and whether there is TIME. Then it's messy.
Poor Foundation, too weak... Success ...!!!! I am a child with poor logic...
The only thing I'm proud of is that I think of macro definition E, W, S, and N as subscripts to facilitate coordinate transformation, which is very convenient.
Code:
#include<cstdio>#include<iostream>#include<cstring>#define N 0#define W 1#define S 2#define E 3using namespace std;int dx[4]={-1,0,1,0};int dy[4]={0,-1,0,1};int n;void change(char &dir){ if(dir=='E') dir='N'; else if(dir=='N') dir='W'; else if(dir=='W') dir='S'; else dir='E';}void go(int &x,int &y,int &time,char &dir,int s){ for(int i=0;i<s;i++) { if(dir=='E') { if(y==n) { dir='W'; y=y+dy[W]; } else y=y+dy[E]; } else if(dir=='W') { if(y==1) { dir='E'; y=y+dy[E]; } else y=y+dy[W]; } else if(dir=='N') { if(x==1) { dir='S'; x+=dx[S]; } else x+=dx[N]; } else if(dir=='S') { if(x==n) { dir='N'; x+=dx[N]; } else x+=dx[S]; } //printf("test: %d %d\n",x,y); } time++;}int main(){ char c1,c2,tmp; int tim1,tim2,k,t1,t2,x1,y1,x2,y2,s1,s2; while(scanf("%d",&n)&&n) { getchar(); scanf("%c%d%d",&c1,&s1,&t1); getchar(); scanf("%c%d%d",&c2,&s2,&t2); scanf("%d",&k); x1=1,y1=1,x2=n,y2=n; tim1=0,tim2=0; while(k--) { go(x1,y1,tim1,c1,s1); go(x2,y2,tim2,c2,s2); if(x1==x2&&y1==y2) { tmp=c1; c1=c2; c2=tmp; } else { if(tim1%t1==0) change(c1); if(tim2%t2==0) change(c2); } } printf("%d %d\n%d %d\n",x1,y1,x2,y2); } return 0;}