diff --git a/main and 4.c b/main and 4.c new file mode 100644 index 0000000..4e598e2 --- /dev/null +++ b/main and 4.c @@ -0,0 +1,98 @@ +#include +int main() +{ + void function1(); + void function2(); + void function3(); + void function4(); + return 0; + function1(); + function2(); + function3(); + function4(); +} + + +#include + +int sudoku[9][9]; + + +int in_row(int row, int num) { + for (int i = 0; i < 9; i++) { + if (sudoku[row][i] == num) { + return 1; + } + } + return 0; +} + + +int in_col(int col, int num) { + for (int i = 0; i < 9; i++) { + if (sudoku[i][col] == num) { + return 1; + } + } + return 0; +} + + +int in_box(int row, int col, int num) { + int box_row = row / 3 * 3; + int box_col = col / 3 * 3; + for (int i = box_row; i < box_row + 3; i++) { + for (int j = box_col; j < box_col + 3; j++) { + if (sudoku[i][j] == num) { + return 1; + } + } + } + return 0; +} + + +int is_valid(int row, int col, int num) { + return !in_row(row, num) && !in_col(col, num) && !in_box(row, col, num); +} + + +int solve_sudoku(int row, int col) { + if (row == 9) { + return 1; + } + if (col == 9) { + return solve_sudoku(row + 1, 0); + } + if (sudoku[row][col] != 0) { + return solve_sudoku(row, col + 1); + } + for (int num = 1; num <= 9; num++) { + if (is_valid(row, col, num)) { + sudoku[row][col] = num; + if (solve_sudoku(row, col + 1)) { + return 1; + } + sudoku[row][col] = 0; + } + } + return 0; +} + +void function4() +{ + function1(); + if (solve_sudoku(0, 0)) { + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + printf("%d ", sudoku[i][j]); + } + printf("\n"); + } + } + else { + printf("No solution!\n"); + } + return 0; +}