AutoRun.java
/***<br /> * 解決騎士巡遊問題更具創意的方法之一是由J. C. Warnsdorff在1823年提出的。其規則是:騎士總是移向具有最少出口且沒有到達過的方格之一。<br /> * @author Administrator<br /> *<br /> */</p><p>public class AutoRun {</p><p>//int[][] map = new int[8][8];//二維數組儲存走過的地圖資料,</p><p>//"never pass"表示沒走過,"pass"表示走過<br />int step = 1;//走過了多少個沒有重複的格子<br />int x = 7;//象開始時的x座標<br />int y = 0;//象開始時的y座標</p><p>public static void main(String[] args) {<br />AutoRun run = new AutoRun();</p><p>}</p><p>public AutoRun(){</p><p>Node root = new Node(null ,0,0);//初始化根節點</p><p>Node childNode = run(root);</p><p>int[][] map = childNode.getMap();<br /> for (int i = 0; i < 8; i++) {//列印步驟<br /> for (int j = 0; j < 8; j++) {<br /> System.out.print( (map[i][j] + " ").substring(0, 4));<br /> }<br /> System.out.println();<br /> }</p><p>}</p><p>public Node run(Node node){<br /> if (node.getStep()==64) //如果走完了64個格子返回最後的節點,這是遞迴的允出準則<br /> return node;</p><p> node.canGo(node);//產生子節點<br /> List<Node> childNodes = node.getChildNodes();</p><p> if (childNodes == null)//沒子節點的話返回null<br /> return null;</p><p> for (int i = 0; i < childNodes.size(); i++) {//遞迴調用run<br /> Node currentNode = run(childNodes.get(i));<br /> return currentNode;<br /> }<br /> return null;</p><p>}</p><p>}<br />
Node.java
public class Node {<br />private int x;//節點的x座標<br />private int y;//節點的y座標<br />private int step;//走到了第幾步<br />private List<Node> childNodes = new ArrayList<Node>();// 子節點<br />private Node fatherNode = null;//父節點<br /> private int[][] map = new int[8][8];//走過的地圖</p><p>/***<br /> * Node的建構函式,傳入它的父節點和要走的,x、y的座標<br /> *<br /> * @param x 要走的x座標<br /> * @param y 要走的y座標<br /> *<br /> */<br />public Node(Node fatherNode, int x, int y) {<br />this.fatherNode = fatherNode;<br />this.x = x;<br />this.y = y;<br />if (fatherNode != null) {<br />this.step = this.fatherNode.step + 1;//增加步數</p><p>for (int i = 0; i < fatherNode.map.length; i++)//把父節點的地圖傳給子節點<br />for (int j = 0; j < fatherNode.map[i].length; j++)<br />this.map[i][j] = fatherNode.map[i][j];</p><p>} else<br />this.step = 1;//如果是跟節點的話步數從1開始<br />this.map[x][y] = this.step;//把node所在的地圖位置的值設定為當前的步數<br />/*測試步驟<br />for (int i = 0; i < 8; i++) {<br /> for (int j = 0; j < 8; j++) {<br /> System.out.print( (this.map[i][j] + " ").substring(0, 4));<br /> }<br /> System.out.println();<br /> }</p><p> System.out.println();<br /> System.out.println();<br /> System.out.println();<br /> */<br />}</p><p>public Node() {</p><p>}</p><p>/***<br /> * 根據輸入的node節點,產生它能走的子節點,並根據子節點的子節點個數從小到大排序<br /> *<br /> * @param node 傳入的節點<br /> */<br />public void canGo(Node node) {<br />int x = node.getX();<br />int y = node.getY();<br />if (checkOrient(x,y,1)) {<br />Node data = new Node(node, x - 2, y + 1);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,2)) {<br />Node data = new Node(node, x - 1, y + 2);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,3)) {<br />Node data = new Node(node, x + 1, y + 2);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,4)) {<br />Node data = new Node(node, x + 2, y + 1);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,5)) {<br />Node data = new Node(node, x + 2, y - 1);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,6)) {<br />Node data = new Node(node, x + 1, y - 2);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,7)) {<br />Node data = new Node(node, x - 1, y - 2);<br />node.AddChildNode(data);</p><p>}<br />if (checkOrient(x,y,8)) {<br />Node data = new Node(node, x - 2, y - 1);<br />node.AddChildNode(data);</p><p>}</p><p>for (int i = 0; i < childNodes.size(); i++)<br /> for (int j = 0; j < childNodes.size() - i - 1; j++)<br /> if (childNodes.get(j).getChildNodeCount() > childNodes.get(j + 1).getChildNodeCount()) {<br /> Collections.swap(childNodes, j, j + 1);</p><p> }</p><p>}</p><p>/***<br /> * 檢查這個方向是否可行<br /> * @param x x座標<br /> * @param y y座標<br /> * @param orient 方向參數,1-8,8個方向<br /> * @return<br /> */<br />public boolean checkOrient(int x,int y,int orient){<br />switch (orient){<br />case 1:<br />return checkBound(x - 2, y + 1) && !checkHavePass(x - 2, y + 1);<br />case 2:<br />return checkBound(x - 1, y + 2) && !checkHavePass(x - 1, y + 2);<br />case 3:<br />return checkBound(x + 1, y + 2) && !checkHavePass(x + 1, y + 2);<br />case 4:<br />return checkBound(x + 2, y + 1) && !checkHavePass(x + 2, y + 1);<br />case 5:<br />return checkBound(x + 2, y - 1) && !checkHavePass(x + 2, y - 1);<br />case 6:<br />return checkBound(x + 1, y - 2) && !checkHavePass(x + 1, y - 2);<br />case 7:<br />return checkBound(x - 1, y - 2) && !checkHavePass(x - 1, y - 2);<br />case 8:<br />return checkBound(x - 2, y - 1) && !checkHavePass(x - 2, y - 1);</p><p>}<br />return false;<br />}</p><p>/***<br /> * 統計該節點的子節點個數<br /> * @return 子節點個數<br /> */<br />public int getChildNodeCount() {<br /> int count = 0;<br /> if (checkOrient(x,y,1)) count++;<br /> if (checkOrient(x,y,2)) count++;<br /> if (checkOrient(x,y,3)) count++;<br /> if (checkOrient(x,y,4)) count++;<br /> if (checkOrient(x,y,5)) count++;<br /> if (checkOrient(x,y,6)) count++;<br /> if (checkOrient(x,y,7)) count++;<br /> if (checkOrient(x,y,8)) count++;<br /> return count;<br /> }</p><p>/***<br /> * 檢查是否之前到過這個格<br /> *<br /> * @param x 象的x座標<br /> * @param y 象的y座標<br /> * @return<br /> */<br />private boolean checkHavePass(int x, int y) {<br />if (map[x][y] != 0) {<br />return true;<br />}<br />return false;<br />}</p><p>/***<br /> * 檢查是否越界,即座標是否在0-7的範圍內<br /> *<br /> * @param x 象的x座標<br /> * @param y 象的y座標<br /> * @return 象是否可行<br /> */<br />private boolean checkBound(int x, int y) {<br />if (x >= 0 && x < 8 && y >= 0 && y < 8)<br />return true;<br />return false;<br />}</p><p>/***<br /> * 增加子節點<br /> *<br /> * @param stepData<br /> */<br />public void AddChildNode(Node node) {<br />childNodes.add(node);<br />}</p><p>public List<Node> getChildNodes() {<br />return childNodes;<br />}</p><p>public void setChildNodes(List<Node> childNodes) {<br />this.childNodes = childNodes;<br />}</p><p>public Node getFatherNode() {<br />return fatherNode;<br />}</p><p>public void setFatherNode(Node fatherNode) {<br />this.fatherNode = fatherNode;<br />}</p><p>public int getX() {<br />return x;<br />}</p><p>public void setX(int x) {<br />this.x = x;<br />}</p><p>public int getY() {<br />return y;<br />}</p><p>public void setY(int y) {<br />this.y = y;<br />}</p><p>public int getStep() {<br />return step;<br />}</p><p>public void setStep(int step) {<br />this.step = step;<br />}</p><p>public int[][] getMap() {<br />return map;<br />}</p><p>public void setMap(int[][] map) {<br />this.map = map;<br />}</p><p>}<br />