import java.util.Scanner; /** 棋盘覆盖 */ public class Arithmetic { /** 被覆盖后显示的数字,会根据覆盖的顺序有所递增 */ private int counter = 0; /** 当前棋盘是否已经存在 */ private boolean hasMap = false; /** 棋盘数组 */ private int[][] map = null; /** * 棋盘覆盖算法 * * @param northWestX * :棋盘左上角的X坐标 * @param northWestY * :棋盘左上角的Y坐标 * @param targetX * :特殊位置的X坐标 * @param targetY * :特殊位置的Y坐标 * @param size * :棋盘大小 */ public void chessBoard(int northWestX, int northWestY, int targetX, int targetY, int size) { if (!hasMap) { buildMap(size); hasMap = true; } /** * 如果棋盘大小为1就退出 */ if (size == 1) { return; } /** * 将棋盘大小除以2,将问题规模缩小 */ size /= 2; /** 用于标记当前覆盖位置 */ int sign = ++counter; /** * 1.如果目标在当前棋盘的左上角,则递归求解 */ if (targetX < northWestX + size && targetY < northWestY + size) { chessBoard(northWestX, northWestY, targetX, targetY, size); } else { /** * 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错 */ if (map[northWestY + size - 1][northWestX + size - 1] != 0) { System.out .println("position (" + (northWestX + size - 1) + ", " + (northWestY + size - 1) + ") has been filled!"); } /** * 重新指定目标代码为右下角方格 */ map[northWestY + size - 1][northWestX + size - 1] = sign; /** 递归求解当目标代码为右下角方格时的情况 */ chessBoard(northWestX, northWestY, northWestX + size - 1, northWestY + size - 1, size); } /** * 2.如果目标在当前棋盘的右上角,则递归求解 */ if (targetX >= northWestX + size && targetY < northWestY + size) { chessBoard(northWestX + size, northWestY, targetX, targetY, size); } else { /** * 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错 */ if (map[northWestY + size - 1][northWestX + size] != 0) { System.out.println("position (" + (northWestX + size) + ", " + (northWestY + size - 1) + ") has been filled!"); } /** * 重新指定目标代码为坐下角方格 */ map[northWestY + size - 1][northWestX + size] = sign; /** 递归求解当目标代码为左下角方格时的情况 */ chessBoard(northWestX + size, northWestY, northWestX + size, northWestY + size - 1, size); } /** * 3.如果目标在当前棋盘的右下方,则递归求解 */ if (targetX >= northWestX + size && targetY >= northWestY + size) { chessBoard(northWestX + size, northWestY + size, targetX, targetY, size); } else { /** * 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错 */ if (map[northWestY + size][northWestX + size] != 0) { System.out .println("position (" + (northWestX + size - 1) + ", " + (northWestY + size - 1) + ") has been filled!"); } /** 重新指定目标代码为左上角方格 */ map[northWestY + size][northWestX + size] = sign; /** 递归求解当目标代码为左上角方格时的情况 */ chessBoard(northWestX + size, northWestY + size, northWestX + size, northWestY + size, size); } /** * 4.如果目标在当前棋盘的左下角,则递归求解 */ if (targetX < northWestX + size && targetY >= northWestY + size) { chessBoard(northWestX, northWestY + size, targetX, targetY, size); } else { /** * 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错 */ if (map[northWestY + size][northWestX + size - 1] != 0) { System.out .println("position (" + (northWestX + size - 1) + ", " + (northWestY + size - 1) + ") has been filled!"); } /** 重新指定目标代码为右上角方格 */ map[northWestY + size][northWestX + size - 1] = sign; /** 递归求解当目标代码为右上角方格时的情况 */ chessBoard(northWestX, northWestY + size, northWestX + size - 1, northWestY + size, size); } // /** // * 为便于观察让其睡眠2秒 // */ // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // /** // * 打印棋盘 // */ // printMap(); // System.out.println(); } /** * 生成地图 * * @param size */ private void buildMap(int size) { map = new int[size][size]; /** 初始化数组 */ for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x) { map[y][x] = 0; } } } /** * 打印棋盘 */ public void printMap() { for (int y = 0; y < map.length; ++y) { for (int x = 0; x < map[0].length; ++x) { System.out.print(map[y][x] + "\t"); } System.out.println(); } } public static void main(String[] args) { System.out.println("请输入您想要的合法大小,例如:4"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); System.out.println("你输入的是:" + i + " * " + i + " 的矩阵 , 默认特殊位置为(1, 2),也可以对参数作出更改!"); /** * 确定参数是否合法 */ double flag = Math.log(i) / Math.log(2); if ((flag - (int) flag) != 0) { System.out.println("参数不合法!"); } else { Arithmetic arithmetic = new Arithmetic(); arithmetic.chessBoard(0, 0, 1, 2, i); arithmetic.printMap(); } } }
java实现棋盘覆盖算法,java 棋盘覆盖算法
最新推荐文章于 2022-10-21 14:59:50 发布