棋盘覆盖代码实现
package site.wanjiahao.chesscover;
public class ChessCover {
private static int tile = 1;
private static final int[][] chess = new int[8][8];
public static void main(String[] args) {
optionChessBorder(0 , 0 , 1, 4, 8);
for (int[] ints : chess) {
for (int anInt : ints) {
System.out.printf("%d\t", anInt);
}
System.out.println();
}
}
public static void optionChessBorder(int x, int y, int sx, int sy, int size) {
if (size != 1) {
int t = tile++;
size = size / 2;
if (sx < x + size && sy < y + size) {
optionChessBorder(x, y, sx, sy, size);
} else {
chess[x + size - 1][y + size - 1] = t;
optionChessBorder(x, y, x + size - 1, y + size - 1, size);
}
if (sx >= x + size && sy < y + size) {
optionChessBorder(x + size, y, sx, sy, size);
} else {
chess[x + size][y + size - 1] = t;
optionChessBorder(x + size, y, x + size, y + size - 1, size);
}
if (sx < x + size && sy >= y + size) {
optionChessBorder(x, y + size, sx, sy, size);
} else {
chess[x + size - 1][y + size] = t;
optionChessBorder(x, y + size, x + size - 1, y + size, size);
}
if (sx >= x + size && sy >= y + size) {
optionChessBorder(x + size, y + size, sx, sy, size);
} else {
chess[x + size][y + size] = t;
optionChessBorder(x + size, y + size, x + size, y + size, size);
}
}
}
}