Computer Science, asked by FehlingSolution, 3 months ago

Write a Python program to solve a Sudoku puzzle. If possible use Raw Diagram to show the flow of your program. Remember, the program must be both speed and memory efficient. You are free to import any module. Also, explain the flow of control briefly. A sample Sudoku puzzle is attached. If convenient, attach the output with your answer. ​

Attachments:

Answers

Answered by atrs7391
1

def isValid(i, j, x, board):

   # check row

   for col in range(9):

       if board[i][col] == x:

           return False

   # check column

   for row in range(9):

       if board[row][j] == x:

           return False

   # check block

   startrow = i - i % 3

   startcol = j - j % 3

   p = startrow

   while p <= startrow + 2:

       l = startcol

       while l <= startcol + 2:

           if board[p][l] == x:

               return False

           l += 1

       p += 1

   return True

def solveSudokuHelper(i, j, board):

   if i == 8 and j == 8:

       if board[i][j] != 0:

           for row in board:

               for ele in row:

                   print(ele, end=" ")

               print()

       else:

           for x in range(1, 10):

               if isValid(i, j, x, board) is True:

                   board[i][j] = x

                   for row in board:

                       for ele in row:

                           print(ele, end=" ")

                       print()

                   board[i][j] = 0

       print()

       return

   if j > 8:

       solveSudokuHelper(i + 1, 0, board)

       return

   if board[i][j] == 0:

       for x in range(1, 10):

           if isValid(i, j, x, board) is True:

               board[i][j] = x

               solveSudokuHelper(i, j + 1, board)

               board[i][j] = 0

   else:

       solveSudokuHelper(i, j + 1, board)

   return

def solveSudoku(board):

   solveSudokuHelper(0, 0, board)

board = []

for i in range(9):

   print("Enter space separated elements of row {}".format(i+1))

   row = [int(x) for x in input().strip().split()]

   board.append(row)

print()

solveSudoku(board)

Answered by eeshamaryam
4

def isValid(i, j, x, board):

  # check row

  for col in range(9):

      if board[i][col] == x:

          return False

  # check column

  for row in range(9):

      if board[row][j] == x:

          return False

  # check block

  startrow = i - i % 3

  startcol = j - j % 3

  p = startrow

  while p <= startrow + 2:

      l = startcol

      while l <= startcol + 2:

          if board[p][l] == x:

              return False

          l += 1

      p += 1

  return True

def solveSudokuHelper(i, j, board):

  if i == 8 and j == 8:

      if board[i][j] != 0:

          for row in board:

              for ele in row:

                  print(ele, end=" ")

              print()

      else:

          for x in range(1, 10):

              if isValid(i, j, x, board) is True:

                  board[i][j] = x

                  for row in board:

                      for ele in row:

                          print(ele, end=" ")

                      print()

                  board[i][j] = 0

      print()

      return

  if j > 8:

      solveSudokuHelper(i + 1, 0, board)

      return

  if board[i][j] == 0:

      for x in range(1, 10):

          if isValid(i, j, x, board) is True:

              board[i][j] = x

              solveSudokuHelper(i, j + 1, board)

              board[i][j] = 0

  else:

      solveSudokuHelper(i, j + 1, board)

  return

def solveSudoku(board):

  solveSudokuHelper(0, 0, board)

board = []

for i in range(9):

  print("Enter space separated elements of row {}".format(i+1))

  row = [int(x) for x in input().strip().split()]

  board.append(row)

print()

solveSudoku(board)

Answer:

So you're a Harry Potter fan!

Explanation:

I'm one myself.

Have you read The Cursed Child?

And have you seen;

Fantastic Beasts: The Crimes Of Grindelwald??

❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤

I finished reading six books in just 1 week!

Order Of The Phoenix...

Yeah, it took me 4 weeks because I had to manage my studies along.

Are you ARMY?

Anyways, here, have a few B.T.S jokes to lighten up your mood!

Attachments:
Similar questions