Way to solve Tiling Problems
Question 1: 10359 - Tiling
Given a board of size 2×n
Available tiles 2×1 and 2×2
Question: Find the number of ways to completely fill the board.
E.g when n = 3 , 5ways
consider the board of width n:
How can we fill the rightmost column :
1. By one 2×1 vertically.
2. By two 2×1 horizontally.
3. By one 2×2 either horizontally or vertically( since both are same).
Let f(n) be the number of ways of filling a board of size 2×n.
So f(n) = f(n-1) + f(n-2) + f(n-2)
Now, let's determine the base cases.
1. f(0) = 1, because if we have to find f(1) then , f(1) = f(0) + f(-1) + f(-1) , since f(-1) not exist, so f(-1) = 0 then f(1) = f(0) + 0 + 0 => f(1) = f(0)
since there is one way to fill the board of size 2×1 by using one 2×1 tile. It means that f(0) must
be equal to 1.
2. f(1) = 1
Code:
Complexity: O(n)Question 2. GNY07H - Tiling a Grid With Dominoes
Given a board of size 4×n.
Available tile 2×1
Question: Find the number of ways to completely fill the board.
E.g when n = 2 , 5ways
consider the board of width n:
How can we fill the rightmost column:
1. By two 2×1 vertically.
2. By four 2×1 horizontally.
3. By two 2×1 horizontally in the bottom and one, 2×1 vertically in the top.
4. By one 2×1 vertically in the bottom and two, 2×1 horizontally in the top.
Note: Both 3 and 4 are the same, the only difference is that, the positions but the number of ways of 3 = number of ways of 4.
5. By one 2×1 horizontally in the top, one 2×1 vertically in the middle, and one 2×1 in the bottom.
Let f(n) be the number of ways of filling a board of size 4×n.
Let g(n) be the number of ways of filling a board of size 4×n of this type.
Let h(n) be the number of ways of filling a board of size 4×n of this type.
So f(n) = f(n-1) + f(n-2) + g(n-2) + g(n-2) + h(n-2)
🤓Now let's find g(n).
How can we fill the vacant 2×1 space in g(n):
1. By one 2×1 vertically, then the shape after doing this will be like f(n)
So, g(n) = f(n) + g(n-1)
🤓Now let's find h(n).
How can we fill the vacant 2×1 space in h(n):
1. By one 2×1 vertically, then the shape after doing this will be like f(n)
2. By adding two 2×1 horizontally.
But this shape doesn't like any of f, g or h. So we have to do something so that its shape will belong to any of f, g, or h type.
If one 2×1 horizontally on top and one 2×1 horizontally in the bottom will add, then its shape will similar to h.
So, h(n) = f(n) + h(n-2)
Now, let's determine the base cases.
1. f(0) = 1
2. f(1) = 1
3. g(0) = f(0) + g(-1) = 1 + 0 = 1
4. g(1) = f(1) + g(0) = 1 + 1 = 2
5. h(0) = f(0) + h(-2) = 1 + 0 = 1
6. h(1) = f(1) + h(-1) = 1 + 0 = 1
Code:
Complexity: O(n)
Bonus: M3TILE - LATGACH3
Video Explanation:
Happy coding!👨💻
Great Explanation. Thanks :D
ReplyDeleteThanks :)
DeleteHi,
ReplyDeleteBelow equation:
f(n) = f(n-1) + f(n-2) + g(n-2) + g(n-2) + h(n-2)
it should be g(n -1) and h (n-1) right ?
can you please explain ?
no, it will be g(n-2) and h(n-2) because in both case we are putting the tile horizontally and then tile will cover 2 length. So we left with n-2 space.
DeleteHope It will help you :)
This comment has been removed by the author.
DeleteThis comment has been removed by the author.
Deleteequations can be of the form:
Deletef(n)=f(n-1) + f(n-2) + g(n-1) + g(n-1) + h(n-1)
g(n)=g(n-2) + f(n-1) + f(i-2)
h(n)=h(n-2) + f(n-1)
initial values:
f[0]=1; f[1]=1;
g[0]=0; g[1]=1;
h[0]=0; h[1]=1;
These recurrences giving wrong answer on SPOJ.
DeleteCan you please post your corrected code.
okay thanks..
DeleteGOT IT..
Hi, i was just trying the bonus problem M3TILE on spoj, i am not getting the mistake in my recurrence relation. Can you please have a look and help me out at it.
ReplyDeletell solve(ll n) {
ll f[n+10]={0}, g[n+10]={0};
f[0]=1, f[1]=0, f[2]=3, f[3]=0;
g[0]=0, g[1]=1, g[2]=0, g[3]=1;
if(n<4){
return f[n];
}
for(int i=4;i<=n; i++) {
f[i] = f[i-2] + 2*g[i-1];
g[i] = f[i-1] + g[i-2];
}
return f[n];
}
I think your base cases are wrong.
DeleteThe base cases are:
f[0] = g[0] = 1
f[1] = g[1] = 0
and recurrence relations are:
f[i] = f[i-2] + 2 * g[i-2];
g[i] = f[i] + g[i-2];
Hi, one more doubt in the 2nd example you explained above for 4xn using 2x1.
ReplyDeleteIn this when you are finding g(n), then can the recurrence relation can be like:-
g(n) = f(n) + g(n-2).
why you have taken g(n-1), here g(n-1) as you shown in image, will be the case when the upper 2x1 is empty but here we are taking the cases for g(n) in which the lower 2x1 part is empty.
Are you getting my point?
Actually what i mean is, if you observe images of g(n) and g(n-1) then they are not of same type. I think there should be some horizontal 2x1 tiles added to g(n-1) so that it becomes g(n-2) whose pattern look like that of g(n) initially.
Deletealso if possible then can you give link to this problem.
https://codeforces.com/topic/60968/en2?locale=en
DeleteAtlast a clear explanation of tiling problems have been found. :)
ReplyDeletethank you :)
DeleteWhat does the t represent in the code for Question 2?
ReplyDeletet is nothing but the number of test cases. You can ignore t if you want to run your code for one test case.
Deletef(n)=f(n-2)+2*g(n-1)
ReplyDeleteg(n)=f(n-2)+g(n-2)