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 (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.
![](https://i-blog.csdnimg.cn/blog_migrate/ec5c072eb30af78d30aa57f3eba6f259.jpeg)
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.
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
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
Recommend
zhuyuanchen520
题意:两只兔子。给定一个N*N的格子。初始时兔子1在(1,1),兔子2在(n,n)。兔子每隔一段时间T左转。
1、如果两只兔子刚好跳到同一格而兔子到了该转弯的时间,则不按规定左转,而是两只兔子互换跳动方向。
2、如果兔子碰到墙不能继续前进,则转换为相反的方向。例如:南就变北,东就变西。
输入包括:第一行,n,表示格子的规模。
第二行,兔子1的初始运动方向,S=速度(每小时走几格),T=每隔多长时间转换方向.
第三行,兔子2.。。。
第四行,TIME(问TIME后两只兔子所在的位置)
分析:1、用while循环没到TIME就一直按规定running和change direction。写两个函数go()和change().每次以小时为单位,分开操作。
走完后就判断是不是碰到另一只兔子,判断是否到了转弯的时间。
2、到了TIME,就输出坐标。
3、go()函数:用for循环,每次走一步,直到把一小时可以走的步数走完。如果碰到墙不能走,则变为反方向运动。用tim1、tim2分别计时。
这样就可以在主函数中用取余操作判断是否到了该转弯的时间。参数都传引用。
4、change()是单纯的按规定左转操作函数。
感想:模拟啊模拟。我哭。。。
其实都分析到了,只是我有个地方搞错了,我想的是每次走一步,分开操作,就判断是否两只兔子相碰,碰则交换运动方向;
判断是否撞到墙不能走了。然后又要顾及到有没有到规定的转动时间,还有顾及到有没有到TIME。然后就凌乱了。
基础不好,太弱了。。。囧。。。!!!!我这个逻辑拙计的孩子。。。
唯一让我得意的就是想到宏定义E、W、S、N作为下标方便坐标变换,这个很方便。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 0
#define W 1
#define S 2
#define E 3
using 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;
}