Created
May 31, 2021 15:02
-
-
Save malibayram/2845cf8b204c18fede6614af6124a040 to your computer and use it in GitHub Desktop.
https://youtu.be/8ext9G7xspg?t=6715 python ile anlatılan sudoku çözme ileminin dart ile kodlanması
This file contains hidden or 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
// https://youtu.be/8ext9G7xspg?t=6715 | |
List findNextEmpty(List<List<int>> sudoku) { | |
for (int r = 0; r < 9; r++) { | |
for (int c = 0; c < 9; c++) { | |
if (sudoku[r][c] == 0) { | |
return [r, c]; | |
} | |
} | |
} | |
return [null, null]; | |
} | |
bool isValid(List<List<int>> sudoku, int guess, int row, int col) { | |
if (sudoku[row].contains(guess)) { | |
return false; | |
} | |
if (List.generate(9, (i) => sudoku[i][col]).contains(guess)) { | |
return false; | |
} | |
int rowStart = (row / 3).floor() * 3; | |
int colStart = (col / 3).floor() * 3; | |
for (int r = rowStart; r < rowStart + 3; r++) { | |
for (int c = colStart; c < colStart + 3; c++) { | |
if (sudoku[r][c] == guess) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
bool solveSudoku(List<List<int>> sudoku) { | |
List rc = findNextEmpty(sudoku); | |
if (rc[0] == null) return true; | |
for (int guess = 1; guess < 10; guess++) { | |
if (isValid(sudoku, guess, rc[0], rc[1])) { | |
sudoku[rc[0]][rc[1]] = guess; | |
if (solveSudoku(sudoku)) { | |
return true; | |
} | |
} | |
sudoku[rc[0]][rc[1]] = 0; | |
} | |
return false; | |
} | |
void main() { | |
List<List<int>> sudoku = [ | |
[0, 0, 3, 0, 2, 0, 6, 0, 0], | |
[9, 0, 0, 3, 0, 5, 0, 0, 1], | |
[0, 0, 1, 8, 0, 6, 4, 0, 0], | |
[0, 0, 8, 1, 0, 2, 9, 0, 0], | |
[7, 0, 0, 0, 0, 0, 0, 0, 8], | |
[0, 0, 6, 7, 0, 8, 2, 0, 0], | |
[0, 0, 2, 6, 0, 9, 5, 0, 0], | |
[8, 0, 0, 2, 0, 3, 0, 0, 9], | |
[0, 0, 5, 0, 1, 0, 3, 0, 0], | |
]; | |
if (solveSudoku(sudoku)){ | |
print(sudoku); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment