HDU 4452-Running Rabbits-模拟

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:
Dressing
      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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

2 2
3 3
2 1
2 4
3 1
4 1
题目大意:(1)、输入的第一行是n(以n等于0为结束),表示给定一个n*n的范围,最初将两只兔子放在(1,1)和(n,n)的位置。
(2)、第二行分别输入第一只兔子的前进方向,速度(每个小时走过的格子数),经过多少个现实向左转一次;
(3)、第三行输入的是第二只兔子的信息,输入内容和第一只兔子一致;
(4)、第四行输入一个时间,我们要求的就是经过这么的时间后,两只兔子的位置,注意如果两只兔子相撞了,那么他们就交换彼此的方向。
(6)、注意:1、兔子撞墙后就会反转方向,
2、两只兔子相撞后,会交换各自的方向;
3、每隔固定的时间t,兔子就需要向左转换一次方向;
4、如果相撞和左转同时发生,我们就优先考虑相撞的影响
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int n;
 6 void fun(int &x,int &y,int v,char &d)
 7 {
 8     if(d=='E') x=x+v;
 9     if(d=='W') x=x-v;
10     if(d=='N') y=y-v;
11     if(d=='S') y=y+v;
12     if(x>n){ d='W';x=2*n-x;}
13     if(x<1) { d='E';x=2-x;}
14     if(y>n) {d='N';y=2*n-y;}
15     if(y<1) {d='S';y=2-y;}
16 }
17 void change(char &d)
18 {
19     if(d=='E')
20         d='N';
21     else
22     {
23         if(d=='W')
24             d='S';
25         else
26         {
27             if(d=='N')
28                 d='W';
29             else
30                 d='E';
31         }
32     }
33 }
34 int main()
35 {
36     int v1,v2,t1,t2,time,x1,x2,y1,y2;
37     char d1,d2,temp;
38     while(scanf("%d",&n)&&n)
39     {
40         getchar();
41         scanf("%c %d %d",&d1,&v1,&t1);
42         getchar();
43         scanf("%c %d %d",&d2,&v2,&t2);
44         cin>>time;
45         x1=y1=1;
46         x2=y2=n;
47         for(int i=1;i<=time;i++)
48         {
49             fun(x1,y1,v1,d1);
50             fun(x2,y2,v2,d2);
51             if(x1==x2&&y1==y2)
52             {
53                 temp=d1;
54                 d1=d2;
55                 d2=temp;
56             }
57             else
58             {
59                 if(i%t1==0)
60                     change(d1);
61                 if(i%t2==0)
62                     change(d2);
63             }
64         }
65         cout<<y1<<" "<<x1<<endl;
66         cout<<y2<<" "<<x2<<endl;
67     }
68 }
View Code

 

转载于:https://www.cnblogs.com/xinxiangqing/p/5021222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值