搭积木
小明最近喜欢搭数字积木,
一共有10块积木,每个积木上有一个数字,0~9。
搭积木规则:
每个积木放到其它两个积木的上面,并且一定比下面的两个积木数字小。
最后搭成4层的金字塔形,必须用完所有的积木。
下面是两种合格的搭法:
0
1 2
3 4 5
6 7 8 9
0
3 1
7 5 2
9 8 6 4
请你计算这样的搭法一共有多少种?
请填表示总数目的数字。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
public class S2016JavaA_3 {
public static int count=0;
public static void main(String[] args) {
int[][] a=new int[4][4];
boolean[] used=new boolean[10];
DFS(a,used,0,0);
System.out.println(count);
}
public static void DFS(int[][] a,boolean[] used,int row,int col){
if(col>row) {
row++;
col=0;
}
if(row==4){
if(isValid(a,0,0))
count++;
return ;
}
for(a[row][col]=0;a[row][col]<=9;a[row][col]++){
if(used[a[row][col]]==true)
continue;
else{
used[a[row][col]]=true;
DFS(a,used,row,col+1);
used[a[row][col]]=false;
}
}
}
public static boolean isValid(int[][] a,int row,int col){
if(row==3) return true;
boolean left=isValid(a,row+1,col);
boolean right=isValid(a,row+1,col+1);
return left&&right&&a[row][col]<a[row+1][col]&&a[row][col]<a[row+1][col+1];
}
}