-1

Here is the part of the question with copied for ref:

*You are given a floor of size 5xN. You have tiles of 2 different sizes: 1x5 and 2x5. Of course, you can rotate the tiles to get 2 more tile sizes: 5x1 and 5x2. You have to do the flooring using these tiles in the following way:

  1. Floor space should be completely covered by tiles.
  2. You cannot break tiles, ie, you have to use a tile entirely or not at all.
  3. Any tile should not extend beyond the floor space.
  4. Tiles should be placed parallel to the floor boundaries.

Your task is to find the number of ways in which you can lay the tiles on the floor*

Can I get some help with the approach. Thanks in advance.

Edit: I understand now when we have to count the ways to fill floor of size 5*N with tiles of size 5*1. With dp we can achieve it like this dp[1]=1,dp[2]=1,dp[3]=1,dp[4]=1,dp[5]=2 and dp[n]=dp[n-1]+dp[n-5] http://www.geeksforgeeks.org/count-number-ways-tile-floor-size-n-x-m-using-1-x-m-size-tiles/

But I don't understand how to formulate dp[n] when there are more than one tile of different sizes. You are given a floor of size 5xN. You have tiles of 2 different sizes: 1x5 and 2x5.

user0042
  • 7,917
  • 3
  • 24
  • 39
rd10
  • 1,599
  • 2
  • 10
  • 27

2 Answers2

2

This answer from https://math.stackexchange.com/a/664236/468110 by Steve Kass helped me formulate the recurrence. Posting my solution, for someone may find it useful. There are total 10 ways first column of 5*n can be tiled by dominos of size 1*5 and 2*5 shown and named in picture below: We'll count the number with each kind of beginning configuration and add the results up to get f(n).ways to begin tiling

Any 5*(n-1) can extend to give 5*n tiling and there are no other way to get a "type a" 5*n tiling. So, there are f(n-1) "type a" tilings. Similarly, there are f(n-2) "type b" 5*n tiling. Similarly type c to j all have f(n-5) tiling. That makes:

f(n)=f(a)+f(b)+f(c)+f(d)+f(e)+f(f)+f(g)+f(h)+f(i)+f(j)
f(n)=f(a)+f(b)+8*f(c)
f(n)=f(n-1)+f(n-2)+ 8*f(n-5)

Below is the c++ code used and tested:

int dp[1000000]={0};
int count(int n){
    if(n<0){
        return 0;
    }
    dp[0]=1;
    dp[1]=1;
    dp[2]=2;
    if(dp[n]>0){
        return dp[n];
    }

    dp[n] = count(n-1)+count(n-2)+8*count(n-5);

    return dp[n];    
}
rd10
  • 1,599
  • 2
  • 10
  • 27
1

Some DP with memoization should do the trick:

def solve(x, memo={}):

    # Access already calculated values
    if x in memo:
        return memo[x]

    # Base cases
    if x < 0:
        raise Exception("Negative values are not allowed")
    if x < 2:
        return 1
    if x == 2:
        return 2

    # Place a 1x5 or a 2x5
    res = solve(x - 1) + solve(x - 2)

    # Fill a space of 5x5
    if 5 <= x:
        res += 8 * solve(x - 5)

    # Store result and return
    memo[x] = res
    return res

for x in range(100):
    print "{}: {}".format(x, solve(x))
fafl
  • 7,222
  • 3
  • 27
  • 50
  • Hi @fafl, thanks a lot for your answer. Can you please explain how did you get to the part:# Place a 1x5 or a 2x5 res = 1 + solve(x - 1) + 1 + solve(x - 2) # Fill a space of 5x5 if 5 <= x: res += 8 + solve(x - 5) or maybe you can give any link with something to explain how to approach when there's more than one tile for the tilting. – rd10 Jul 26 '17 at 12:02
  • Hi @RajaDorji, you can imagine the "Place 1x5 or 2x5" as a branching, where 1 is added to the solution and the "Fill a space of 5x5" as 8 extra branches, because there are 8 ways of putting 1x5 and 2x5 boards next to each other to fill a 5x5 space. Is it clearer now? – fafl Jul 26 '17 at 13:15
  • I am sorry. I tried hard for a whole day but I still don't understand. Can you please break down your approach. – rd10 Jul 27 '17 at 06:38
  • I do understand now, how to get the recurrence relation when there's one tile but getting hard time to make it with two tiles. Thank you in advance. – rd10 Jul 27 '17 at 06:40
  • I did it by computing the solutions of n=0, n=1, n=2 and so on until n=10 by hand and then wrote an algorithm that replicates the results. For example with n=3 you can either place three 1x5 boards, one 1x5 and one 2x5 or one 2x5 and one 1x5. So there are three ways to tile the floor. – fafl Jul 27 '17 at 08:17
  • Still confused :( should not there be 16 ways of putting 1*5 and 2*5 boards next to each other to fill a 5*5 space?? 1 with five vertical 1*5, 1 with five horizontal, 3 ways with two vertical 2*5 and one 1*5, 3 ways with two horizontal 2*5 and one 1*5, 4 ways with one vertical 2*5 and three 1*5, 4 ways with one horizontal 2*5 and three 1*5 – rd10 Jul 27 '17 at 14:02
  • @RajaDorji yes, but I already treat the horizontal ones beforehand by placing a 1x5 or 2x5, so only the vertical ones are interesting at this point. – fafl Jul 27 '17 at 15:11
  • Hi @fafl I think the line res += 8 + solve(x - 5) should be res += 8*solve(x - 5). Am i correct? – rd10 Jul 30 '17 at 18:08
  • @RajaDorji ah yes, of course, I'll edit my answer. I didn't check for larger n :) – fafl Jul 31 '17 at 10:59