UVa10620 - A Flea on a Chessboard(模拟问题)

An infinite chessboard is obtained by extending a finite chessboard to the right and up infinitely. Each square of the chessboard is 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 at the point (xy) (given in milimeters) and makes jumps by jumping dx milimeters to the right and dy milimiters up, 0 < dxdy, that is, a flea at position (xy) after one jump lands at position (x+dxy+dy).

Given the starting position of the flea on the board your task is to find out after how many jumps the flea will reach a white square. If the flea lands on a boundary between two squares then it does not count as landing on the white square. Note that it is possible that the flea never reaches a white square.

Input and output

Each test case consists of one line of input containing five non-negative numbers separated by white space and giving  S x y dx , and  dy . An input line containing five zeroes follows the last test case. For test case print one line of output in the format shown in the sample.

Sample input

 
10 2 3 3 2
100 49 73 214 38
25 0 0 5 25
407 1270 1323 1 1
18 72 6 18 6
407 1270 1170 100 114
0 0 0 0 0

Output for sample input

After 3 jumps the flea lands at (11, 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).
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main implements Runnable
{
	private static final boolean DEBUG = false;
	private PrintWriter cout;
	private Scanner cin;
	private int s, x, y, dx, dy;
	
	private void init()
	{
		try {
			if (DEBUG) {
				cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new Scanner(new BufferedInputStream(System.in));
			}
			
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private boolean input()
	{
		s = cin.nextInt();
		x = cin.nextInt();
		y = cin.nextInt();
		dx = cin.nextInt();
		dy = cin.nextInt();
		
		if (s + x + y + dx + dy == 0) return false;
		
		return true;
	}

	private boolean judge(int a, int b)
	{
		return (a % s != 0) && (b % s != 0) && ((a / s + b / s) % 2 != 0);
	}
	
	private void solve()
	{
		int i;
		for (i = 0; i < 2 * s; i++) {
			if (judge(x, y)) {
				cout.println("After " + i + " jumps the flea lands at (" + x + ", " + y + ").");
				break;
			}
			x += dx;
			y += dy;
		}
		
		if (i == 2 * s) {
			cout.println("The flea cannot escape from black squares.");
		}
		
		cout.flush();
	}
	
	public void run()
	{
		init();
		
		while (input()) {
			solve();
		}
	}
	
	public static void main(String[] args)
	{
		new Thread(new Main()).start();
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值