zoukankan      html  css  js  c++  java
  • ZOJ 1229 M-Gift?!

    https://vjudge.net/contest/67836#problem/M

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

    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!

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int N, Gift;
    bool flag[55];
    
    void dfs(int i, int sum) {
        if(sum <= 0 || sum >= N + 1)
            return ;
        flag[sum] = true;
        dfs(i + 1, sum + 2 * i - 1);
        dfs(i + 1, sum - 2 * i + 1);
    }
    
    int main() {
        while(~scanf("%d%d", &N, &Gift)) {
            if(!N && !Gift) break;
    
            if(N >= 50)
                printf("Let me try!
    ");
            else {
                memset(flag, false, sizeof(flag));
                dfs(2, 1);
                if(flag[Gift])
                    printf("Let me try!
    ");
                else
                     printf("Don't make fun of me!
    ");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    算法笔记_148:有向图欧拉回路求解(Java)
    算法笔记_147:有向图欧拉回路判断应用(Java)
    算法笔记_146:TarJan算法的应用(Java)
    算法笔记_145:拓扑排序的应用(Java)
    第十六章、例行性工作排程 (crontab)
    第十五章、磁碟配额(Quota)与进阶文件系统管理
    第十四章、Linux 账号管理与 ACL 权限配置
    第十三章、学习 Shell Scripts
    第十二章、正规表示法与文件格式化处理
    第十一章、认识与学习 BASH
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9529259.html
Copyright © 2011-2022 走看看