HDU5366——The mook jong——dp
The mook jong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 226 Accepted Submission(s): 167
Problem Description
![](../../data/images/C613-1001-1.jpg)
ZJiaQ want to become a strong man, so he decided to play the mook jong。ZJiaQ want to put some mook jongs in his backyard. His backyard consist of n bricks that is 1*1,so it is 1*n。ZJiaQ want to put a mook jong in a brick. because of the hands of the mook jong, the distance of two mook jongs should be equal or more than 2 bricks. Now ZJiaQ want to know how many ways can ZJiaQ put mook jongs legally(at least one mook jong).
ZJiaQ want to become a strong man, so he decided to play the mook jong。ZJiaQ want to put some mook jongs in his backyard. His backyard consist of n bricks that is 1*1,so it is 1*n。ZJiaQ want to put a mook jong in a brick. because of the hands of the mook jong, the distance of two mook jongs should be equal or more than 2 bricks. Now ZJiaQ want to know how many ways can ZJiaQ put mook jongs legally(at least one mook jong).
Input
There ar multiply cases. For each case, there is a single integer n( 1 < = n < = 60)
Output
Print the ways in a single line for each case.
Sample Input
1
2
3
4
5
6
Sample Output
1
2
3
5
8
12
题目大意:在由1*1的地砖铺成的1*n的院子里摆放木桩,每个木桩放在一个地板上。每两个木桩之间间隔至少两个地板,问有多少种放法。
解题思路:比赛的时候用的搜索,那叫一个慢,就打了40——60之间的表,算是蒙混过关。但是其实正解是dp。对于某个地板,可以放或者不放,那么如果放的话,结果加上dp[i-3]。如果不放的话,dp[i-1]是最优子结构,同时加上一共只放一个木桩在i位置的情况。dp[i]=dp[i-3]+dp[i-1]+1。结果超int需注意。
#include<bits/stdc++.h> using namespace std; typedef long long INT; INT dp[65]; int main(){ dp[1]=1,dp[2]=2,dp[3]=3; for(int i=4;i<=62;i++) dp[i]=dp[i-3]+dp[i-1]+1; int n; while(scanf("%d",&n)!=EOF){ printf("%lld\n",dp[n]); } return 0; }
学学学 练练练 刷刷刷