HDU4452 Running Rabbits (Analog, Checkered Steering)

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

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

ideas

There are two people walking the grid, the first person starts at the (1,1)point, and the second person starts at the (n,n)point.

The question first gave one n, and then gave two lines, each line has three numbers, which represent the current facing direction, the current speed, and how many seconds to change the direction (turn left).

Also note:

  1. Will bounce back after hitting a wall
  2. When the two meet, they exchange directions
  3. It will turn to the left every fixed time

Then there is the simulation. I look at the code specifically. During the competition, because the topic is too long, I didn't understand it. .

code

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include<list>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
#define x1 hpc_x1
#define y1 hpc_y1
typedef long long ll;
const int N=10+20;
const int M=N*2;
int a1,b1,a2,b2,op1,op2,t,n;
int x1,y1,x2,y2;
int dx[4]= {0,1,0,-1};
int dy[4]= {-1,0,1,0};
int get_pos(char *s)
{
    if(s[0]=='W') return 0;
    if(s[0]=='S') return 1;
    if(s[0]=='E') return 2;
    if(s[0]=='N') return 3;
}
void change(int &x,int &y,int &op)//掉头函数
{
    //用2是为了方便找到对称点
    if(x<1)
    {
        x=2-x;
        op=1;
    }
    else if(x>n)
    {
        x=2*n-x;
        op=3;
    }
    else if(y<1)
    {
        y=2-y;
        op=2;
    }
    else if(y>n)
    {
        y=2*n-y;
        op=0;
    }
}
void solve()
{
    for(int i=1; i<=t; i++)
    {
        x1+=dx[op1]*a1;
        y1+=dy[op1]*a1;
        x2+=dx[op2]*a2;
        y2+=dy[op2]*a2;
        change(x1,y1,op1);
        change(x2,y2,op2);
        if(x1==x2&&y1==y2)
            swap(op1,op2);
        else
        {
            //左转
            if(i%b1==0) op1=(op1+1)%4;
            if(i%b2==0) op2=(op2+1)%4;
        }
    }
    printf("%d %d\n%d %d\n",x1,y1,x2,y2);
}
int main()
{
    //freopen("in.txt","r",stdin);
    char s1[5],s2[5];
    while(~scanf("%d",&n)&&n)
    {
        x1=y1=1;
        x2=y2=n;
        scanf("%s%d%d",s1,&a1,&b1);
        scanf("%s%d%d",s2,&a2,&b2);
        op1=get_pos(s1),op2=get_pos(s2);
        scanf("%d",&t);
        solve();
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855526&siteId=291194637