-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
function solveSudoku(grid) { | ||
const cell = [0, 0]; | ||
|
||
if (!findEmptyLocation(grid, cell)) return grid; | ||
|
||
const [row, col] = cell; | ||
|
||
for (let num = 1; num < 10; num++) { | ||
if (checkSafeLocation(grid, row, col, num)) { | ||
grid[row][col] = num; | ||
if (solveSudoku(grid)) return grid; | ||
else grid[row][col] = 0; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function findEmptyLocation(grid, cell) { | ||
for (let row = 0; row < 9; row++) { | ||
for (let col = 0; col < 9; col++) { | ||
if (!grid[row][col]) { | ||
cell[0] = row; | ||
cell[1] = col; | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function checkSafeLocation(grid, row, col, num) { | ||
return ( | ||
!usedInRowOrColumn(grid, row, col, num) && | ||
!usedInBox(grid, row - row % 3, col - col % 3, num) | ||
); | ||
} | ||
|
||
function usedInRowOrColumn(grid, row, col, num) { | ||
for (let i = 0; i < 9; i++) { | ||
if (grid[row][i] === num || grid[i][col] === num) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function usedInBox(grid, row, col, num) { | ||
for (let i = 0; i < 3; i++) { | ||
for (let x = 0; x < 3; x++) { | ||
if(grid[i + row][x + col] === num) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
const grid = [ | ||
[3, 0, 6, 5, 0, 8, 4, 0, 0], | ||
[5, 2, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 8, 7, 0, 0, 0, 0, 3, 1], | ||
[0, 0, 3, 0, 1, 0, 0, 8, 0], | ||
[9, 0, 0, 8, 6, 3, 0, 0, 5], | ||
[0, 5, 0, 0, 9, 0, 6, 0, 0], | ||
[1, 3, 0, 0, 0, 0, 2, 5, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 7, 4], | ||
[0, 0, 5, 2, 0, 6, 3, 0, 0] | ||
]; | ||
console.log(solveSudoku(grid)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters