贴磁砖问题(DP)

问题描述:一块地面的大小事2xn,所给的磁砖有2x2 和 2x1 两种,则该地面共有多少种方法可以贴地砖。

The following figure is an example of domino tiling in an 2x8 frame.

 

 

This problems seens to be difficult, but it is realy easy. 

To solve this problem we can use a simple form of dinamic programming.

Consider the following base-cases:
n = 1 : There is only one possibility for this case

n = 2: There are three possibilities for this case, || , [] and =

 

Now analyse the case when n=3:

 

Consider the two steps we can follow:
1) Put one 1x2 (vertical) block

2) Fill a 2x2 block (There are three possibilities for this)

 

This two steps are enough to solve the problem.

 

Choosing the step 1, we have 1 * calc(n-1) possibilities, where calc() is the method that solve this problem

Choosing the step 2, we have 3 * calc(n-2) possibilities, but the case when we put two 1x2 (vertical) blocks, is encompassed by the step one. It means that if we already choose step 1, there are 2 * calc(n-2) new possibilities.

 

So, we have the following that is enough to solve the problem:
calc(n) = 1*calc(n-1) + 2*calc(n-2)

 

posted on 2015-02-10 11:05  木星来客  阅读(198)  评论(0编辑  收藏  举报

导航