HDU - 4452

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)走,问你k小时后两兔的位置。当然后几种骚操作需要你遵守。

题目首先给了一个n,然后给了两行,每行有三个数,分别代表当前面朝的方向,当前的速度,还有每隔多少秒转变一次方向(向左转).

还要注意:

  1. 撞墙后会反弹方向
  2. 两个人相遇后会交换各自的方向
  3. 每隔固定时间会向左转变一次方向

当发生第二个条件时若也同时发生第三个条件,那么第三个就不会发生了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char c1[10],c2[10];
int n;
int dis(char x)
{
    if(x=='N')
        return 0;
    else if(x=='W')
        return 1;
    else if(x=='S')
        return 2;
    else
        return 3;
}
int aa(int &x,int &y,int s,int &d)
{
    if(d==0)
    {
        x-=s;
        if(x<=0)
        {
            x=2-x;
            d=2;
        }
    }
    else if(d==1)
    {
        y-=s;
        if(y<=0)
        {
            y=2-y;
            d=3;
        }
    }
    else if(d==2)
    {
        x+=s;
        if(x>n)
        {
            x=2*n-x;
            d=0;
        }
    }
    else if(d==3)
    {
        y+=s;
        if(y>n)
        {
            y=2*n-y;
            d=1;
        }
    }
}
int main()
{
    int x1,x2,y1,y2,t1,t2,s1,s2,tt1,tt2;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%s%d%d",c1,&s1,&t1);
        scanf("%s%d%d",c2,&s2,&t2);
//        printf("%d %d",dis(c1[0]),dis(c2[0]));
        int d1=dis(c1[0]);
        int d2=dis(c2[0]);
        int k;
        scanf("%d",&k);
        x1=1;
        y1=1;
        x2=n;
        y2=n;
        tt1=0;
        tt2=0;
        while(k--)
        {
            aa(x1,y1,s1,d1);
            aa(x2,y2,s2,d2);
            tt1++;
            tt2++;
            if(x1==x2&&y1==y2)
            {
                swap(d1,d2);
            }
            else
            {
                if(tt1%t1==0) d1=(d1+1)%4;
                if(tt2%t2==0) d2=(d2+1)%4;
            }
        }
        printf("%d %d\n",x1,y1);
        printf("%d %d\n",x2,y2);
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/80221616
hdu
今日推荐