打印结果:
[1, 8, 11, 16, 3, 18, 13, 64]
[10, 27, 2, 7, 12, 15, 4, 19]
[53, 24, 9, 28, 17, 6, 63, 14]
[26, 39, 52, 23, 62, 29, 20, 5]
[43, 54, 25, 38, 51, 22, 33, 30]
[40, 57, 42, 61, 32, 35, 48, 21]
[55, 44, 59, 50, 37, 46, 31, 34]
[58, 41, 56, 45, 60, 49, 36, 47]
4.代码优化
马儿不同的走法〈策略),会得到不同的结果,效率也会有影响(优化)
使用贪心算法对原来的算法优化
1 。我们获取当前位置,可以走的下一个位置的集合//获取当前位置可以走的下一个位置的集合
ps= next(new point(, row));
⒉我们斋要对ps 中所有的Point 的下一步的所有集合的数目,进行非递减排序,就ok ,
public class HorseChessBoard {public static int X;//棋盘的列数public static int Y;//棋盘的行数public static boolean[] isVisted;//标记棋盘的各个位置是否被访问过了public static boolean isFinshed; //表示所有位置都被访问过了public static void main(String[] args) {X = 8;Y = 8;isVisted = new boolean[X * Y];int x = 1, y = 1;int[][] chessBoard = new int[X][Y];travelChessBoard(chessBoard, x - 1, y - 1, 1);for (int[] ints : chessBoard) {System.out.println(Arrays.toString(ints));}}/*** 完成其实周游问题的算法** @param chessBoard 棋盘* @param row马儿当前在哪一行* @param column马儿当前在哪一列* @param step当前在第几步*/public static void travelChessBoard(int[][] chessBoard, int row, int column, int step) {chessBoard[row][column] = step;isVisted[row * X + column] = true; //标记位置已经被访问//获取当前位置可以走的位置ArrayList next = next(new Point(column, row));//对next进行排序,排序的规则是next的下一个的数目sort(next);while (!next.isEmpty()) {Point point = next.remove(0);//取出下一个可以访问的结点//判断此点是否访问过if (!isVisted[point.y * X + point.x]) {//没有访问过travelChessBoard(chessBoard, point.y, point.x, step + 1);}}//.判断马儿是否完成了任务,使用step和应该走的步数比较,// 如果没有达到数量,则表示没有完成任务,将整个棋盘置0if (step < X * Y && !isFinshed) {chessBoard[row][column] = 0;isVisted[row * X + column] = false;} else {isFinshed = true;}}public static ArrayList next(Point curPoint) {ArrayList points = new ArrayList<>();Point point = new Point();if ((point.x = curPoint.x - 2) >= 0 && (point.y = curPoint.y - 1) >= 0) {//5points.add(new Point(point));}if ((point.x = curPoint.x - 1) >= 0 && (point.y = curPoint.y - 2) >= 0) {//6points.add(new Point(point));}if ((point.x = curPoint.x + 1) < X && (point.y = curPoint.y - 2) >= 0) {//7points.add(new Point(point));}if ((point.x = curPoint.x + 2) < X && (point.y = curPoint.y - 1) >= 0) {//0points.add(new Point(point));}if ((point.x = curPoint.x + 2) < X && (point.y = curPoint.y + 1) < Y) {//1points.add(new Point(point));}if ((point.x = curPoint.x + 1) < X && (point.y = curPoint.y + 2) < Y) {//2points.add(new Point(point));}if ((point.x = curPoint.x - 1) >= 0 && (point.y = curPoint.y + 2) < Y) {//3points.add(new Point(point));}if ((point.x = curPoint.x - 2) >= 0 && (point.y = curPoint.y + 1) < Y) {//4points.add(new Point(point));}return points;}//根据当前这一步的下一步的选择位置,进行排序public static void sort(ArrayList next){next.sort(new Comparator() {@Overridepublic int compare(Point o1, Point o2) {ArrayList next1 = next(o1);ArrayList next2 = next(o2);return next1.size()-next2.size();}});}}
- ESP32使用TFT_eSPI库实现屏幕触摸
- 这1000道JAVA面试题,刷完50%妥妥的也能上岸
- Java实现 【数据结构课程设计报告】路径规划系统
- Windows 系统下实现间接命令执行
- Java swing中树状组件JTree的使用,左边点击不同菜单
- java毕业设计——基于JSP+JavaBean+sqlserver的在线购物系
- 您是哪种Java开发人员? 参加我们的Java测验找出答案!
- 三十、java版商城之移动商城楼层装修 Spring Cloud+SpringB
- JavaScript图片廊——实现点击小图片栏显示大图
- 8 【CesiumJS入门】自定义鼠标指针图标——地球范围内实现