-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbruteForce.py
62 lines (52 loc) · 1.94 KB
/
bruteForce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class BruteForce:
def __init__(self, grid):
self.grid = grid
self.counter = 0
def solve(self, grid):
"""
Attempt to solve the Sudoku puzzle using a brute force approach.
Parameters:
- grid (list of lists): The 9x9 Sudoku grid to be solved.
Returns:
- bool: True if a valid solution is found and the grid is solved; False otherwise.
"""
for i in range(9):
for j in range(9):
if grid[i][j] == 0:
for k in range(1, 10):
if self.check(grid, i, j, k):
grid[i][j] = k
self.counter += 1
if self.solve(grid):
return True
else:
grid[i][j] = 0
self.counter -= 1
return False
return True
def check(self, grid, row, column, num):
"""
Check if it's valid to place a number at a specific position in the Sudoku grid.
Parameters:
- grid (list of lists): The 9x9 Sudoku grid to be checked.
- row (int): The row index of the cell being checked.
- column (int): The column index of the cell being checked.
- num (int): The number to be checked for validity.
Returns:
- bool: True if placing the number at the specified position is valid;
False otherwise.
"""
if num in grid[row]:
return False
for i in range(9):
if grid[i][column] == num:
return False
x = (row - (row % 3))
y = (column - (column % 3))
for i in range(3):
for j in range(3):
if (i, j) == (row, column):
continue
if grid[x + j][y + i] == num:
return False
return True