- Stack.java : 定義堆疊的介面
- package DSwJ.S14;
- public interface Stack
{ - public T peek();
- public void push(T item);
- public T pop();
- public int size();
- public boolean isEmpty();
- }
- ALStack.java : 解法所使用的堆疊結構
- package DSwJ.S14;
- import java.util.ArrayList;
- public class ALStack
implements Stack { - private ArrayList
stackList = null;- public ALStack() {
- stackList = new ArrayList
(); - }
- public void clear(){stackList.clear();}
- public String display(){
- if(stackList.size()==0) return "[]";
- StringBuffer sb = new StringBuffer("[");
- sb.append(stackList.get(0));
- for(int i=1; i
- sb.append(", "+stackList.get(i));
- sb.append("]");
- return sb.toString();
- }
- public void copyTo(ArrayList
copy){ - for(int i=0; i
- }
- @Override
- public T peek() {
- if(stackList.size()==0) throw new EmptyStackException("Empty Stack");
- return stackList.get(stackList.size()-1);
- }
- @Override
- public T pop() {
- if(stackList.size()==0) throw new EmptyStackException("Empty Stack");
- return stackList.remove(stackList.size()-1);
- }
- @Override
- public void push(T item) {
- stackList.add(item);
- }
- @Override
- public int size() {
- return stackList.size();
- }
- @Override
- public boolean isEmpty() {
- return stackList.isEmpty();
- }
- }
- Queue8.java : 解題目的主程式
- package DSwJ.S14.apps;
- import java.util.ArrayList;
- import DSwJ.S14.ALStack;
- public class Queue8 {
- private int col=0;
- private int size=8;
- private ALStack
posStack = new ALStack(); - public Queue8(int c){this();col = c;}
- public Queue8(){}
- public Queue8(int s, int c) {size=s; col=c;}
- /**
- * BD 回到上一個點的下一個不會遭受已存在皇后攻擊的點
- * @return
- */
- protected boolean rollback(){
- int tmp;
- boolean flag = true;
- do{
- if(posStack.size()==0 || (posStack.size()==1 && posStack.peek()==size-1)) return false;
- while((tmp=posStack.pop())==(size-1)){
- if(posStack.size()==0)return false;
- };
- for(int i=tmp+1; i
- if(!attack(i)){
- posStack.push(i);
- flag = false;
- break;
- }
- }
- }while(flag);
- return true;
- }
- /**
- * BD 檢查是否位置有遭已存在的皇后的攻擊. 如遭攻擊則返回 True.
- * @param p
- * @return
- */
- protected boolean attack(int p) {
- if(posStack.size()==0) return false;
- ArrayList
copyList = new ArrayList (); - posStack.copyTo(copyList); // 複製Stack 內容到 ArrayList copyList
- int k;
- for(int i=copyList.size()-1; i>=0; i--) {
- k = copyList.get(i);
- if(k==p||(k+copyList.size()-i)==p||(k-copyList.size()+i)==p) {
- return true;
- }
- }
- return false;
- }
- /**
- * BD 開始找尋Solution
- * @return
- */
- protected boolean start() {
- if(posStack.size()==size) { //Next solution
- if(!rollback()){
- System.out.println("No more solution!");
- return false;
- }
- if(posStack.size() == size) return true;
- else return start();
- } else { //First solution
- while(posStack.size()
- if(posStack.isEmpty()){
- posStack.push(col);
- continue;
- } else {
- int i;
- for(i=0; i
- if(!attack(i)){
- posStack.push(i);
- break;
- }
- }
- if(i==size){
- if(!rollback()) {
- System.out.println("No more solution!");
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- protected void printTable(){
- ArrayList
copyList = new ArrayList (); - posStack.copyTo(copyList);
- int k;
- for(int i=0; i
- k = copyList.get(i);
- for(int j=0; j
- if(j==k) {
- System.out.print("[Q] ");
- continue;
- }
- System.out.print("[#] ");
- }
- System.out.println("->"+k);
- }
- }
- /**
- * BD 找出指定數目的解法, 並列印出來.
- * @param num
- */
- public void findSol(int num) {
- for(int i=0; i
- if(start()){
- System.out.printf("====================(Solution %2d)===================\n",i+1);
- printTable();
- }else {
- break;
- }
- }
- }
- public static void main(String args[]) {
- Queue8 queue = new Queue8(0);
- queue.findSol(93); // 找出 93 組解, 但其實總共只有 92組解
- }
- }
執行結果 :
...(以上省略)...
====================(Solution 91)===================
[#] [#] [#] [#] [#] [#] [#] [Q] ->7
[#] [#] [Q] [#] [#] [#] [#] [#] ->2
[Q] [#] [#] [#] [#] [#] [#] [#] ->0
[#] [#] [#] [#] [#] [Q] [#] [#] ->5
[#] [Q] [#] [#] [#] [#] [#] [#] ->1
[#] [#] [#] [#] [Q] [#] [#] [#] ->4
[#] [#] [#] [#] [#] [#] [Q] [#] ->6
[#] [#] [#] [Q] [#] [#] [#] [#] ->3
====================(Solution 92)===================
[#] [#] [#] [#] [#] [#] [#] [Q] ->7
[#] [#] [#] [Q] [#] [#] [#] [#] ->3
[Q] [#] [#] [#] [#] [#] [#] [#] ->0
[#] [#] [Q] [#] [#] [#] [#] [#] ->2
[#] [#] [#] [#] [#] [Q] [#] [#] ->5
[#] [Q] [#] [#] [#] [#] [#] [#] ->1
[#] [#] [#] [#] [#] [#] [Q] [#] ->6
[#] [#] [#] [#] [Q] [#] [#] [#] ->4
No more solution! <總共只有 92組解>
沒有留言:
張貼留言