15,794,475 members

See more:

Write a program to solve a Sudoku puzzle by filling the empty cells where0represent empty cell. Rules: All the number in sudoku must appear exactly once in left-top to right-bottom diagonal. All the number in sudoku must appear exactly once in right-top to left-bottom diagonal. All the number in sudoku must appear exactly once in a square(3*3). However number can repeat in row or column.

Constraints:

N = 9; where N represent rows and column of grid.

Basically I am stuck on the implementation of checking distinct number along diagonal. So my question is how I can make sure that no elements gets repeated in those diagonal and sub-grid.

N = 9 def printing(arr): for i in range(N): for j in range(N): print(arr[i][j], end=" ") print() def isSafe(grid, row, col, num): for x in range(9): if grid[x][x] == num: return False cl = 0 for y in range(8, -1, -1): if grid[cl][y] == num: cl += 1 return False startRow = row - row % 3 startCol = col - col % 3 for i in range(3): for j in range(3): if grid[i + startRow][j + startCol] == num: return False return True def solveSudoku(grid, row, col): if (row == N - 1 and col == N): return True if col == N: row += 1 col = 0 if grid[row][col] > 0: return solveSudoku(grid, row, col + 1) for num in range(1, N + 1, 1): if isSafe(grid, row, col, num): grid[row][col] = num print(grid) if solveSudoku(grid, row, col + 1): return True grid[row][col] = 0 return False

Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

"how to solve"is far too general for "Quick Answers".if one line contains each elements in the array_LCD , the line is ok.

if you prefer positive reading ( without -- ) to fetch your lines, columns or diagonals, remenber your start point could always be 0, by reading from start or end of a line.

there are a lot of way to achieve by Arrays, and a lot of methods for array ( or fetch them ).

before reading any line, maybe a temporary array can help.

to_check_array will be a copy of one of the way to check a line.

isolate one line in any direction before testing its content.

it's few ideas to help you

horizontal : you read the line

diagonal : you read the first slot, then up ( or down ) about 1 column and 1 slot , then again and again

vertical : x++

horizontal : y++

diagonal : x++;y++; [ because of 2 coordinates to change ]

if it's too hard to understand ,

hardcode all the 'read' to do in several arrays.

it will be arrays of coordinates [slot 1[ x ; y ],slot 2 [x2 ; y2] and so and so.

it give you 20 arrays to write then read to check your grid.

that you fill the grid step by step,

and in your code : separate the display / the input / the grid check

it could be 3 ways to do.

a grid is a coordinates system x;y,

for sudoku : a matrix ( an array one dimension too or else ) about 81 slots.

21 lines to check , 9 squares

don't avoid any hypothesis, try all methods : enjoy !

and don't forget a method to fill by random any slots