package Algorithm;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
public class Horse {
private static int X;//列數
private static int Y;//行數
//創建數組 標記各個位置是否被訪問過
private static boolean isvisited[];
//使用一個屬性標記是否棋盤所有位置都被訪問過了
private static boolean isfinished;//如果true成功
//完成馬踏棋盤row行 colum列step當前第幾步 step初始爲1
public static void traversalChessboard(int[][] chessboard,int row,int colum,int step) {
chessboard[row][colum] = step;
//System.out.println("菜雞淚目!!!"+chessboard[row][colum]);
isvisited[row*X + colum] = true;
ArrayList<Point> ps = next(new Point(colum,row));
//遍歷ps
while(!ps.isEmpty()) {
Point p = (Point) ps.remove(0);
//判斷該點是否已經訪問過
if(!isvisited[p.y*X+p.x]) {
//判斷該點是否已經被訪問過
traversalChessboard(chessboard,p.y,p.x,step+1);
}
}
//step < X* Y成立的情況有兩種 棋盤目前爲止仍然沒有走完 Or 棋盤走完了但是在回漱中
if(step < X*Y && (!isfinished)) {
chessboard[row][colum] = 0;
isvisited[row*X + colum] = false;
System.out.println("是這裏");
} else {
isfinished = true;
}
}
public static ArrayList<Point> next(Point curPoint){
ArrayList<Point> ps = new ArrayList<Point>();
//創建一個Point
Point p1 = new Point();
//判斷馬兒能不能走那些位置
if((curPoint.x - 2) >= 0) {
p1.x = curPoint.x - 2;
if(curPoint.y - 1 >= 0) {
p1.y = curPoint.y - 1;
ps.add(p1);
}
if(curPoint.y + 1 < Y)
p1.y = curPoint.y + 1;
ps.add(p1);
}
if((curPoint.x - 1) >= 0) {
p1.x = curPoint.x - 1;
if(curPoint.y - 2 >= 0) {
p1.y = curPoint.y - 2;
ps.add(p1);
}
if(curPoint.y + 2 < Y)
p1.y = curPoint.y + 2;
ps.add(p1);
}
if((curPoint.x + 2) < X) {
p1.x = curPoint.x + 2;
if(curPoint.y - 1 >= 0) {
p1.y = curPoint.y - 1;
ps.add(p1);
}
if(curPoint.y + 1 < Y)
p1.y = curPoint.y + 1;
ps.add(p1);
}
if((curPoint.x + 1) < X) {
p1.x = curPoint.x + 1;
if(curPoint.y - 2 >= 0) {
p1.y = curPoint.y - 2;
ps.add(p1);
}
if(curPoint.y + 2 < Y)
p1.y = curPoint.y + 2;
ps.add(p1);
}
// if(((p1.x = curPoint.x - 2)>= 0) &&((p1.y = curPoint.y - 1)>=0) ){
// ps.add(p1);
// }
// if((p1.x = curPoint.x -1 ) >= 0 &&(p1.y = curPoint.y - 2)>=0)
// ps.add(p1);
// if((p1.x = curPoint.x + 1 )>= 0 &&(p1.x = curPoint.x + 1 ) < X &&(p1.y = curPoint.y - 2)>=0)
// ps.add(p1);
// if((p1.x = curPoint.x + 2 )>= 0 &&(p1.x = curPoint.x + 2 ) < X &&(p1.y = curPoint.y - 1)>=0)
// ps.add(p1);
// if((p1.x = curPoint.x + 2 )>=0&&(p1.x = curPoint.x + 2 ) < X &&(p1.y = curPoint.y + 1)>= 0&&(p1.y = curPoint.y + 1) < Y)
// ps.add(p1);
// if((p1.x = curPoint.x + 1 ) >= 0&&(p1.x = curPoint.x + 1 ) < X && (p1.y = curPoint.y + 2) >=0 &&(p1.y = curPoint.y + 2) < Y)
// ps.add(p1);
// if((p1.x = curPoint.x - 1 ) >=0 &&(p1.y = curPoint.y + 2) >= 0 && (p1.y = curPoint.y + 2) < Y)
// ps.add(p1);
// if((p1.x = curPoint.x -2 ) >=0 &&(p1.y = curPoint.y + 1) >= 0 && (p1.y = curPoint.y + 1) < Y)
// ps.add(p1);
return ps;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//測試騎士周遊算法是否正確
X = 8;
Y = 8;
int row = 1;
int colum = 1; //馬兒初始位置的列
//創建棋盤
int[][] chessboard = new int[X][Y];
isvisited = new boolean[X*Y]; //初始爲false
//測試一下horse
long start = System.currentTimeMillis();
traversalChessboard(chessboard,row-1,colum-1,1);
long end = System.currentTimeMillis();
System.out.println("共耗時:"+ (end-start));
//輸出棋盤的最後情況
for(int i = 0; i < chessboard.length; i++) {
for(int j = 0; j< chessboard[0].length;j++) {
System.out.print(chessboard[i][j] +" ");
}
System.out.println();
}
}
}