棋盘覆盖代码实现

棋盘覆盖代码实现

package site.wanjiahao.chesscover;
public class ChessCover {

    // L形骨牌数量
    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) {
            /*
            * L形状旗子
            * 注意:的是需要定义一个外部变量,这样在方法中递增才会改变数值
            * 并且要一个变量引用t,这样各个棋盘对应规定的数值才会一样,不能直接使用tile变量
            * Example: 对于第一个左上棋盘,如果是else情况,棋盘则会直接标记
            * chess[x + size - 1][y + size - 1] = t;
            * 执行递归方法,t继续增加,如果直接引用tile,至导致tile增加一定的数值后,才会执行右上棋盘
            * 这样就不能保证L形骨牌的唯一性
            */
            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);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值