POJ 1728 A flea on a chessboard

Source: Internet
Author: User
Tags bool printf
A flea on a chessboard
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1509 Accepted: 491

Description an infinite chessboard are obtained by extending a finite chessboard to the right and up infinitely. Each square of the chessboard was either black or white with the side of S milimiters, 0 < s <= 1000. The leftmost bottom square of the chessboard is black. A Flea is positioned on the chessboard @ the point (x, y) (given in milimeters) and makes jumps by jumping DX milimeters To-right and dy-milimiters up, 0 < DX, dy, that's, a flea at position (x, y) after one jump lands at position (x+d X, Y+dy) . 
Given The starting position of the flea on the Board your task was to find out after how many jumps the Flea would reach a white square. If the Flea lands on a boundary between and then it is squares not count as does on the white square. Note that it was possible that the flea never reaches a white square. 

Input each test case consists the one line of the input containing five non-negative numbers separated by white space and Givin g integers S, x, y, DX, and dy. An input line containing five zeroes follows the last test case.

Output for test case print one line of output in the format shown in the sample.

Sample Input

2 3 3 2 (
214) 0 0 5
407 1270 1323 1 1 6 6 407 1270
1170
0 0 0 0 0

Sample Output

After 3 jumps the Flea lands at (one, 9).
After 1 jumps the Flea lands at (263, 111).
The flea cannot escape from black squares.
After 306 jumps the Flea lands at (1576, 1629).
The flea cannot escape from black squares.
After 0 jumps the flea lands at (1270, 1170).

Source

Waterloo Local 2004.01.31

/* First it is easy to determine that the current coordinates (x, y) fall on the white lattice condition is Q (x/s) + q (y/s) is odd, q means that the next rounding can be from the beginning of the continuous cycle of jumping, and use the above conditions to determine whether to jump to the white lattice, if not white lattice, then the current coordinate mode S , y% S) has been accessed outdated instructions there is a place to pay attention to here, this place let me a lot of times, that is: when the modulo operation if x 2 = = 0 (or y% 2 = = 0) When it falls on the boundary, you need to determine whether the point is at the left or right edge of the lattice, judge the current The color of this lattice, if the Hegez is the beginning, if the white lattice is at the far right * * #include <iostream> #define MAX_N 1005 using namespace std; BOOL V[max_n + 1][max_n + 1]; int main () {__int64 S, x, y, dx, dy, while (scanf ("%i64d%i64d%i64d%i64d%i64d", &s, &x, &y, &AMP;DX, &dy) & amp;& (S + x + y + dx + dy) = 0) {memset (V, 0, sizeof (v)), __int64 CurX = x, CurY = y, steps = 0; bool can = true; w Hile (True) {__int64 r1 = CurX% s, r2 = curY% s; __int64 q1 = curx/s, q2 = cury/s; if (r1! = 0 && r2! = 0 &am p;& (Q1 + q2)% 2 = = 1) break; Determine at which boundary if (r1 = = 0 && (Q1 + q2)% 2 = = 1) r1 = S; if (r2 = = 0 && (Q1 + q2)% 2 = = 1) r2 = S; if (V[r1][r2]) {can = false; break;} steps++; V[R1][R2] = true; CurX + = DX; CurY + = dy; } if (!can) printf ("TheFlea cannot escape from black squares./n "); else printf ("After%i64d jumps the Flea lands at (%i64d,%i64d)./n", Steps, CurX, CurY); } return 0; } 

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.