Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- char head[15];
- bool lock;
- int table[10][10];
- void PrintTable(){
- printf("%s\n", head);
- for(int i = 0; i < 9; ++i){
- for(int j = 0; j < 9; ++j){
- cout << table[i][j] << " ";
- }
- cout << "\n";
- }
- return;
- }
- bool ValidNumber(int r, int c, int x){
- for(int i = 0; i < 9; ++i){
- if(table[i][c] == x|| table[r][i] == x){
- return false;
- }
- }
- for(int i = r - (r % 3); i < r - (r % 3) + 3; ++i){
- for(int j = c - (c % 3); j < c - (c % 3) + 3; ++j){
- if(table[i][j] == x){
- return false;
- }
- }
- }
- return true;
- }
- void Sudoku(int row, int col){
- if(row == 9 && col == 0){
- if(!lock){
- PrintTable();
- lock = true;
- }
- return;
- } else if(table[row][col] != 0){
- if(col == 8 && !lock){
- Sudoku(row + 1, 0);
- } else {
- Sudoku(row, col + 1);
- }
- return;
- } else {
- for(int i = 1; i <= 9; ++i){
- if(ValidNumber(row, col, i) && !lock){
- table[row][col] = i;
- if(col == 8){
- Sudoku(row + 1, 0);
- } else {
- Sudoku(row, col + 1);
- }
- table[row][col] = 0;
- }
- }
- return;
- }
- }
- int main(){
- freopen("input_sudoku.txt", "r", stdin);
- freopen("output_sudoku.txt", "w", stdout);
- int x;
- for(int k = 0; k < 100; ++k){
- lock = false;
- scanf(" %[^\n]", head);
- for(int i = 0; i < 9; ++i){
- for(int j = 0; j < 9; ++j){
- scanf("%d", &x);
- table[i][j] = x;
- }
- }
- Sudoku(0, 0);
- }
- fclose(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement