|
| 1 | +package com.hspedu; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | +import java.awt.*; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Comparator; |
| 7 | +public class HorseChessBoard { |
| 8 | + |
| 9 | + //定义属性 |
| 10 | + private static int X = 6; // 表示col |
| 11 | + private static int Y = 6; // 表示row |
| 12 | + private static int[][] chessBoard = new int[Y][X]; //棋盘 |
| 13 | + private static boolean[] visited = new boolean[X * Y];//记录某个位置是否走过 |
| 14 | + private static boolean finished = false; //记录马儿是否遍历完棋盘. |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + |
| 18 | + int row = 2; |
| 19 | + int col = 2; |
| 20 | + |
| 21 | + //测试一把 |
| 22 | + long start = System.currentTimeMillis(); |
| 23 | + traversalChessBoard(chessBoard, row - 1, col - 1, 1); |
| 24 | + long end = System.currentTimeMillis(); |
| 25 | + |
| 26 | + System.out.println("遍历耗时=" + (end - start)); |
| 27 | + //输出当前这个棋盘的情况 |
| 28 | + for (int[] rows : chessBoard) { |
| 29 | + for (int step : rows) {//step 表示 该位置是马儿应该走的第几步 |
| 30 | + System.out.print(step + "\t"); |
| 31 | + } |
| 32 | + System.out.println(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + //写一个方法,对ps的各个位置,可以走的下一个位置的次数进行排序, 把可能走的下一个位置从小到大排序 |
| 37 | + public static void sort(ArrayList<Point> ps) { |
| 38 | + ps.sort(new Comparator<Point>() { |
| 39 | + @Override |
| 40 | + public int compare(Point o1, Point o2) { |
| 41 | + return next(o1).size() - next(o2).size(); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + //编写最核心的算法,遍历棋盘,如果遍历成功,就把 finished 设置为true , |
| 47 | + //并且,将马儿走的每一步step,记录到 chessBoard |
| 48 | + public static void traversalChessBoard(int[][] chessBoard, int row, int col, int step) { |
| 49 | + |
| 50 | + //先把step 记录到 chessBoard |
| 51 | + chessBoard[row][col] = step; |
| 52 | + //把这个位置,设置为已经访问 |
| 53 | + visited[row * X + col] = true; |
| 54 | + //获取当前这个位置可以走的下一个位置有哪些 |
| 55 | + ArrayList<Point> ps = next(new Point(col, row)); // 注意这里的处理: col - X , row - Y |
| 56 | + sort(ps);// 排序:我们应该选择下一个的下一个可以走的位置较少的点,开始走,这样可以减少回溯 |
| 57 | + //遍历 |
| 58 | + while (!ps.isEmpty()) { |
| 59 | + //取出当前这个 ps 第一个位置(点) |
| 60 | + Point p = ps.remove(0); |
| 61 | + //判断该位置是否走过,如果没有走过,我们就递归遍历 |
| 62 | + if (!visited[p.y * X + p.x]) { |
| 63 | + //递归遍历 |
| 64 | + traversalChessBoard(chessBoard, p.y, p.x, step + 1); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + //当退出while,看看是否遍历成功, 如果没有成功,就重置相应的值,然后进行回溯 |
| 69 | + if (step < X * Y && !finished) { |
| 70 | + //重置 |
| 71 | + chessBoard[row][col] = 0; |
| 72 | + visited[row * X + col] = false; |
| 73 | + } else { |
| 74 | + finished = true; |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + //编写方法,可以获取当前位置,可以走的下一步的所有位置(Point表示 x,y) |
| 80 | + public static ArrayList<Point> next(Point curPoint) { |
| 81 | + |
| 82 | + //创建一个ArrayList |
| 83 | + ArrayList<Point> ps = new ArrayList<>(); |
| 84 | + |
| 85 | + //创建一个Point对象(点/位置), 准备放入到 ps |
| 86 | + Point p1 = new Point(); |
| 87 | + |
| 88 | + //判断在 curPoint 是否可以走如下位置,如果可以走,就将该点(Point) 放入到ps |
| 89 | + |
| 90 | + //判断是否可以走5位置 |
| 91 | + if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) { |
| 92 | + ps.add(new Point(p1)); //这里一定要new Point |
| 93 | + } |
| 94 | + //判断是否可以走6位置 |
| 95 | + if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) { |
| 96 | + ps.add(new Point(p1)); //这里一定要new Point |
| 97 | + } |
| 98 | + //判断是否可以走7位置 |
| 99 | + if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) { |
| 100 | + ps.add(new Point(p1)); //这里一定要new Point |
| 101 | + } |
| 102 | + //判断是否可以走0位置 |
| 103 | + if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) { |
| 104 | + ps.add(new Point(p1)); //这里一定要new Point |
| 105 | + } |
| 106 | + //判断是否可以走1位置 |
| 107 | + if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) { |
| 108 | + ps.add(new Point(p1)); //这里一定要new Point |
| 109 | + } |
| 110 | + //判断是否可以走2位置 |
| 111 | + if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) { |
| 112 | + ps.add(new Point(p1)); //这里一定要new Point |
| 113 | + } |
| 114 | + //判断是否可以走3位置 |
| 115 | + if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) { |
| 116 | + ps.add(new Point(p1)); //这里一定要new Point |
| 117 | + } |
| 118 | + //判断是否可以走4位置 |
| 119 | + if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) { |
| 120 | + ps.add(new Point(p1)); //这里一定要new Point |
| 121 | + } |
| 122 | + return ps; |
| 123 | + } |
| 124 | +} |
0 commit comments