forked from sanghaisubham/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiling.cpp
More file actions
66 lines (61 loc) · 1.59 KB
/
Tiling.cpp
File metadata and controls
66 lines (61 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*BIT MASKING*/
#include<bits/stdc++.h>
using namespace std;
long long n;
long long dp[20][20][4][1001];
long long rec(long long current,long long next,long long r,long long c)
{
if(c==n)
return 1;
if(r==4 && current==15)
{
return rec(next,0,0,c+1);
}
if(r==4 && current!=15)
return 0;
if(r>4)
return 0;
long long s=0;
if(dp[current][next][r][c]!=-1)
return dp[current][next][r][c];
if(((c+1)<n)&& (((current&(1<<r))==0)&&((next&(1<<r))==0)) && (((current&(1<<r))==0)&&((current&(1<<(r+1)))==0))) //Case when we can either select vertically or horizontally
{
s+=rec((current|(1<<r)),(next|(1<<r)),r+1,c)+rec((current|(1<<r)|(1<<(r+1))),next,r+2,c);
}
else if((c+1)<n) //when it is not last column
{
if(((current&(1<<r))==0)&&((next&(1<<r))==0))
s+=rec((current|(1<<r)),(next|(1<<r)),r+1,c);
else if(((current&(1<<r))==0)&&((current&(1<<(r+1)))==0))
{
s+=rec((current|(1<<r)|(1<<(r+1))),next,r+2,c);
}
else
s+=rec(current,next,r+1,c);
}
else //when it is the last column and we can only place it horizontally only
{
if(((current&(1<<r))==0)&&((current&(1<<(r+1)))==0))
{
s+=rec((current|(1<<r)|(1<<(r+1))),next,r+2,c);
}
else
s+=rec(current,next,r+1,c);
}
return dp[current][next][r][c]=s;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(false);
long long t;
cin>>t;
long long l=0;
while(t--)
{
memset(dp,-1,sizeof(dp));
l++;
cin>>n;
cout<<l<<" "<< rec(0,0,0,0)<<"\n";
}
}