簡書不支持代碼格式码邻,蛋疼。瑰钮。冒滩。。
要求梗概:堆場5排浪谴,4層高开睡,放有次序的20個箱子進去,求有多少種方法苟耻。
那么也就是每個箱子都可以標號1-5篇恒,代表放的排的位置。
大致思路可以表示如下凶杖。deg【i】代表i排的箱子數(shù)目
就是一個遞歸思路胁艰,注意最后還是要deg[i]++,因為如果不++智蝠,遞歸套一層出來后所有箱子信息就是0了腾么,這樣就只有一個結果了。
import java.util.ArrayList;
import java.util.List;
/**
* Created by LeeTom on 2018/1/29.
*/
public class traversal {
public static final int height =4;//層高;
? ? public static final int row =5;//排數(shù)
? ? public static int []count =new int[]{height,height,height,height,height,height};//1*6的數(shù)組
? ? public static int total =0;//總數(shù)
? ? private static int m =height*row;
public static void main(String[] arg){
????????List L1 =new ArrayList<>();
? ? ? ? new traversal().findall(L1,m);
????????System.out.println("count: " +total);
}
private void findall( List L1,int m){
????if(m ==0){
????????????System.out.println();*/
? ? ? ? ? ? total++;
????????????return;
????}
????List L2;
????for(int i=1;i<=row;i++){
????L2 =new ArrayList();
????L2.addAll(L1);
????if(count[i]>0){
????L2.add(i);
????count[i]--;
????findall(L2,m-1);
????count[i]++;
}
}
}
}