Hdu-4452-Running Rabbits

Source: Internet
Author: User

A square field of N * N is divided into 1*1 grids. There are two rabbits. Tom is in the upper left corner (1, 1) and Jerry is in the lower right corner (N, N ), they can go in four directions in the Southeast and northwest regions, but they cannot go out of order. Each has its own speed. When they meet each other, the two rabbits exchange the direction of movement and reverse the way they touch the border, they also have their own turn to the left cycle, the cycle is one to turn to the left (but at this time the two rabbits do not execute this cycle to the left), ask the coordinates of the two rabbits after K hours. --> Just follow the instructions. Boundary processing: If N = 5, right or bottom bounds: x (or y) directly modulo 2 * N-21234543212345432123 ...... Cycle is 2*5-2, save to array a; left or upper bounds: x (or y) to 1-x (OR 1-y) and then modulo 2 * N-2 ...... The 123454321234543212345 period is 2*5-2 and is saved to array B. I didn't notice speed (1 ≤ s <N). I thought it would change the direction multiple times and write more errors ...... In fact, it's quite difficult ...... [Cpp] # include <cstdio> # include <algorithm> using namespace std; int N, a [50], B [50]; void turn (char & c) // turn left {switch (c) {case 'E': c = 'n'; break; case 's': c = 'E'; break; case 'W': c = 's'; break; case 'N': c = 'W'; break;} void moveto (char & c, int S, int & x, int & y) // step {switch (c) {case 'E': {y + = s; if (y> N) {y = a [y % (2 * N-2)]; c = 'W';} break;} case 's': {x + = S; if (x> N) {x = A [x % (2 * N-2)]; c = 'n';} break;} case 'W': {y-= s; if (y <1) {y = B [(1-y) % (2 * N-2)]; c = 'E';} break;} case 'N': {x-= s; if (x <1) {x = B [() % (2 * N-2)]; c = 's';} break ;}} int main () {int K, T_s, T_t, J_s, J_t, I; char T_c, J_c; while (~ Scanf ("% d", & N) {if (! N) return 0; scanf ("\ n % c % d", & T_c, & T_s, & T_t ); scanf ("\ n % c % d", & J_c, & J_s, & J_t); scanf ("% d", & K); int T_x = 1, t_y = 1, J_x = N, J_y = N; a [0] = 2; for (I = 1; I <= N; I ++) a [I] = I; for (I = N + 1; I <= 2 * N-3; I ++) a [I] = 2 * N-I; B [0] = 1; for (I = 1; I <= N-1; I ++) B [I] = I + 1; for (I = N; I <= 2 * N-3; I ++) B [I] = 2 * N-i-1; for (I = 1; I <= K; I ++) {moveto (T_c, t_s, T_x, T_y); moveto (J_c, J_s, J_x, J_y); if (T_x = J_x & T_y = J_y) swap (T_c, J_c ); else {if (I % T_t = 0) turn (T_c); if (I % J_t = 0) turn (J_c );}} printf ("% d \ n", T_x, T_y); printf ("% d \ n", J_x, J_y);} return 0 ;}

Related Article

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.