HDU 4452-running rabbits-Simulation

Source: Internet
Author: User

Running Rabbits

Problem Description:

Rabbit Tom and Rabbit Jerry is running in a field. The field is an nxn grid. Tom starts from the Up-left cell and Jerry starts from the Down-right cell. The coordinate of the Up-left cell is (all) and the coordinate of the Down-right cell is (n,n). A 4x4 field and some coordinates of its cells is shown below:       The rabbits can run in four directions (north, south, west and east) and they run at Certai n 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'll turn around and keep running. For example, in a 5x5 grid, if a rabbit are heading west with a speed of 3 cells per hour, and it's in the (3, 2) cell now , then one hour later it'll get to cell (3,3) and keep heading east. For example again, if a rabbit are in the (1,3) cell and it's heading north by speed 2,then a hour latter it'll get to ( 3,3). The Rabbits start running at 0 o ' clock. If rabbits meet in the same cell at K o ' clock sharp (k can is any positive integer), Tom would change his direction in To Jerry's direction, and Jerry also would change he direction into Tom's original direction. This direction changing is before the judging of whether they should turn around.       The Rabbits'll turn left every Certain hours. For example, if Tom turns to every 2 hours, then he'll turn left at 2 o ' clock, 4 o ' clock, 6 o ' clock. etc. But if a rabbit was just about to turn left when the rabbit meet, he would forget to turn this time. Given the initial speed and directions of the both rabbits, you should figure out where is they after some time.

Input:

There is several test cases.       For each test case:the first line is a integer N, meaning that field is an nxn 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, N Orth 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 was about Jerry and it's in the same format as the second line.       The last line was an integer k meaning this should calculate the position of Tom and Jerry at K o ' clock (1≤k≤200). The input ends with N = 0.

Output:

There is several test cases.       For each test case:the first line is a integer N, meaning that field is an nxn 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, N Orth 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 was about Jerry and it's in the same format as the second line.       The last line was an integer k meaning this should calculate the position of Tom and Jerry at K o ' clock (1≤k≤200). The input ends with N = 0.

Sample input:

4E 1 1W 1 124E 1 1W 2 154E 2 2W 3 150

Sample output:

2 23 32 12 43 14 1
The main topic: (1), the first line of input is n (ending with n equals 0), indicating the range of a given n*n, initially placing two rabbits in (n,n) position.
(2), the second line entered the direction of the first rabbit, the speed (the number of squares per hour), after the number of realities to turn left once;
(3), the third line input is the second rabbit information, the input content and the first rabbit consistent;
(4), the fourth line to enter a time, we ask is after such a time, two rabbits position, note if two rabbits collided, then they exchanged each other's direction.
(6), note: 1, the rabbit will reverse the direction of the wall,
2, two rabbits collide, will exchange their direction;
3, every fixed time t, the Rabbit will need to change direction to the left;
4. If the collision and the left turn occur simultaneously, we will prioritize the impact of the collision
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intN;6 voidFunint&x,int&y,intVChar&d)7 {8     if(d=='E') x=x+v;9     if(d=='W') x=x-v;Ten     if(d=='N') y=y-v; One     if(d=='S') y=y+v; A     if(x>n) {d='W'; x=2*n-x;} -     if(x<1) {d='E'; x=2-x;} -     if(y>n) {d='N'; y=2*n-y;} the     if(y<1) {d='S'; y=2-y;} - } - voidChangeChar&d) - { +     if(d=='E') -D='N'; +     Else A     { at         if(d=='W') -D='S'; -         Else -         { -             if(d=='N') -D='W'; in             Else -D='E'; to         } +     } - } the intMain () * { $     intV1,v2,t1,t2,time,x1,x2,y1,y2;Panax Notoginseng     Chard1,d2,temp; -      while(SCANF ("%d", &n) &&N) the     { + GetChar (); Ascanf"%c%d%d",&d1,&v1,&t1); the GetChar (); +scanf"%c%d%d",&d2,&v2,&T2); -Cin>>Time ; $x1=y1=1; $X2=y2=N; -          for(intI=1; i<=time;i++) -         { the Fun (X1,Y1,V1,D1); - Fun (X2,Y2,V2,D2);Wuyi             if(x1==x2&&y1==y2) the             { -temp=D1; Wud1=D2; -D2=temp; About             } $             Else -             { -                 if(i%t1==0) - Change (D1); A                 if(i%t2==0) + Change (D2); the             } -         } $cout<<y1<<" "<<x1<<Endl; thecout<<y2<<" "<<x2<<Endl; the     } the}
View Code

HDU 4452-running rabbits-Simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.