Running Rabbits HDU - 4452 模拟

Running Rabbits

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

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

题意:现在有两只兔子,第一只在(1,1),第二只在(n,n);两只兔子有它们初始的方向W和速度S(S格/每小时),它们跑起来有一个特点:当它向前走S格时,半路就遇到了墙壁,那么它会向后转,然后把剩余的S跑完。每个兔子有一个时间t,每过t小时就会向左转,当两只兔子到达的坐标一样时(跑的途中相遇则不算,只看每小时结束时坐标),它们会改变方向,变成对方当前的方向,(若这时候某只兔子需要左转时,这个左转可以不用转了);

思路:模拟这个过程。只要把转向,坐标改变写成函数,注意模拟细节这道题就问题不大,具体细节看代码。我代码敲完,满满的都是BUG,调了10分钟才调完。(好在思路还是比较清晰的)

代码:

#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
typedef long long LL;
const int N=5e4+20;
const double pi=acos(-1);
char s1[10],s2[10];
int sp1,sp2,T1,T2,n;
int F(char s)//方向:北N 0,东E 1,南S 2,西W 3
{
    if(s=='N') return 0;
    if(s=='E') return 1;
    if(s=='S') return 2;
    return 3;
}
void K(int &x,int &y,int sp,int &z)//兔子跑一小时坐标变化
{
    if(z==0)
    {
        x-=sp;
        if(x<=0) x=2-x,z=(z+2)%4;//这里超出边界,自己模拟一下即可明白
    }
    else if(z==1)
    {
        y+=sp;
        if(y>n) y=2*n-y,z=(z+2)%4;
    }
    else if(z==2)
    {
        x+=sp;
        if(x>n) x=2*n-x,z=(z+2)%4;
    }
    else if(z==3)
    {
        y-=sp;
        if(y<=0) y=2-y,z=(z+2)%4;
    }
}
void W(int &x)//左转,改变方向
{
    x=(x+3)%4;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        scanf("%s%d%d",s1,&sp1,&T1);
        scanf("%s%d%d",s2,&sp2,&T2);
        int k;
        scanf("%d",&k);
        int x1=1,y1=1,t1=0,x2=n,y2=n,t2=0,f1,f2;
        f1=F(s1[0]);
        f2=F(s2[0]);
        //x1,y1,表示兔子一坐标,t1表示跑了多久,f1表示当前方向,x2,y2,t2,f2同理。
        while(k--)
        {
            K(x1,y1,sp1,f1);//第一只兔子跑一小时
            K(x2,y2,sp2,f2);//
            t1++,t2++;//时间++
            if(x1==x2&&y1==y2)//相遇
            {
                swap(f1,f2);//方向交换
                continue;//不继续运行下面,一定不左转。
            }
            if(t1%T1==0) W(f1);//每T1小时就左转
            if(t2%T2==0) W(f2);
        }
        printf("%d %d\n%d %d\n",x1,y1,x2,y2);
    }
}



猜你喜欢

转载自blog.csdn.net/xiangaccepted/article/details/80208957