zcq

    1. package com.atguigu.horse;
    2. import java.awt.Point;
    3. import java.util.ArrayList;
    4. import java.util.Comparator;
    5. public class HorseChessboard {
    6. private static int X; // 棋盘的列数
    7. private static int Y; // 棋盘的行数
    8. //创建一个数组,标记棋盘的各个位置是否被访问过
    9. private static boolean visited[];
    10. //使用一个属性,标记是否棋盘的所有位置都被访问
    11. private static boolean finished; // 如果为true,表示成功
    12. public static void main(String[] args) {
    13. System.out.println("骑士周游算法,开始运行~~");
    14. //测试骑士周游算法是否正确
    15. X = 8;
    16. Y = 8;
    17. int row = 1; //马儿初始位置的行,从1开始编号
    18. int column = 1; //马儿初始位置的列,从1开始编号
    19. //创建棋盘
    20. int[][] chessboard = new int[X][Y];
    21. visited = new boolean[X * Y];//初始值都是false
    22. //测试一下耗时
    23. long start = System.currentTimeMillis();
    24. traversalChessboard(chessboard, row - 1, column - 1, 1);
    25. long end = System.currentTimeMillis();
    26. System.out.println("共耗时: " + (end - start) + " 毫秒");
    27. //输出棋盘的最后情况
    28. for(int[] rows : chessboard) {
    29. for(int step: rows) {
    30. System.out.print(step + "\t");
    31. }
    32. System.out.println();
    33. }
    34. }
    35. /**
    36. * 完成骑士周游问题的算法
    37. * @param chessboard 棋盘
    38. * @param row 马儿当前的位置的行 从0开始
    39. * @param column 马儿当前的位置的列 从0开始
    40. * @param step 是第几步 ,初始位置就是第1步
    41. */
    42. public static void traversalChessboard(int[][] chessboard, int row, int column, int step) {
    43. chessboard[row][column] = step;
    44. //row = 4 X = 8 column = 4 = 4 * 8 + 4 = 36
    45. visited[row * X + column] = true; //标记该位置已经访问
    46. //获取当前位置可以走的下一个位置的集合
    47. ArrayList<Point> ps = next(new Point(column, row));
    48. //对ps进行排序,排序的规则就是对ps的所有的Point对象的下一步的位置的数目,进行非递减排序
    49. sort(ps);
    50. //遍历 ps
    51. while(!ps.isEmpty()) {
    52. Point p = ps.remove(0);//取出下一个可以走的位置
    53. //判断该点是否已经访问过
    54. if(!visited[p.y * X + p.x]) {//说明还没有访问过
    55. traversalChessboard(chessboard, p.y, p.x, step + 1);
    56. }
    57. }
    58. //判断马儿是否完成了任务,使用 step 和应该走的步数比较 ,
    59. //如果没有达到数量,则表示没有完成任务,将整个棋盘置0
    60. //说明: step < X * Y 成立的情况有两种
    61. //1. 棋盘到目前位置,仍然没有走完
    62. //2. 棋盘处于一个回溯过程
    63. if(step < X * Y && !finished ) {
    64. chessboard[row][column] = 0;
    65. visited[row * X + column] = false;
    66. } else {
    67. finished = true;
    68. }
    69. }
    70. /**
    71. * 功能: 根据当前位置(Point对象),计算马儿还能走哪些位置(Point),并放入到一个集合中(ArrayList), 最多有8个位置
    72. * @param curPoint
    73. * @return
    74. */
    75. public static ArrayList<Point> next(Point curPoint) {
    76. //创建一个ArrayList
    77. ArrayList<Point> ps = new ArrayList<Point>();
    78. //创建一个Point
    79. Point p1 = new Point();
    80. //表示马儿可以走5这个位置
    81. if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) {
    82. ps.add(new Point(p1));
    83. }
    84. //判断马儿可以走6这个位置
    85. if((p1.x = curPoint.x - 1) >=0 && (p1.y=curPoint.y-2)>=0) {
    86. ps.add(new Point(p1));
    87. }
    88. //判断马儿可以走7这个位置
    89. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
    90. ps.add(new Point(p1));
    91. }
    92. //判断马儿可以走0这个位置
    93. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
    94. ps.add(new Point(p1));
    95. }
    96. //判断马儿可以走1这个位置
    97. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
    98. ps.add(new Point(p1));
    99. }
    100. //判断马儿可以走2这个位置
    101. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
    102. ps.add(new Point(p1));
    103. }
    104. //判断马儿可以走3这个位置
    105. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
    106. ps.add(new Point(p1));
    107. }
    108. //判断马儿可以走4这个位置
    109. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
    110. ps.add(new Point(p1));
    111. }
    112. return ps;
    113. }
    114. //根据当前这个一步的所有的下一步的选择位置,进行非递减排序, 减少回溯的次数
    115. public static void sort(ArrayList<Point> ps) {
    116. ps.sort(new Comparator<Point>() {
    117. @Override
    118. public int compare(Point o1, Point o2) {
    119. // TODO Auto-generated method stub
    120. //获取到o1的下一步的所有位置个数
    121. int count1 = next(o1).size();
    122. //获取到o2的下一步的所有位置个数
    123. int count2 = next(o2).size();
    124. if(count1 < count2) {
    125. return -1;
    126. } else if (count1 == count2) {
    127. return 0;
    128. } else {
    129. return 1;
    130. }
    131. }
    132. });
    133. }
    134. }