/
DeC2018
/
0037
Обзор
Документация
Войти
/
DeC2018
/
0037
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
66 строк
2 KB
Den
create main.cpp
18 янв 2025, 03:38
18 янв 2025, 03:38
4bee081
Код
Авторство
О чём код?
#include <stdio.h> #include <stdbool.h> void solveSudoku(char board[9][9]); bool solve(char board[9][9]); bool isValid(char board[9][9], int row, int col, char num); void solveSudoku(char board[9][9]) { solve(board); } bool solve(char board[9][9]) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { if (board[row][col] == '.') { for (char num = '1'; num <= '9'; num++) { if (isValid(board, row, col, num)) { board[row][col] = num; // Place the number if (solve(board)) { // Recur return true; } board[row][col] = '.'; // Backtrack } } return false; // No valid number found } } } return true; // Solved } bool isValid(char board[9][9], int row, int col, char num) { for (int i = 0; i < 9; i++) { if (board[row][i] == num || board[i][col] == num || board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == num) { return false; // Check row, column, and box } } return true; // Valid placement } int main() { char board[9][9] = { {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'} }; solveSudoku(board); // Print the solved board for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { printf("%c ", board[row][col]); } printf("\n"); } return 0; }