HDU4452 Running Rabbits

涉及知识点:

1. direction数组。

2. 一一映射(哈希)。

Running Rabbits

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

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

Source

2012 Asia JinHua Regional Contest

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  55665565556455635562

Statistic | Submit | Discuss | Note

#include<stdio.h>

int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};

int Map[256];
int Map1[256];

int main() {
    Map[‘N‘] = 0;
    Map[‘W‘] = 1;
    Map[‘S‘] = 2;
    Map[‘E‘] = 3;
    Map1[‘N‘] = ‘W‘;
    Map1[‘W‘] = ‘S‘;
    Map1[‘S‘] = ‘E‘;
    Map1[‘E‘] = ‘N‘;
    int n;
    while(~scanf("%d", &n), n) {
        char d1[2], d2[2];
        int s1, s2;
        int t1, t2;
        scanf("%s%d%d", d1, &s1, &t1);
        scanf("%s%d%d", d2, &s2, &t2);
        int k;
        scanf("%d", &k);
        int x1 = 1, y1 = 1, x2 = n, y2 = n;
        for(int i = 0; i < k; i++) {
            int id = Map[d1[0]];
            if(!dir[id][0]) {
                y1 += s1 * dir[id][1];
                if(y1 <= 0) {
                    y1 = 2 - y1;
                    d1[0] = ‘E‘;
                }
                if(y1 > n) {
                    y1 = 2 * n - y1;
                    d1[0] = ‘W‘;
                }
            } else {
                x1 += s1 * dir[id][0];
                if(x1 <= 0) {
                    x1 = 2 - x1;
                    d1[0] = ‘S‘;
                }
                if(x1 > n) {
                    x1 = 2 * n - x1;
                    d1[0] = ‘N‘;
                }
            }
            id = Map[d2[0]];
            if(!dir[id][0]) {
                y2 += s2 * dir[id][1];
                if(y2 <= 0) {
                    y2 = 2 - y2;
                    d2[0] = ‘E‘;
                }
                if(y2 > n) {
                    y2 = 2 * n - y2;
                    d2[0] = ‘W‘;
                }
            } else {
                x2 += s2 * dir[id][0];
                if(x2 <= 0) {
                    x2 = 2 - x2;
                    d2[0] = ‘S‘;
                }
                if(x2 > n) {
                    x2 = 2 * n - x2;
                    d2[0] = ‘N‘;
                }
            }
            if(x1 == x2 && y1 == y2) {
                int tmp = d1[0];
                d1[0] = d2[0];
                d2[0] = tmp;
            } else {
                if(!((i + 1) % t1))
                    d1[0] = Map1[d1[0]];
                if(!((i + 1) % t2))
                    d2[0] = Map1[d2[0]];
            }
        }
        printf("%d %d\n%d %d\n", x1, y1, x2, y2);
    }
    return 0;
}

  

时间: 2024-08-07 22:41:58

HDU4452 Running Rabbits的相关文章

模拟 HDOJ 4552 Running Rabbits

题目传送门 1 /* 2 模拟:看懂题意,主要是碰壁后的转向,笔误2次 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <vector> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 struct Rabbit 13

hdu 4452 Running Rabbits 模拟

Running RabbitsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1344    Accepted Submission(s): 925 Problem DescriptionRabbit Tom and rabbit Jerry are running in a field. The field is an N×N grid

HDU4452——模拟——Running Rabbits

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

[模拟] hdu 4452 Running Rabbits

题意: 两个人一个人在(1,1),一个人在(N,N) 给每个人每秒移动的速度v,和一个s代表移动s秒后左转方向 特别注意的是如果撞墙,要反弹回去,方向改变 比如在(1,1),往左走一步到(1,0) 其实就是走到了(1,2) 然后如果两个人见面那么交换方向并且不再左转! 思路: 直接模拟.. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath&qu

04 Rabbits and Recurrence Relations

Problem A sequence is an ordered collection of objects (usually numbers), which are allowed to repeat. Sequences can be finite or infinite. Two examples are the finite sequence (π,−2–√,0,π)(π,−2,0,π) and the infinite sequence of odd numbers (1,3,5,7,

POJ 题目3661 Running(区间DP)

Running Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5652   Accepted: 2128 Description The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ≤ N ≤ 10,000) minutes. During each minute, she can

无解了吗Zabbix server is not running:the information displayed may not be current

Zabbix server is not running:the information displayed may not be current 安装版本2.4.5 php5.5 mysql5.5 debian7.7 注:网上说要开启php支持openssl扩展,已经开启了呀 方法如下: 1.php.ini文件中查找 allow_url_fopen = On: 让你的php支持 opensll扩展. 2.默认,是没有openssl扩展的,只能重新编译安装. cd /data/php-5.5.2

医疗时鲜(Running)资讯(ZSSURE):移动医疗不算事儿

背景 最近在看Dr.2的书<移动医疗那点事儿>,所以此次博文就厚颜无耻的叫做"移动医疗不算事儿".新的一年,新的积累与进步,继续关注医疗行业的最新动态.此次主要介绍吐槽一下关于"自由执业"和"电子病例"的新闻.Running-- 自由执业 在<移动医疗那点事儿>中,Dr.2开篇问了一个问题"什么样的医生会出来自由执业?刚毕业的孩子会出来自由执业么?",其观点认为:会从体制内出来的医生通常都是一些在临床业

IntelliJ运行下载的Servlet时报错 Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException

学习Java Servlet时,从Wrox上下载了示例代码,准备run/debug时发现以下错误: Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException "Socket closed" /Users/GuQiang/Tomcat/apache-tomcat-8.5.8/bin/catalina.sh stopUsing CATALINA_B