马踏棋盘问题(骑士周游问题)及其优化算法java实现

来源:互联网 发布:医院设备科工资知乎 编辑:程序博客网 时间:2024/05/20 13:09

马踏棋盘问题java实现(骑士周游问题)实际上是图的深度优先搜索(DFS)的应用或者进一步理解为一颗8叉树的深度遍历

package ccnu.offer.tree;import java.awt.Point;import java.util.ArrayList;import java.util.Scanner;public class Demo07 {private static int X; // 棋盘的列数private static int Y; // 棋盘的行数private static boolean visited[]; // 用来标记棋盘上各个位置是否被访问过private static boolean finished; // 用来标记是否期盼所有位置均被访问(意味着已成功)public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("请输入棋盘的行数和列数:");do{Y = sc.nextInt();X = sc.nextInt();}while(X <= 0 || Y <= 0);System.out.print("请输入起始位置(先行数后列数):");int row, column;do{row = sc.nextInt();column = sc.nextInt();}while(row <= 0 || row > Y || column <= 0 || column > X);int[][] chessBoard = new int[Y][X];visited = new boolean[X * Y];long start = System.currentTimeMillis();traversalChessBoard(chessBoard, row - 1, column - 1, 1);long end = System.currentTimeMillis();System.out.println("共耗时:" + (end - start) + "ms");for(int[] rows : chessBoard){for(int columns : rows){System.out.print(columns + "\t");}System.out.println();} sc.close();}public static void traversalChessBoard(int[][] chessBoard, int row, int column, int step) {chessBoard[row][column] = step;visited[row * X + column] = true; // 此位置已访问ArrayList<Point> ps = next(new Point(column, row)); // 由当前位置得到下一次所有位置的集合while (!ps.isEmpty()) {Point p = ps.remove(0);if (!visited[p.y * X + p.x]) { // 这个位置没有访问,那么就从这个位置开始进行下一次访问traversalChessBoard(chessBoard, p.y, p.x, step + 1);}}if(step < X * Y && !finished){ // (step < X * Y)这个条件成立,有两种情况:第一种,棋盘到目前为止仍没有走完;第二种,棋盘已经走完过,此时在回溯的过程中chessBoard[row][column] = 0; // 如果整个棋盘最终全部为零,则表示无解visited[row * X + column] = false;}else{finished = true;}}// 在当前位置p处,下一次的位置(最多有8个位置)public static ArrayList<Point> next(Point p) {ArrayList<Point> ps = new ArrayList<Point>();Point p1 = new Point(p);if ((p1.x = p.x - 2) >= 0 && (p1.y = p.y - 1) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x - 1) >= 0 && (p1.y = p.y - 2) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 1) < X && (p1.y = p.y - 2) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 2) < X && (p1.y = p.y - 1) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 2) < X && (p1.y = p.y + 1) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x + 1) < X && (p1.y = p.y + 2) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x - 1) >= 0 && (p1.y = p.y + 2) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x - 2) >= 0 && (p1.y = p.y + 1) < Y) {ps.add(new Point(p1));}return ps;}}


贪心算法优化处理

    贪心算法(又称贪婪算法),是指在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。它只考虑局部的最优,从而让总体也最优。就我们这个马踏棋盘来说,我们每走一步都取最优的那个选择(而不是随便选择下一步),从而让整体的算法也最优。

   那么此问题的局部最优选择是什么呢?答案是,对于当前的一步,如果它的下一步有多种选择,我们应该选择它的下一步中的下一步选择最少的那个作为它的下一步。理由:因为只有在这样的选择方案下,当方案不符时,能够最快的回溯到当前的这一步上来(例如,它的下一步有4种选择,并且这4种选择的各自的下一步又分别有3,10,1,8种选择,而现在只有选择了下一步中的某一个才能成功,那么问题就是如何以最快的速度遍历出它的下一步中正确选择,也就是说当当前选择不对时,如何快速回溯到当前这一步,很显然,那只有它的下一步的下一步选择越少才会越快的结束这个选择),接着选择当前这一步的下一步选择次少作为它的下一步,依此类推。

package ccnu.offer.tree;import java.awt.Point;import java.util.ArrayList;import java.util.Comparator;import java.util.Scanner;public class Demo07 {private static int X; // 棋盘的列数private static int Y; // 棋盘的行数private static boolean visited[]; // 用来标记棋盘上各个位置是否被访问过private static boolean finished; // 用来标记是否期盼所有位置均被访问(意味着已成功)private static int count = 1;public static void main(String[] args) {Scanner sc = new Scanner(System.in);do{System.out.print("请输入棋盘的行数和列数:");Y = sc.nextInt();X = sc.nextInt();}while(X <= 0 || Y <= 0);int row, column;do{System.out.print("请输入起始位置(先行数后列数):");row = sc.nextInt();column = sc.nextInt();}while(row <= 0 || row > Y || column <= 0 || column > X);int[][] chessBoard = new int[Y][X];visited = new boolean[X * Y];long start = System.currentTimeMillis();traversalChessBoard(chessBoard, row - 1, column - 1, 1);long end = System.currentTimeMillis();System.out.println("共耗时:" + (end - start) + "ms");for(int[] rows : chessBoard){for(int columns : rows){System.out.print(columns + "\t");}System.out.println();}sc.close();}public static void traversalChessBoard(int[][] chessBoard, int row, int column, int step) {chessBoard[row][column] = step;visited[row * X + column] = true; // 此位置已访问ArrayList<Point> ps = next(new Point(column, row)); // 由当前位置得到下一次所有位置的集合sort(ps); // 按照当前(new Point(column, row))这步的下一步的下一步选择数目进行非递减排序while (!ps.isEmpty()) {Point p = ps.remove(0); // 每次选择仍然未选中的下一步的下一步选择数目最少的下一步作为当前这步的下一步if (!visited[p.y * X + p.x]) { // 这个位置没有访问,那么就从这个位置开始进行下一次访问traversalChessBoard(chessBoard, p.y, p.x, step + 1);}}if(step < X * Y && !finished){ // (step < X * Y)这个条件成立,有两种情况:第一种,棋盘到目前为止仍没有走完;第二种,棋盘已经走完过,此时在回溯的过程中chessBoard[row][column] = 0; // 如果整个棋盘最终全部为零,则表示无解visited[row * X + column] = false;}else{ // 此处代表已成功走出覆盖棋盘的完整路径,如果此时将结果输出(去掉finished变量),以便回溯可以得到所有的结果finished = true;}}// 在当前位置p处,下一次的位置(最多有8个位置)public static ArrayList<Point> next(Point p) {ArrayList<Point> ps = new ArrayList<Point>();Point p1 = new Point(p);if ((p1.x = p.x - 2) >= 0 && (p1.y = p.y - 1) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x - 1) >= 0 && (p1.y = p.y - 2) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 1) < X && (p1.y = p.y - 2) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 2) < X && (p1.y = p.y - 1) >= 0) {ps.add(new Point(p1));}if ((p1.x = p.x + 2) < X && (p1.y = p.y + 1) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x + 1) < X && (p1.y = p.y + 2) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x - 1) >= 0 && (p1.y = p.y + 2) < Y) {ps.add(new Point(p1));}if ((p1.x = p.x - 2) >= 0 && (p1.y = p.y + 1) < Y) {ps.add(new Point(p1));}return ps;}// 根据当前的这一步的所有下一步的选择数目进行非递减排序public static void sort(ArrayList<Point> ps){ps.sort(new Comparator<Point>() {@Overridepublic int compare(Point o1, Point o2) {int count1 = next(o1).size(); // 当前这一步o1的下一步的选择数目int count2 = next(o2).size();if (count1 < count2) {return -1;} else if (count1 == count2) {return 0;} else {return 1;}}});}}





1 1
原创粉丝点击