链接:https://ac.nowcoder.com/acm/contest/301/B
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
小乐乐想要给自己搭建一个积木城堡。
积木城堡我们假设为nm的平面矩形。
小乐乐现在手里有12,2*1两种地砖。
小乐乐想知道自己有多少种组合方案。
输入描述:
第一行输入整数n,m。(1<=n,m<=10)
输出描述:
输出组合方案数。
示例1
输入
2 3
输出
3
说明
示例2
输入
1 3
输出
0
示例3
输入
2 5
输出
8
#include<cstdio>
#include<cstring>
using namespace std;
bool book[15][15];
int s,n,m;
int ans;
bool judge(int x,int y,int t){
if(t==0){//1*2的地砖
if(y+1==m) return false;
if(!book[x][y+1]) return true;
}
else{ //2*1的地砖
if(x+1==n) return false;
if(!book[x+1][y]) return true;
}
return false;
}
void dfs(int p){
if(p==s){
ans++;
return ;
}
int x=p/m;//第几行
int y=p%m;//第几列
if(book[x][y]) return dfs(p+1);//一定要return,因为这里已经有砖了,就不摆放了
book[x][y]=true;
for(int i=0;i<=1;i++){//两种砖
if(i==0){ //1*2的砖
if(judge(x,y,i)){
book[x][y+1]=true;
dfs(p+1);
book[x][y+1]=false;
}
}
else{ //2*1的砖
if(judge(x,y,i)){
book[x+1][y]=true;
dfs(p+1);
book[x+1][y]=false;
}
}
}
book[x][y]=false;
return ;
}
int main(){
scanf("%d%d",&n,&m);
ans=0;
s=n*m;
dfs(0);
printf("%lld\n",ans);
return 0;
}