矩形覆盖

题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

思路:依然是斐波那契的变形,

 

(1)当 n < 1时,显然不需要用2*1块覆盖,按照题目提示应该返回 0。

 

(2)当 n = 1时,只存在一种情况。
(3)当 n = 2时,存在两种情况。
(4)当 n = n时,分为两步考虑: 第一次摆放一块 2*1 的小矩阵,则摆放方法总共为f(n - 1),第一次摆放一块1*2的小矩阵,则摆放方法总共为f(n-2).
 public int RectCover(int target) {
        if(target==0) return 0;
        if(target==1) return 1;
        if(target==2) return 2;
        else{
            return RectCover(target-1)+RectCover(target-2);
            
        }
    }

 

 
       

 

posted @ 2017-04-08 13:58  雪浪snowWave  阅读(111)  评论(0编辑  收藏  举报