Sudoku Solution Validator 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 ...

Sudoku Solution Validator solution

 

Sudoku Solution Validator


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

Solution


import java.util.*;
public class SudokuValidator {
    public static boolean check(int[][] s) {
        //do your magic
      
      for(int i =0;i<9;i++) {
        for(int j=0;j<8;j++) {
          for(int k=j+1;k<9;k++) {
            if(s[i][k]==0 || s[i][j]==s[i][k]) {
              return false;
            }
          }
        }
      }
            
      for(int i =0;i<9;i++) {
        for(int j=0;j<8;j++) {
          for(int k=j+1;k<9;k++) {
            if(s[j][i]==s[k][i]) {
              return false;
            }
          }
        }
      }
          
      ArrayList<Integer> l = new ArrayList<Integer>();
      int i=0,j=0;
      for(i=0;i<3;i++) 
        for(j=0;j<3;j++) 
          l.add(s[i][j]);
      
      int n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();
      
      for(i=0;i<3;i++) 
        for(j=3;j<6;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();

      for(i=0;i<3;i++) 
        for(j=6;j<9;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();
      
      for(i=3;i<6;i++) 
        for(j=0;j<3;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();
      
      for(i=3;i<6;i++) 
        for(j=3;j<6;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;
      
      l.clear();

      for(i=3;i<6;i++) 
        for(j=6;j<9;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();

      
      for(i=6;i<9;i++) 
        for(j=0;j<3;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();
      
      for(i=6;i<9;i++) 
        for(j=3;j<6;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      l.clear();

      for(i=6;i<9;i++) 
        for(j=6;j<9;j++) 
          l.add(s[i][j]);
      
      n=l.size();
      for(i=0;i<n-1;i++) 
        for(j=i+1;j<n;j++) 
          if(l.get(i)==l.get(j)) 
            return false;

      
      return true;
      
    }
}

0 comments:

Do not spam here.