• HDU 3469 Catching the Thief (博弈 + DP递推)


    Catching the Thief

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 653    Accepted Submission(s): 359


    Problem Description
    In the Qingshui Village, there's a clever thief and a cleverer police. 

    There are N houses in Qingshui Village which are located in a straight line. And the N houses are numbered from 1 to N according to the direction of the line. Two houses are consided to be neighbor of each other if and only if there is no other house between them.

    The thief hides in one of N houses now, and the police tries to find him out. Every day the police will choose a house to check and he will catch the thief if he hides in that house. If the thief survive the arrest of the police, in the night he will move to a neighboring house to pass through the next day.

    What is the number of days the police needs to catch the thief in the worst case?
    Remember that the police is a clever man.

     

    Input
    In the first line, an integer T (T <= 100) indicates the number of cases. 
    T lines follow. Each contains an integer N described above. (1 <= N <= 10000)


     

    Output
    For each test case, output “Case x: d” in which x is the number of test case counted from one, and d is the number of days before the police catch the thief in the worst case.

     

    Sample Input
    2 1 2
     

    Sample Output
    Case 1: 1 Case 2: 2
    Hint
    Case 1: There is only one room, so the police can catch the thief on the first day. Case 2: There are two rooms. The police can check room 1 on the first day. The worst case is that the thief is in room 2, but in this case the police can check room 1 on the second day and will catch the thief for sure.
    这道题目首先要解出前面四个的解
    房间为一个的时候答案为1天
    房间为两个的时候答案为2天
    房间为三个的时候答案为2天
    房间为四个的时候答案为4天
    第五个房子则能够递推。例如以下图
    从左往右走。一步步排除。小偷所在的房子。dp[2]代表着两个房间里最多用多少天能够抓住小偷,如此,我们能够不断递推,先排除,左边两个房间会出现小偷的情况,接着右边还有三个房子,可是为什么图中将第二个房子都给画圈了。由于我们排除了最左边的房子不会出现小偷,可是此时无法防止第二个房子不会再出现小偷。如此要将他算进去,所以dp[5] = dp[2] + dp[4]如此不断递推得出终于的状态转移方程
    dp[n] = dp[2] + dp[n - 1]
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int MAXN = 10000 + 5;
    LL dp[MAXN];
    int n, T;
    void init(){
        dp[1]=1;
        dp[2] = 2;
        dp[3] = 2;
        for(int i = 4;i < MAXN;i ++){
            dp[i] = dp[2] + dp[i - 1];
        }
    }
    int main(){
        init();
        int cas = 1;
        scanf("%d", &T);
        while(T --){
            scanf("%d", &n);
            printf("Case %d: %I64d
    ",cas ++, dp[n]);
        }
    }


     
  • 相关阅读:
    微信:JSSDK开发
    JSSDK图像接口多张图片上传下载并将图片流写入本地
    微信JSSDK上传多张图片
    laravel自定义公共函数的引入
    php把数据转换为json格式
    laravel控制器方法中,用函数作为变量进行传递时的处理方法
    ckeditor富文本编辑器的基本配置设置:
    laravel处理菜单保持的方法:
    laravel基于Bootstrap的成功和失败的提示信息和验证提示信息
    laravel Eloquent 查询数据库判断获取的内容是否为空
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7240117.html
Copyright © 2020-2023  润新知