我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
链接:https://www.nowcoder.com/questionTerminal/72a5a919508a4251859fb2cfb987a0e6
来源:牛客网
依旧是斐波那契数列
2*n的大矩形,和n个2*1的小矩形
其中target*2为大矩阵的大小
有以下几种情形:
1⃣️target <= 0 大矩形为<= 2*0,直接return 1;
2⃣️target = 1大矩形为2*1,只有一种摆放方法,return1;
3⃣️target = 2 大矩形为2*2,有两种摆放方法,return2;
4⃣️target = n 分为两步考虑:
第一次摆放一块 2*1 的小矩阵,则摆放方法总共为f(target - 1)
第一次摆放一块1*2的小矩阵,则摆放方法总共为f(target-2)
√ | |||||||
√ |
第一次摆放一块1*2的小矩阵,则摆放方法总共为f(target-2)
因为,摆放了一块1*2的小矩阵(用√√表示),对应下方的1*2(用××表示)摆放方法就确定了,所以为f(targte-2)
√ | √ | ||||||
× | × |
1 public int RectCover(int target) { 2 if(target <= 0) 3 return 0; 4 if(target == 1) 5 return 1; 6 int one = 1; 7 int two = 2; 8 if(target > 1) { 9 while(target-- > 2){ 10 two += one; 11 one = two - one; 12 } 13 } 14 return two; 15 }