题目大意:给出一个N*4的方格,要求你用2*2,2*1,1*2,1*1的格子将其填满,2*2的格子必须要有,问有多少中填满N*4方格的方法
解题思路:回溯
#include<cstdio>
#include<cstring>
int ans[5] = {0,0,18};
int vis[5][4],res;
bool flag;
bool judge(int cur) {
if(!flag)
return false;
for(int i = 0; i < cur; i++)
for(int j = 0; j < 4; j++)
if(!vis[i][j])
return false;
return true;
}
void DFS(int x,int y,int cur) {
if(judge(cur)) {
res++;
return ;
}
if(y == 4) {
x++;
y = 0;
}
for(int i = x; i < cur; i++)
for(int j = y; j < 4; j++) {
if(!flag && i + 1 < cur && j + 1 < 4 && !vis[i][j] && !vis[i][j+1] && !vis[i+1][j] && !vis[i+1][j+1]) {
vis[i][j] = vis[i][j+1] = vis[i+1][j] = vis[i+1][j+1] = 1;
flag = true;
DFS(i,j+2,cur);
flag = false;
vis[i][j] = vis[i][j+1] = vis[i+1][j] = vis[i+1][j+1] = 0;
}
if(i + 1 < cur && !vis[i][j] && !vis[i+1][j]) {
vis[i][j] = vis[i+1][j] = 1;
DFS(i,j+1,cur);
vis[i][j] = vis[i+1][j] = 0;
}
if(j + 1 < 4 && !vis[i][j] && !vis[i][j+1]) {
vis[i][j] = vis[i][j+1] = 1;
DFS(i,j+2,cur);
vis[i][j] = vis[i][j+1] = 0;
}
if(!vis[i][j]) {
vis[i][j] = 1;
DFS(i,j+1,cur);
vis[i][j] = 0;
}
if(j == 3)
y = 0;
}
}
int main() {
int test,temp;
memset(vis,0,sizeof(vis));
flag = false;
res = 0;
DFS(0,0,3);
ans[3] = res;
memset(vis,0,sizeof(vis));
flag = false;
res = 0;
DFS(0,0,4);
ans[4] = res;
scanf("%d",&test);
while(test--) {
scanf("%d",&temp);
printf("%d\n",ans[temp]);
}
return 0;
}