数独9*9常规校验算法 纳秒级别 java 单元测试 附代码 实测40000纳级别

效果(先看单元测试效果)

在这里插入图片描述

代码

import org.junit.Test;


public class NineSquares {

    @Test
    public void nineSquaresCheckUnitTest() {
        int[][] data = {
                {1, 2, 3, 4, 5, 6, 7, 8, 9},
                {4, 5, 6, 7, 8, 9, 1, 2, 3},
                {7, 8, 9, 1, 2, 3, 4, 5, 6},
                {2, 3, 1, 5, 6, 4, 8, 9, 7},
                {5, 6, 4, 8, 9, 7, 2, 3, 1},
                {8, 9, 7, 2, 3, 1, 5, 6, 4},
                {3, 1, 2, 6, 4, 5, 9, 7, 8},
                {6, 4, 5, 9, 7, 8, 3, 1, 2},
                {9, 7, 8, 3, 1, 2, 6, 4, 5},
        };
        long s = System.currentTimeMillis();
        System.out.println("校验结果:" + nineSquaresCheck(data));
        System.out.println("执行时间:" + (System.currentTimeMillis() - s) + "ms");
    }

    /**
     * 每个块的中心点记录
     */
    private static final int[][] boxIndex = new int[][]{{1, 1}, {1, 4}, {1, 7}, {4, 1}, {4, 4}, {4, 7}, {7, 1}, {7, 4}, {7, 7}};

    /**
     * 校验九宫格数据是否全对
     *
     * @param data 二维数组9*9的九宫格数据
     * @return 是否符合常规九宫格数据
     */
    public boolean nineSquaresCheck(int[][] data) {
        int ylen = data.length;
        int xlen = data[0].length;
        if (ylen != 9 || xlen != 9) {
            return false;
        }
        // x检查
        for (int i = 0; i < 9; i++) {
            if (!checkUnitA(data[i])) {
                return false;
            }
        }
        // y检查
        for (int i = 0; i < 9; i++) {
            if (!checkUnitB(data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5], data[i][6], data[i][7], data[i][8])) {
                return false;
            }
        }
        // 块检查
        return checkBox(data);
    }

    private boolean checkBox(int[][] data) {
        for (int i = 0; i < boxIndex.length; i++) {
            int[] boxIndexItem = NineSquares.boxIndex[i];
            int y = boxIndexItem[0];
            int x = boxIndexItem[1];
            if (!checkUnitB(data[y - 1][x - 1], data[y - 1][x], data[y - 1][x + 1], data[y][x - 1], data[y][x], data[y][x + 1], data[y + 1][x - 1], data[y + 1][x], data[y + 1][x + 1])) {
                return false;
            }
        }
        return true;
    }


    private boolean checkUnitA(int[] unitData) {
        int[] checks = new int[9];
        for (int item : unitData) {
            if (item > 9 || item < 0) {
                return false;
            }
            int check = checks[item - 1];
            if (check == 1) {
                return false;
            } else {
                checks[item - 1] = 1;
            }
        }
        return true;
    }

    private boolean checkUnitB(int... unitData) {
        int[] checks = new int[9];
        for (int item : unitData) {
            if (item > 9 || item < 0) {
                return false;
            }
            int check = checks[item - 1];
            if (check == 1) {
                return false;
            } else {
                checks[item - 1] = 1;
            }
        }
        return true;
    }
}

思路

思路很简单,就是常规的横轴竖轴以及每个3*3小块的校验,因为想了很多情况下,尽量减少复杂度上来说,最少27次才能全部校验完,而且我们也无法获知填写的哪一个步骤出错,所以理论没办法优化了。
校验其9个单元数据是否正确性的时候,用了数组来校验的,其他的就比较普通了。不合理的地方望指出,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若光672

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值