2 1 2
Case 1: 1 Case 2: 2HintCase 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\n",cas ++, dp[n]); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 3469 Catching the Thief (博弈 + DP递推)
原文:http://blog.csdn.net/qq_18661257/article/details/47663595