题意:一个N*N的棋盘,N<=20,两只兔子分别在左上角和右下角。兔子会跑,有四个方向,装到墙会弹回来,兔子在整点相遇会互换方向,而且每过一定的时间,兔子就会左转,(还有些细节要仔细读题)
思路:由于棋盘很小,并且告诉你兔子的速度小于边长,所以每次跑最多撞一次强,简单了。而且兔子仅仅在整点相遇才会发生互换方向,也简单了许多,只要分别的模拟,在每个小时判断一下就好了
Running Rabbits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1543 Accepted Submission(s): 1082
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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long LL;
const int INF=0x7fffffff;
const int MAX_N=10000;
//E=0,S=1,W=2,N=3
int d[4][2]={0,1,1,0,0,-1,-1,0};
int N,K;
int F[25][25];
struct node{
int speed;
int dir;
int turn;
int x,y;
};
node T,J;
bool cango(int x,int y){
return x<=N&&y<=N&&x>0&&y>0;
}
void walk(node &T){
int nx=T.x;
int ny=T.y;
nx+=d[T.dir][0]*T.speed;
ny+=d[T.dir][1]*T.speed;
if(cango(nx,ny)){
T.x=nx;
T.y=ny;
}
else{
if(nx<=0){
T.x=abs(nx)+2;
T.y=ny;
T.dir=4-T.dir;
}
else if(nx>N){
T.x=N-(nx-N);
T.y=ny;
T.dir=4-T.dir;
}
else if(ny<=0){
T.y=abs(ny)+2;
T.x=nx;
T.dir=2-T.dir;
}
else if(ny>N){
T.y=N-(ny-N);
T.x=nx;
T.dir=2-T.dir;
}
}
}
void input(){
char c;
scanf(" %c %d%d",&c,&T.speed,&T.turn);
T.x=1;
T.y=1;
if(c=='E'){
T.dir=0;
}
else if(c=='S'){
T.dir=1;
}
else if(c=='W'){
T.dir=2;
}
else if(c=='N'){
T.dir=3;
}
scanf(" %c %d%d",&c,&J.speed,&J.turn);
if(c=='E'){
J.dir=0;
}
else if(c=='S'){
J.dir=1;
}
else if(c=='W'){
J.dir=2;
}
else if(c=='N'){
J.dir=3;
}
J.x=N;
J.y=N;
scanf("%d",&K);
}
int main(){
while(scanf("%d",&N)&&N!=0){
input();
for(int i=1;i<=K;i++){
walk(T);
walk(J);
if(T.x==J.x&&T.y==J.y){
swap(T.dir,J.dir);
}
else{
if(i%T.turn==0)T.dir=(T.dir+4-1)%4;
if(i%J.turn==0)J.dir=(J.dir+4-1)%4;
}
}
printf("%d %d\n%d %d\n",T.x,T.y,J.x,J.y);
}
return 0;
}