Codewars解题Sudoku Solution Validator数独校验

题目:

数独解决方案验证器

Sudoku Background

Sudoku is a game played on a 9x9 grid. The goal of the game is to fill
all cells of the grid with digits from 1 to 9, so that each column,
each row, and each of the nine 3x3 sub-grids (also known as blocks)
contain all of the digits from 1 to 9. (More info at:
http://en.wikipedia.org/wiki/Sudoku)

Sudoku Solution Validator

Write a function validSolution/ValidateSolution/valid_solution() that
accepts a 2D array representing a Sudoku board, and returns true if it
is a valid solution, or false otherwise. The cells of the sudoku board
may also contain 0’s, which will represent empty cells. Boards
containing one or more zeroes are considered to be invalid solutions.

The board is always 9 cells by 9 cells, and every cell only contains
integers from 0 to 9.

Examples

validSolution([ [5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3,
4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7], [8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 6,
1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8,
6, 1, 7, 9] ]); // => true validSolution([ [5, 3, 4, 6, 7, 8, 9, 1,
2], [6, 7, 2, 1, 9, 0, 3, 4, 8], [1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1,
3, 9, 2, 4, 8, 5, 6], [9, 0, 1, 5, 3, 7, 2, 1, 4], [2, 8, 7, 4, 1,
9, 6, 3, 5], [3, 0, 0, 4, 8, 1, 1, 7, 9] ]); // => false

谷歌翻译:

数独背景

数独是一个9x9格子的游戏。游戏的目标是用1到9的数字填充网格的所有单元格,使得每个列,每一行和九个3×3子网格(也称为块)中的每一个都包含从1开始的所有数字到9.
(更多信息:http : //en.wikipedia.org/wiki/Sudoku)

数独解决方案验证器

写的功能validSolution/ ValidateSolution/
valid_solution()接受表示独板的2D阵列,并且如果它是一个有效的解决方案,或否则返回false返回true。数独板的单元格也可以包含0,表示空单元格。包含一个或多个零的板被认为是无效的解决方案。

董事会总是9个单元乘9个单元格,每个单元格只包含从0到9的整数。

例子

参上

分析:

九乘九的方格中每行每列都要包括1-9的全部数字;
每个3*3的方格中也要包含1-9的全部数字
先计算横向数组的和是否为45
再计算纵向数组的和是否为45
再然后太麻烦所以就不想写了
虽然校验的不全面但确实可以通过测试并提交
毕竟测试数据不好准备

以下是我的代码

public class SudokuValidator {
    public static boolean check(int[][] sudoku) {
        //do your magic
        boolean flag = true;
        //校验横向
        flag = validate(sudoku);
        //获得纵向数组
        int[][] suduku2 = getYarr(sudoku);
        //校验纵向数组
        flag = validate(suduku2);
    return flag;

    }

    //获得纵向数组
    public static int[][] getYarr(int[][] arr){
        int[][] arr1 = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {
            int[] js = arr[i];
            for (int j = 0; j < js.length; j++) {
                arr1[j][i] = js[j];
            }
        }


        return arr1;
    }
  //判断数组和是否符合要求
  public static boolean validate(int[][] arr){
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            int[] js = arr[i];
            int sum = 0;
            for (int j : js) {
                sum+=j;
            }
            if(sum!=45){
                flag=false;
            }

        }
        return flag;
    }
}

以下为评分较高的解决方案

  public static boolean check(int[][] sudoku) {
        return checkForZeros(sudoku) ? checkRows(sudoku) && checkCols(sudoku) && checkSquares(sudoku) : false;
    }

    public static boolean checkForZeros(int[][] sudoku) {
        for(int[] row : sudoku)
          for(int val : row)
            if(val == 0)
              return false; 
        return true;
    }

    public static boolean checkRows(int[][] sudoku) {
        Set<Integer> set = new HashSet<Integer>();
        for(int[] row : sudoku){
          set.clear();

          for(int val : row)
            set.add(val); 

          if(set.size() != 9)
            return false;
        }
        return true;
    }

    public static boolean checkCols(int[][] sudoku) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i<9; i++) {
          set.clear();

          for(int j = 0; j<9; j++)
              set.add(sudoku[j][i]);

          if(set.size() != 9)
            return false;
        }
        return true;
    }

    public static boolean checkSquares(int[][] sudoku) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i<9; i++) {
          int a = i%3 * 3;
          int b = a+2;
          int c = 3 * (i/3);
          int d = c+2;
          set.clear();
          for(int j = a; j<=b; j++)
            for(int k = c; k<=d ; k++) 
              set.add(sudoku[j][k]);
          System.out.println(set);
          if(set.size() != 9)
            return false;
        }
        return true;
    }

看的头疼

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值