UVa10120 - Gift?!

Problem D. Gift?! 

The Problem

There is a beautiful river in a small village. N rocks are arranged in a straight line numbered 1 to N from left bank to the right bank, as shown below.

[Left Bank] - [Rock1] - [Rock2] - [Rock3] - [Rock4] ... [Rock n] - [Right Bank]

The distance between two adjacent rocks is exactly 1 meter, while the distance between the left bank and rock 1 and the distance between Rock n and the right bank are also 1 meter.

Frog Frank was about to cross the river, his neighbor Frog Funny came to him and said,

'Hello, Frank. Happy Children's Day! I have a gift for you. See it? A little parcel on Rock 5.'

'Oh, that's great! Thank you! I'll get it.'

'Wait...This present is for smart frogs only. You can't get it by jumping to it directly.'

'Oh? Then what should I do?'

'Jump more times. Your first jump must be from the left bank to Rock 1, then, jump as many times as you like - no matter forward or backward, but your ith jump must cover 2*i-1 meters. What's more, once you return to the left bank or reach the right bank, the game ends, and no more jumps are allowed.'

'Hmmm, not easy... let me have a think!' Answered Frog Frank, 'Should I give it a try?'

The Input

The input will contain no more than 2000 test cases. Each test case contains a single line. It contains two positive integers N (2<=N<=10^6), and M (2<=M<=N), M indicates the number of the rock on which the gift is located. A test case in which N=0, M=0 will terminate the input and should not be regarded as a test case.

The Output

For each test case, output a single line containing 'Let me try!' If it's possible to get to Rock m, otherwise, output a single line containing 'Don't make fun of me!'

Sample Input

9 5
12 2
0 0

Sample Output

Don't make fun of me!
Let me try!


import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Arrays;

class Main
{
	public static final boolean DEBUG = false;
	public static final int N = 1000010;
	public static final int INF = 0x3f3f3f3f;
	public static int[] d = new int[N];
	public static int n, m;
	
	public static boolean bfs()
	{
		if (n >= 50) return true;
		
		Arrays.fill(d, INF);
		
		Queue<Integer> q = new LinkedList<Integer>();
		d[1] = 1;
		q.add(1);
		
		while (!q.isEmpty()) {
			int u = q.poll();
			
			if (u == m) return true;
			int v = u + d[u] + 2;
			if (v <= n) {
				q.add(v);
				d[v] = d[u] + 2;
			}
			
			v = u - d[u] - 2;
			if (v > 0) {
				q.add(v);
				d[v] = d[u] + 2;
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) throws IOException
	{
		Scanner cin;
		
		if (DEBUG) {
			cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new Scanner(new InputStreamReader(System.in));
		}
		
		while (cin.hasNext()) {
			n = cin.nextInt();
			m = cin.nextInt();
			
			if (n == 0 && m == 0) break;
			
			if (bfs()) {
				System.out.println("Let me try!");
			} else {
				System.out.println("Don't make fun of me!");
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值