zoukankan      html  css  js  c++  java
  • 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 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
     
    /*
    遇到墙转向,在同一点相遇相互交换方向,不往左转,其他如果达到cout左转
    */
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dirx[] = {0, -1, 0, 1};
    int diry[] = {1, 0, -1, 0};
    int main()
    {
        int n, m;
        char ch1, ch2;
        int d1, d2, t1, t2, dir1, dir2;
        while(~scanf("%d", &n)){
            if(n == 0) break;
            getchar();
            scanf("%c%d%d", &ch1, &d1, &t1);
            if(ch1 == 'E') dir1 = 0;
            else if(ch1 == 'N') dir1 = 1;
            else if(ch1 == 'W') dir1 = 2;
            else if(ch1 == 'S') dir1 = 3;
            getchar();
            scanf("%c%d%d", &ch2, &d2, &t2);
            if(ch2 == 'E') dir2 = 0;
            else if(ch2 == 'N') dir2 = 1;
            else if(ch2 == 'W') dir2 = 2;
            else if(ch2 == 'S') dir2 = 3;
    
            scanf("%d", &m);
            int x1 = 1, y1 = 1, x2 = n, y2 = n;
            int cout1 = 0, cout2 = 0;
            for(int i = 1; i <= m ;i++){
                cout1++;
                cout2++;
                x1 = x1 + d1 * dirx[dir1];
                y1 = y1 + d1 * diry[dir1];
                x2 = x2 + d2 * dirx[dir2];
                y2 = y2 + d2 * diry[dir2];
                if(x1 < 1) {
                    x1 = 2 - x1;
                    dir1 = (dir1 + 2) % 4;
                }
                if(x1 > n){
                    x1 = n - (x1 - n);
                    dir1 = (dir1 + 2) % 4;
                }
                if(y1 < 1){
                    y1 = 2 - y1;
                    dir1 = (dir1 + 2) % 4;
                }
                if(y1 > n){
                    y1 = n - (y1 - n);
                    dir1 = (dir1 + 2) % 4;
                }
                if(x2 < 1){
                    x2 = 2 - x2;
                    dir2 = (dir2 + 2) % 4;
                }
                if(x2 > n){
                    x2 = n - (x2 - n);
                    dir2 = (dir2 + 2) % 4;
                }
                if(y2 < 1){
                    y2 = 2 - y2;
                    dir2 = (dir2 + 2) % 4;
                }
                if(y2 > n){
                    y2 = n - (y2 - n);
                    dir2 = (dir2 + 2) % 4;
                }
    
                if(x1 == x2 && y1 == y2){
                    int temp = dir1;
                    dir1 = dir2;
                    dir2 = temp;
                }
                else {
                    if(cout1 >= t1){
                    dir1 = (dir1 + 1) % 4;
                    cout1 = 0;
                    }
                    if(cout2 >= t2){
                    dir2 = (dir2 + 1) % 4;
                    cout2 = 0;
                    }
                }
              //  printf("%d %d %d %d %d %d
    ", x1, y1,dir1, x2, y2, dir2);
    
            }
            printf("%d %d
    ", x1, y1);
            printf("%d %d
    ", x2, y2);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Razor 视图
    可选参数和命名参数
    CPU性能分析工具原理
    从硬件到语言,详解C++的内存对齐(memory alignment)
    谈谈C++的volatile关键字以及常见的误解
    C++11的value category(值类别)以及move semantics(移动语义)
    C++基于范围循环(range-based for loop)的陷阱
    C++模板入门教程(一)——模板概念与基本语法
    自己动手实现深度学习框架-8 RNN文本分类和文本生成模型
    自己动手实现深度学习框架-7 RNN层--GRU, LSTM
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4655766.html
Copyright © 2011-2022 走看看