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
|
#include <iostream>
#include <map>
using namespace std;
int h=12,w=9;
int area[][3]={{0,w,w+1},{0,w,w-1},{0,w+1,1},{0,w,1},{0,w,2*w},{0,1,2}};
bool inBoard(int x,int t){
switch(t){
case 1: return x/w<h-1 && x%w>0;
case 4: return x/w<h-2;
case 5: return x%w<w-2;
default: return x/w<h-1 && x%w<w-1;
}
}
bool canPut(bool board[],int x,int t){
if(!inBoard(x,t))return false;
for(int i=0;i<3;i++)if(board[x+area[t][i]])return false;
return true;
}
long shape(bool board[]){
long long s=0;
for(int i=0,j,t,p;i<w;i++,s=2*h*s+t+p)
for(p=0,t=0,j=0;j<h;j++)if(board[w*j+i]){
p=t;
t=j+1;
}
return s;
}
long long solve(map<long,long long> &memo,bool board[],int x){
long s = shape(board);
if(memo.find(s)!=memo.end()) return memo[s];
if(x==h*w) return 1;
if(board[x]) return solve(memo,board,x+1);
long long count=0;
for(int t=0;t<6;t++)
if(canPut(board,x,t)){
for(int i=0;i<3;i++)board[x+area[t][i]]=true;
count+=solve(memo,board,x+1);
for(int i=0;i<3;i++)board[x+area[t][i]]=false;
}
if(count>1) memo[s]=count;
return count;
}
int main(){
bool board[h*w];
map<long,long long> memo;
for(int i=0;i<h*w;i++)board[i]=false;
cout << solve(memo,board,0) << "\n";
}
|