using namespace std; long long f[25][4050]; bool adj[4050][4050]; //所有可能的转移 int W,H; void dfs(int from,int step,int to) { if(step == W) { adj[from][to] = 1; return; } if(((1 << step) & from) != 0) //已经放过,考察下一个 dfs(from,step + 1,to); else { if((step <= W - 2) && ((1 << (step + 1)) & from) == 0) //两块都是空的,可以横放 dfs(from,step + 2,to); dfs(from,step + 1,to | (1 << step)); //竖着放 } } int main() { while(scanf(“%d%d”,&W,&H)) { if(W==0&&H==0){ return 0; } if((W * H) % 2 == 1) //无解情况 { printf(“0\n”); } else { int maxn = 1 << W; memset(f,0,sizeof(f)); memset(adj,false,sizeof(adj)); int i,j,k; for(i = 0; i < maxn; i ++) //DFS构建矩阵 dfs(i,0,0); f[0][0] = 1; for(i = 0; i < H; i ++) for(j = 0; j < maxn; j ++) for(k = 0; k < maxn; k ++) if(adj[j][k]) f[i + 1][k] += f[i][j]; printf(“%lld\n”,f[H][0]); }
} return 0;
}
此处Shut down MasterX 记录 直接把代码给你吧,估计你现在也写不出来。
same as 走道铺砖
include
include
include
include
include
using namespace std;
long long f[25][4050];
bool adj[4050][4050]; //所有可能的转移
int W,H;
void dfs(int from,int step,int to)
{
if(step == W)
{
adj[from][to] = 1;
return;
}
if(((1 << step) & from) != 0) //已经放过,考察下一个
dfs(from,step + 1,to);
else
{
if((step <= W - 2) && ((1 << (step + 1)) & from) == 0) //两块都是空的,可以横放
dfs(from,step + 2,to);
dfs(from,step + 1,to | (1 << step)); //竖着放
}
}
int main()
{
while(scanf(“%d%d”,&W,&H))
{
if(W==0&&H==0){
return 0;
}
if((W * H) % 2 == 1) //无解情况
{
printf(“0\n”);
}
else
{
int maxn = 1 << W;
memset(f,0,sizeof(f));
memset(adj,false,sizeof(adj));
int i,j,k;
for(i = 0; i < maxn; i ++) //DFS构建矩阵
dfs(i,0,0);
f[0][0] = 1;
for(i = 0; i < H; i ++)
for(j = 0; j < maxn; j ++)
for(k = 0; k < maxn; k ++)
if(adj[j][k])
f[i + 1][k] += f[i][j];
printf(“%lld\n”,f[H][0]);
}
}
此处Shut down MasterX 记录
直接把代码给你吧,估计你现在也写不出来。
same as 走道铺砖