HDU2046:骨牌铺方格(递推)

骨牌铺方格
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69998 Accepted Submission(s): 33543

Problem Description
在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.
例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:

在这里插入图片描述

Input
输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50)。

Output
对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input
1
3
2

Sample Output
1
3
2

这道题从n的情况开始看 ,首先排骨牌有两种排法,横排和竖排。
竖排的情况:前f(n-1)种情况下多填了一列,直接放入多的那一列,即为f(n-1)种插法。
横排的情况:前f(n-2)种情况下多了两列给后面的两个骨牌放,只有一种放法,两块都横着放,即为f(n-2)中插法。

递推方程:f(n) = f(n-1) + f(n-2); (n >= 3)

(对了,以后做递推的题目都开long long int数组吧,免得wa了都不知道错哪)

下面附上ac代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#define INF 0x7ffffff
using namespace std;
typedef long long int ll;
ll a[100]; //递推题目都开ll比较保险
int main() {
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    for(int i = 4;i < 60; i++) {
        a[i] = a[i-1] + a[i-2];
    }
    int n;
    while(cin >> n){
        cout << a[n] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/86557221
今日推荐