HDU 4452 - Running Rabbits(模拟)

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
  
  
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

                                                                           

题意:两只兔子分别从N*N矩阵上的(1,1)和(N,N)出发,给出一开始的方向(W/E/N/S),速度和经过t 时间会向左转,一旦两只兔子相遇,则交换方向(不左转);兔子碰到矩阵墙壁则会往反方向走。求出K时间是两只兔子的位置坐标。

思路:简单模拟。用数字表示四个方向,则左转的公式为 f = (f + 3) % 4.

注意几条公式:if(x <= 0) x = 2 - x;
                            if(x > n) x = 2*n - x;
                            if(y <= 0) y = 2 - y;
                            if(y > n) y = 2*n - y;

本是一道简单题,但是代码能力太差,错漏百出T^T...以后要细心写代码。

CODE:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

int n;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
map<char, int> m;

void adjust(int &x, int &y, int &f)
{
    if(x <= 0 || x > n || y <= 0 || y > n) {
        if(f == 0) f = 2;
        else if(f == 2) f = 0;
        else if(f == 1) f = 3;
        else if(f == 3) f = 1;

        if(x <= 0) x = 2 - x;
        if(x > n) x = 2*n - x;
        if(y <= 0) y = 2 - y;
        if(y > n) y = 2*n - y;
    }
}

int main()
{
//freopen("in", "r", stdin);
    char c1, c2;
    int s1, s2, t1, t2, x1, y1, x2, y2, f1, f2, t;
    m['N'] = 0; m['E'] = 1; m['S'] = 2; m['W'] = 3;
    while(cin>>n){
        if(n == 0) break;
        cin>>c1>>s1>>t1>>c2>>s2>>t2;
        cin>>t;

        x1 = 1; y1 = 1; f1 = m[c1];
        x2 = n; y2 = n; f2 = m[c2];

        for(int i = 1; i <= t; ++i){
            x1 = x1 + s1*dx[f1];
            y1 = y1 + s1*dy[f1];

            adjust(x1, y1, f1);

            x2 = x2 + s2*dx[f2];
            y2 = y2 + s2*dy[f2];

            adjust(x2, y2, f2);

            if(x1 == x2 && y1 == y2){
                int tmp = f1;
                f1 = f2;
                f2 = tmp;
            }
            else {
                if(i % t1 == 0){
                    f1 = (f1 + 3) % 4;
                }
                if(i % t2 == 0){
                    f2 = (f2 + 3) % 4;
                }
            }
        }
        printf("%d %d\n%d %d\n", x1, y1, x2, y2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值