HDOJ 4452 Running Rabbits(模拟)

来源:互联网 发布:小视频网站源码 编辑:程序博客网 时间:2024/05/20 14:25

Running Rabbits

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


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


题解:这题纯模拟即可, 一步一步的求,每到达一个格子后, 看其是否改变方向,模拟的时候注意方向的转变。


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 21;int n;int dx[] = {-1, 0, 1, 0};int dy[] = {0, -1, 0, 1};int get_dir(char ch) {if (ch == 'N') return 0;if (ch == 'W') return 1;if (ch == 'S') return 2;return 3;}bool is_point(int x, int y) {return x >= 1 && x <= n && y >= 1 && y <= n;}void go(int & s, int & dir, int & x, int & y) {    for (int i = 0; i < s; ++i) {        if (is_point(x + dx[dir], y + dy[dir])) {            x += dx[dir], y += dy[dir];        }        else {            dir = (dir + 2) % 4;            x += dx[dir], y += dy[dir];        }    }}int main() {while (~scanf("%d", &n), n) {char str[5];int x1, y1, s1, t1, dir1;int x2, y2, s2, t2, dir2;x1 = y1 = 1;x2 = y2 = n;scanf("%s%d%d", str, &s1, &t1);dir1 = get_dir(str[0]);scanf("%s%d%d", str, &s2, &t2);dir2 = get_dir(str[0]);int k;scanf("%d", &k);bool flag = false;for (int i = 1; i <= k; ++i) {go(s1, dir1, x1, y1);go(s2, dir2, x2, y2);if (x1 == x2 && y1 == y2)                swap(dir1, dir2);else {if (i % t1 == 0)dir1 = (dir1 + 1) % 4;if (i % t2 == 0)dir2 = (dir2 + 1) % 4;}}printf("%d %d\n", x1, y1);printf("%d %d\n", x2, y2);}return 0;}


0 0
原创粉丝点击