📜  检查给定的数独解决方案是否有效

📅  最后修改于: 2021-10-26 05:29:40             🧑  作者: Mango

给定一个大小为9 × 9的 2D 数组board[][] ,它表示数独谜题的解决方案,任务是检查已解决数独谜题的给定表示是否有效。

例子:

处理方法:通过检查以下条件可以解决问题:

  • 检查board[][]数组的每一行是否仅存储范围 [ 1 , 9 ] 中的唯一值。
  • 检查board[][]数组的每一列是否存储了范围 [ 1 , 9 ] 中的唯一值。
  • 检查board[][]数组的所有可能的3 × 3 子矩阵是否存储了范围 [ 1 , 9 ] 中的唯一值。

请按照以下步骤解决问题:

  • 遍历给定的矩阵board[][]
  • 检查以上条件是否满足。
  • 如果不满足上述任何条件,则打印“无效”。
  • 否则,打印“有效”。

下面是上述方法的实现:

C++
board[][] = {{7, 9, 2, 1, 5, 4, 3, 8, 6}, 
             {6, 4, 3, 8, 2, 7, 1, 5, 9},
             {8, 5, 1, 3, 9, 6, 7, 2, 4},
             {2, 6, 5, 9, 7, 3, 8, 4, 1},
             {4, 8, 9, 5, 6, 1, 2, 7, 3},
             {3, 1, 7, 4, 8, 2, 9, 6, 5},
             {1, 3, 6, 7, 4, 8, 5, 9, 2},
             {9, 7, 4, 2, 1, 5, 6, 3, 8},
             {5, 2, 8, 6, 3, 9, 4, 1, 7}}


Java
board[][] = {{5, 5, 5, 5, 5, 5, 5, 5, 5}, 
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5},
             {5, 5, 5, 5, 5, 5, 5, 5, 5}}


Python3
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define N 9
 
// Function to check if all elements
// of the board[][] array store
// value in the range[1, 9]
bool isinRange(int board[][N])
{
 
    // Traverse board[][] array
    for (int i = 0; i < N;
         i++) {
        for (int j = 0; j < N;
             j++) {
 
            // Check if board[i][j]
            // lies in the range
            if (board[i][j] <= 0 || board[i][j] > 9) {
                return false;
            }
        }
    }
    return true;
}
 
// Function to check if the solution
// of sudoku puzzle is valid or not
bool isValidSudoku(int board[][N])
{
    // Check if all elements of board[][]
    // stores value in the range[1, 9]
    if (isinRange(board)
        == false) {
        return false;
    }
 
    // Stores unique value
    // from 1 to N
    bool unique[N + 1];
 
    // Traverse each row of
    // the given array
    for (int i = 0; i < N; i++) {
 
        // Initialize unique[]
        // array to false
        memset(unique, false,
               sizeof(unique));
 
        // Traverse each column
        // of current row
        for (int j = 0; j < N;
             j++) {
 
            // Stores the value
            // of board[i][j]
            int Z = board[i][j];
 
            // Check if current row
            // stores duplicate value
            if (unique[Z]) {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each column of
    // the given array
    for (int i = 0; i < N; i++) {
 
        // Initialize unique[]
        // array to false
        memset(unique, false,
               sizeof(unique));
 
        // Traverse each row
        // of current column
        for (int j = 0; j < N;
             j++) {
 
            // Stores the value
            // of board[j][i]
            int Z = board[j][i];
 
            // Check if current column
            // stores duplicate value
            if (unique[Z]) {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each block of
    // size 3 * 3 in board[][] array
    for (int i = 0; i < N - 2;
         i += 3) {
 
        // j stores first column of
        // each 3 * 3 block
        for (int j = 0; j < N - 2;
             j += 3) {
 
            // Initialize unique[]
            // array to false
            memset(unique, false,
                   sizeof(unique));
 
            // Traverse current block
            for (int k = 0; k < 3;
                 k++) {
 
                for (int l = 0; l < 3;
                     l++) {
 
                    // Stores row number
                    // of current block
                    int X = i + k;
 
                    // Stores column number
                    // of current block
                    int Y = j + l;
 
                    // Stores the value
                    // of board[X][Y]
                    int Z = board[X][Y];
 
                    // Check if current block
                    // stores duplicate value
                    if (unique[Z]) {
                        return false;
                    }
                    unique[Z] = true;
                }
            }
        }
    }
 
    // If all conditions satisfied
    return true;
}
 
// Driver Code
int main()
{
    int board[N][N]
        = { { 7, 9, 2, 1, 5, 4, 3, 8, 6 },
            { 6, 4, 3, 8, 2, 7, 1, 5, 9 },
            { 8, 5, 1, 3, 9, 6, 7, 2, 4 },
            { 2, 6, 5, 9, 7, 3, 8, 4, 1 },
            { 4, 8, 9, 5, 6, 1, 2, 7, 3 },
            { 3, 1, 7, 4, 8, 2, 9, 6, 5 },
            { 1, 3, 6, 7, 4, 8, 5, 9, 2 },
            { 9, 7, 4, 2, 1, 5, 6, 3, 8 },
            { 5, 2, 8, 6, 3, 9, 4, 1, 7 } };
 
    if (isValidSudoku(board)) {
        cout << "Valid";
    }
    else {
        cout << "Not Valid";
    }
}


C#
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
static int N = 9;
 
// Function to check if all elements
// of the board[][] array store
// value in the range[1, 9]
static boolean isinRange(int[][] board)
{
     
    // Traverse board[][] array
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // Check if board[i][j]
            // lies in the range
            if (board[i][j] <= 0 ||
                board[i][j] > 9)
            {
                return false;
            }
        }
    }
    return true;
}
 
// Function to check if the solution
// of sudoku puzzle is valid or not
static boolean isValidSudoku(int board[][])
{
     
    // Check if all elements of board[][]
    // stores value in the range[1, 9]
    if (isinRange(board) == false)
    {
        return false;
    }
 
    // Stores unique value
    // from 1 to N
    boolean[] unique = new boolean[N + 1];
 
    // Traverse each row of
    // the given array
    for(int i = 0; i < N; i++)
    {
         
        // Initialize unique[]
        // array to false
        Arrays.fill(unique, false);
 
        // Traverse each column
        // of current row
        for(int j = 0; j < N; j++)
        {
             
            // Stores the value
            // of board[i][j]
            int Z = board[i][j];
 
            // Check if current row
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each column of
    // the given array
    for(int i = 0; i < N; i++)
    {
         
        // Initialize unique[]
        // array to false
        Arrays.fill(unique, false);
 
        // Traverse each row
        // of current column
        for(int j = 0; j < N; j++)
        {
             
            // Stores the value
            // of board[j][i]
            int Z = board[j][i];
 
            // Check if current column
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each block of
    // size 3 * 3 in board[][] array
    for(int i = 0; i < N - 2; i += 3)
    {
         
        // j stores first column of
        // each 3 * 3 block
        for(int j = 0; j < N - 2; j += 3)
        {
             
            // Initialize unique[]
            // array to false
            Arrays.fill(unique, false);
 
            // Traverse current block
            for(int k = 0; k < 3; k++)
            {
                for(int l = 0; l < 3; l++)
                {
                     
                    // Stores row number
                    // of current block
                    int X = i + k;
 
                    // Stores column number
                    // of current block
                    int Y = j + l;
 
                    // Stores the value
                    // of board[X][Y]
                    int Z = board[X][Y];
 
                    // Check if current block
                    // stores duplicate value
                    if (unique[Z])
                    {
                        return false;
                    }
                    unique[Z] = true;
                }
            }
        }
    }
 
    // If all conditions satisfied
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int[][] board = { { 7, 9, 2, 1, 5, 4, 3, 8, 6 },
                      { 6, 4, 3, 8, 2, 7, 1, 5, 9 },
                      { 8, 5, 1, 3, 9, 6, 7, 2, 4 },
                      { 2, 6, 5, 9, 7, 3, 8, 4, 1 },
                      { 4, 8, 9, 5, 6, 1, 2, 7, 3 },
                      { 3, 1, 7, 4, 8, 2, 9, 6, 5 },
                      { 1, 3, 6, 7, 4, 8, 5, 9, 2 },
                      { 9, 7, 4, 2, 1, 5, 6, 3, 8 },
                      { 5, 2, 8, 6, 3, 9, 4, 1, 7 } };
 
    if (isValidSudoku(board))
    {
        System.out.println("Valid");
    }
    else
    {
        System.out.println("Not Valid");
    }
}
}
 
// This code is contributed by akhilsaini


Javascript
# Python3 program to implement
# the above approach
 
# Function to check if all elements
# of the board[][] array store
# value in the range[1, 9]
def isinRange(board):
   
  N = 9
   
  # Traverse board[][] array
  for i in range(0, N):
    for j in range(0, N):
       
      # Check if board[i][j]
      # lies in the range
      if ((board[i][j] <= 0) or
          (board[i][j] > 9)):
        return False
       
  return True
 
# Function to check if the solution
# of sudoku puzzle is valid or not
def isValidSudoku(board):
   
  N = 9
   
  # Check if all elements of board[][]
  # stores value in the range[1, 9]
  if (isinRange(board) == False):
    return False
 
  # Stores unique value
  # from 1 to N
  unique = [False] * (N + 1)
 
  # Traverse each row of
  # the given array
  for i in range(0, N):
     
    # Initialize unique[]
    # array to false
    for m in range(0, N + 1):
      unique[m] = False
 
    # Traverse each column
    # of current row
    for j in range(0, N):
       
      # Stores the value
      # of board[i][j]
      Z = board[i][j]
 
      # Check if current row
      # stores duplicate value
      if (unique[Z] == True):
        return False
       
      unique[Z] = True
 
  # Traverse each column of
  # the given array
  for i in range(0, N):
     
    # Initialize unique[]
    # array to false
    for m in range(0, N + 1):
      unique[m] = False
 
    # Traverse each row
    # of current column
    for j in range(0, N):
       
      # Stores the value
      # of board[j][i]
      Z = board[j][i]
 
      # Check if current column
      # stores duplicate value
      if (unique[Z] == True):
        return False
       
      unique[Z] = True
 
  # Traverse each block of
  # size 3 * 3 in board[][] array
  for i in range(0, N - 2, 3):
     
    # j stores first column of
    # each 3 * 3 block
    for j in range(0, N - 2, 3):
       
      # Initialize unique[]
      # array to false
      for m in range(0, N + 1):
        unique[m] = False
 
      # Traverse current block
      for k in range(0, 3):
        for l in range(0, 3):
           
          # Stores row number
          # of current block
          X = i + k
 
          # Stores column number
          # of current block
          Y = j + l
 
          # Stores the value
          # of board[X][Y]
          Z = board[X][Y]
 
          # Check if current block
          # stores duplicate value
          if (unique[Z] == True):
            return False
           
          unique[Z] = True
           
  # If all conditions satisfied
  return True
 
# Driver Code
if __name__ == '__main__':
   
  board = [ [ 7, 9, 2, 1, 5, 4, 3, 8, 6 ],
            [ 6, 4, 3, 8, 2, 7, 1, 5, 9 ],
            [ 8, 5, 1, 3, 9, 6, 7, 2, 4 ],
            [ 2, 6, 5, 9, 7, 3, 8, 4, 1 ],
            [ 4, 8, 9, 5, 6, 1, 2, 7, 3 ],
            [ 3, 1, 7, 4, 8, 2, 9, 6, 5 ],
            [ 1, 3, 6, 7, 4, 8, 5, 9, 2 ],
            [ 9, 7, 4, 2, 1, 5, 6, 3, 8 ],
            [ 5, 2, 8, 6, 3, 9, 4, 1, 7 ] ]
 
  if (isValidSudoku(board)):
    print("Valid")
  else:
    print("Not Valid")
     
# This code is contributed by akhilsaini


输出:
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int N = 9;
 
// Function to check if all elements
// of the board[][] array store
// value in the range[1, 9]
static bool isinRange(int[, ] board)
{
     
    // Traverse board[][] array
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // Check if board[i][j]
            // lies in the range
            if (board[i, j] <= 0 ||
                board[i, j] > 9)
            {
                return false;
            }
        }
    }
    return true;
}
 
// Function to check if the solution
// of sudoku puzzle is valid or not
static bool isValidSudoku(int[, ] board)
{
     
    // Check if all elements of board[][]
    // stores value in the range[1, 9]
    if (isinRange(board) == false)
    {
        return false;
    }
 
    // Stores unique value
    // from 1 to N
    bool[] unique = new bool[N + 1];
 
    // Traverse each row of
    // the given array
    for(int i = 0; i < N; i++)
    {
 
        // Initialize unique[]
        // array to false
        Array.Fill(unique, false);
 
        // Traverse each column
        // of current row
        for(int j = 0; j < N; j++)
        {
 
            // Stores the value
            // of board[i][j]
            int Z = board[i, j];
 
            // Check if current row
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each column of
    // the given array
    for(int i = 0; i < N; i++)
    {
 
        // Initialize unique[]
        // array to false
        Array.Fill(unique, false);
 
        // Traverse each row
        // of current column
        for(int j = 0; j < N; j++)
        {
 
            // Stores the value
            // of board[j][i]
            int Z = board[j, i];
 
            // Check if current column
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }
 
    // Traverse each block of
    // size 3 * 3 in board[][] array
    for(int i = 0; i < N - 2; i += 3)
    {
         
        // j stores first column of
        // each 3 * 3 block
        for(int j = 0; j < N - 2; j += 3)
        {
 
            // Initialize unique[]
            // array to false
            Array.Fill(unique, false);
 
            // Traverse current block
            for(int k = 0; k < 3; k++)
            {
                for(int l = 0; l < 3; l++)
                {
                     
                    // Stores row number
                    // of current block
                    int X = i + k;
 
                    // Stores column number
                    // of current block
                    int Y = j + l;
 
                    // Stores the value
                    // of board[X][Y]
                    int Z = board[X, Y];
 
                    // Check if current block
                    // stores duplicate value
                    if (unique[Z])
                    {
                        return false;
                    }
                    unique[Z] = true;
                }
            }
        }
    }
 
    // If all conditions satisfied
    return true;
}
 
// Driver Code
public static void Main()
{
    int[,] board = { { 7, 9, 2, 1, 5, 4, 3, 8, 6 },
                      { 6, 4, 3, 8, 2, 7, 1, 5, 9 },
                      { 8, 5, 1, 3, 9, 6, 7, 2, 4 },
                      { 2, 6, 5, 9, 7, 3, 8, 4, 1 },
                      { 4, 8, 9, 5, 6, 1, 2, 7, 3 },
                      { 3, 1, 7, 4, 8, 2, 9, 6, 5 },
                      { 1, 3, 6, 7, 4, 8, 5, 9, 2 },
                      { 9, 7, 4, 2, 1, 5, 6, 3, 8 },
                      { 5, 2, 8, 6, 3, 9, 4, 1, 7 } };
 
    if (isValidSudoku(board))
    {
        Console.WriteLine("Valid");
    }
    else
    {
        Console.WriteLine("Not Valid");
    }
}
}
 
// This code is contributed by akhilsaini

时间复杂度: O(N 2 )
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程