Vérification de la solution Java Sudoku


J'essaie actuellement de créer un programme Java qui lit 81 entiers (1-9) dans une matrice 9 X 9, puis teste pour voir si cette matrice est une solution à un puzzle de Sudoku. Les paramètres d'une solution de Sudoku sont les suivants: chaque nombre (1-9) doit être représenté dans chaque ligne, colonne et carré 3x3 sans aucune répétition dans ces zones. J'ai écrit une méthode pour vérifier que les paramètres sont respectés pour toutes les lignes et colonnes, cependant, j'ai du mal à trouver un algorithme pour vérifiez les carrés. Voici ce que j'ai jusqu'à présent:

import java.util.*;
public class SudokuCheck
{
  public static boolean sudokuCheck(int[][] s)
  {
    for(int row=0;row<9;row++)
      for(int col=0;col<8;col++)
      if(s[row][col]==s[row][col+1]){
      return false;}
     //Verifies rows

    for(int col2=0;col2<9;col2++)
      for(int row2=0;row2<8;row2++)
      if (s[row2][col2]==s[row2+1][col2])
      return false;
    //verifies columns

    return true;    
  }


  public static void main (String[] args)
  {
    Scanner input = new Scanner(System.in);

    int[][] solution = new int [9][9];
    System.out.println("Enter the values of a 9 X 9 Sudoku solution");

    for(int i=0;i<9;i++)
      for(int j=0;j<9;j++)
      solution[i][j]=input.nextInt();
    //read values into matrix


    if(sudokuCheck(solution)==true)
      System.out.println("The entered 9 X 9 grid is a solution to a Sudoku puzzle.");
else
  System.out.println("The entered 9 X 9 grid is not a solution to a Sudoku puzzle.");
  }
}
Author: A.334, 2015-12-04

3 answers

Cela peut probablement être optimisé mais en suivant votre approche

// row checker
for(int row = 0; row < 9; row++)
   for(int col = 0; col < 8; col++)
      for(int col2 = col + 1; col2 < 9; col2++)
         if(s[row][col]==s[row][col2])
            return false;

// column checker
for(int col = 0; col < 9; col++)
   for(int row = 0; row < 8; row++)
      for(int row2 = row + 1; row2 < 9; row2++)
         if(s[row][col]==s[row2][col])
            return false;

// grid checker
for(int row = 0; row < 9; row += 3)
   for(int col = 0; col < 9; col += 3)
      // row, col is start of the 3 by 3 grid
      for(int pos = 0; pos < 8; pos++)
         for(int pos2 = pos + 1; pos2 < 9; pos2++)
            if(s[row + pos%3][col + pos/3]==s[row + pos2%3][col + pos2/3])
               return false;
 1
Author: cmd, 2017-11-24 04:20:29

J'espère que cela vous aide

public static void main(String[] args) throws Exception {
    int[][] f = {{4,2,9,8,1,3,5,6,7}, {5,1,6,4,7,2,9,3,8}, {7,8,3,6,5,9,2,4,1},
                 {6,7,2,1,3,4,8,5,9}, {3,9,5,2,8,6,1,7,4}, {8,4,1,7,9,5,6,2,3}, 
                 {1,5,8,3,6,7,4,9,2}, {9,3,4,5,2,8,7,1,6}, {2,6,7,9,4,1,3,8,5}};
    System.out.println(Arrays.toString(f));
    System.out.println(checkBlock(f, 2, 2));
}

public static boolean checkBlock(int[][] f, int z, int s) throws Exception {
    boolean[] mark = new boolean[9];
    boolean res = true;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            int v = f[z * 3 + i][s * 3 + j];
            if (v == 0) {
                res = false;
            }
            if (mark[v - 1]) {
                throw new Exception();
            }
            mark[v - 1] = true;
        }
    }
    return res;
}
 0
Author: wassim fourkaoui, 2018-02-10 13:45:43
public class SudokuVerify {
    public static void main(String[] args) {
        int[][] arr = { { 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 } };
        boolean flag = true;
        int ExpectedVal = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9);
        int ActualVal = 0;
//---># Checking for row Validation**
        for (int row = 0; row < 9; row++) {
            ActualVal = 0;
            for (int col = 0; col < 9; col++) {
                ActualVal ^= arr[row][col];
            }
            if (ActualVal != ExpectedVal) {
                flag = false;
                break;
            }
        }
//---># **Checking for col Validation**
        if (flag) {
            for (int row = 0; row < 9; row++) {
                ActualVal = 0;
                for (int col = 0; col < 9; col++) {
                    ActualVal ^= arr[col][row];
                }
                if (ActualVal != ExpectedVal) {
                    flag = false;
                    break;
                }
            }
        }
//---># **Checking for inside box Validation**
        if (flag) {
            for (int i = 0; i < 9; i += 3) {

                if (flag) {
                    for (int j = 0; j < 9; j += 3) {
                        ActualVal = 0;
                        for (int ii = i; ii < (i + 3); ii++) {
                            for (int jj = j; jj < (j + 3); jj++) {
                                ActualVal ^= arr[ii][jj];
                            }
                            System.out.println();
                        }
                        if (ActualVal != ExpectedVal) {
                            flag = false;
                            break;
                        }
                    }
                } else
                    break;

            }
        }
        if (flag)
            System.out.println("Passes");
        else {
            System.out.println("Faied");
        }
    }
}
 -1
Author: gpsOnCoding, 2018-01-17 18:54:18