HDU 4452 模拟
Running Rabbits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1787 Accepted Submission(s): 1252
Total Submission(s): 1787 Accepted Submission(s): 1252
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:
![](http://acm.split.hdu.edu.cn/data/images/4452-1.jpg)
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.
![](http://acm.split.hdu.edu.cn/data/images/4452-1.jpg)
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
2012 Asia JinHua Regional Contest
一个兔子在左上角,另一个兔子在右下角,给定N*N矩阵,初始方向,速度,左转周期,和过了m秒,求m秒后两个兔子的坐标。注意当兔子相遇后交换方向,此时不左转。相遇是以每小时为单位后查看是否相遇,中间(在一小时内)相遇不算不反转。
看到这个题我想到了以前做的一个搜索题,那个转向也是有头方向和四面方向的,当时学姐讲的我都没听懂,看到老胡代码豁然开朗,就是按照顺时针方向定义0,1,2,3,每次后转就直接+2,向左转+1,在定义两个x,y的移动数组对应固定北西南东四个方向,方向数组和移动数组一起用就解决这个问题了。
老胡传送门:http://www.cnblogs.com/pach/p/5779371.html
一个兔子在左上角,另一个兔子在右下角,给定N*N矩阵,初始方向,速度,左转周期,和过了m秒,求m秒后两个兔子的坐标。注意当兔子相遇后交换方向,此时不左转。相遇是以每小时为单位后查看是否相遇,中间(在一小时内)相遇不算不反转。
看到这个题我想到了以前做的一个搜索题,那个转向也是有头方向和四面方向的,当时学姐讲的我都没听懂,看到老胡代码豁然开朗,就是按照顺时针方向定义0,1,2,3,每次后转就直接+2,向左转+1,在定义两个x,y的移动数组对应固定北西南东四个方向,方向数组和移动数组一起用就解决这个问题了。
老胡传送门:http://www.cnblogs.com/pach/p/5779371.html
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int dx[]= {0,-1,0,1}; //方向顺序为N,W,S,E。这是为了方便向左转 int dy[]= {-1,0,1,0}; int n; int time; struct Coo { int x; int y; } coo[2]; struct Rabbit { int di; int v; int turn; } rabbit[2]; void solve() { coo[0].x=coo[0].y=1; coo[1].x=coo[1].y=n; for(int i=1; i<=time; i++) { bool flag1=true; for(int j=0; j<rabbit[0].v; j++) //一步一步走,不能往前走转向 { int x=coo[0].x; int y=coo[0].y; int di=rabbit[0].di;//简化代码,方便书写(听老胡是这样,代码本身没什么意义,就是偷点懒) if((x==1 && di==1) || (x==n && di==3) || (y==1 && di==0) || (y==n && di==2)) rabbit[0].di=di=(di+2)%4; //向后转,方向逆时针转向,所以直接+2 coo[0].x=x+dx[di]; coo[0].y=y+dy[di]; } for(int j=0; j<rabbit[1].v; j++) { int x=coo[1].x; int y=coo[1].y; int di=rabbit[1].di; if((x==1 && di==1) || (x==n && di==3) || (y==1 && di==0) || (y==n && di==2)) rabbit[1].di=di=(di+2)%4; coo[1].x=x+dx[di]; coo[1].y=y+dy[di]; } if(coo[0].x==coo[1].x && coo[0].y==coo[1].y) { int direct; direct=rabbit[0].di; rabbit[0].di=rabbit[1].di; rabbit[1].di=direct; flag1=false; } if(i%rabbit[0].turn==0 && flag1) rabbit[0].di=(rabbit[0].di+1)%4; //向左转 if(i%rabbit[1].turn==0 && flag1) rabbit[1].di=(rabbit[1].di+1)%4; } printf("%d %d\n%d %d\n",coo[0].y,coo[0].x,coo[1].y,coo[1].x); } int main() { //freopen("in.txt","r",stdin); while(scanf("%d",&n),n) { for(int i=0; i<2; i++) { char c; getchar(); scanf("%c%d%d",&c,&rabbit[i].v,&rabbit[i].turn); if(c=='N')rabbit[i].di=0; //将方向转化为数字,向左转的话+1再模4,向后转+2再模4 else if(c=='W')rabbit[i].di=1; else if(c=='S')rabbit[i].di=2; else if(c=='E')rabbit[i].di=3; } scanf("%d",&time); solve(); } return 0; }