UVA10120 ZOJ1229 Gift?!DFS+BFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10120 ZOJ1229 Gift?!DFS+BFS相关的知识,希望对你有一定的参考价值。

Gift?!
Time Limit: 2000 msMemory Limit: 65536 KB
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.

[ L e f t B a n k ] − [ R o c k 1 ] − [ R o c k 2 ] − [ R o c k 3 ] − [ R o c k 4 ] . . . [ R o c k n ] − [ R i g h t B a n k ] [Left Bank] - [Rock1] - [Rock2] - [Rock3] - [Rock4] ... [Rock n] - [Right Bank] [LeftBank][Rock1][Rock2][Rock3][Rock4]...[Rockn][RightBank]

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?’

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 (1<=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.

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!

Note

In test case 2, Frank can reach the gift in this way:

Forward(to rock 4), Forward(to rock 9), Backward(to rock 2, got the gift!)

Note that if Frank jumps forward in his last jump, he will land on the right bank(assume that banks are large enough) and thus, lost the game.

问题链接UVA10120 ZOJ1229 Gift?!
问题简述:给定n和m,表示有n个各格子,每次走1,3,5…2n-1步。不能走出格子,问能否走到m?
问题分析:搜索问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(DFS)如下:

/* UVA10120 ZOJ1229 Gift?! */

#include <bits/stdc++.h>

using namespace std;

int n, m;

int dfs(int s, int p)
{
    if (s == m) return 1;
    int l = 2 * p - 1;
    if (s + l <= n && dfs(s + l, p + 1)) return 1;
    if (s - l > 0 && dfs(s - l, p + 1)) return 1;
    return 0;
}

int main()
{
    while (scanf("%d%d", &n, &m) == 2 && (n || m))
        if (n >= 49) puts("Let me try!");
        else if(dfs(1, 2)) puts("Let me try!");
        else puts("Don't make fun of me!");

    return 0;
}

AC的C++语言程序(BFS)如下:

/* UVA10120 ZOJ1229 Gift?! */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
int d[N];
int n, m;

bool judge()
{
    if (n >= 50) return true;

    queue<int> q;
    d[1] = 1;
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        int v = u + d[u] + 2;
        q.pop();
        if (u == m) return true;
        if (v <= n) {
            q.push(v);
            d[v] = d[u] + 2;
        }
        if ((v = u - d[u] - 2) > 0) {
            q.push(v);
            d[v] = d[u] + 2;
        }
    }
    return false;
}

int main()
{
    while (scanf("%d%d", &n, &m) == 2 && (n || m))
        printf("%s\\n", judge() ? "Let me try!" : "Don't make fun of me!");

    return 0;
}

以上是关于UVA10120 ZOJ1229 Gift?!DFS+BFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA10417 Gift Exchanging概率

UVA10144 ZOJ2044 Expression

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

UVA10081 POJ2537 ZOJ1883 Tight WordsDP