POJ 2676 Sudoku【DFS】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37867156/article/details/82597836

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

题解:暴搜。主要是要确定当前 i ,j 是在第几个方格中。设 a = i / 3,b = j / 3,k 表示是第几个方格,则有 3a+b = k。(在草稿纸上面画画就看出来了)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Sudoku[10][10];
int row_has[10][10];//row_has[i][v]:第i行是否已存在值v。
int cow_has[10][10];
int grid_has[10][10];//grid[i][v]:第i个方块中是否已存在v
int dfs(int x, int y) {
    if(x == 10 && y == 1)
        return 1;
    if(Sudoku[x][y]) {
        return dfs(y == 9 ? x+1 : x, y ==9 ? 1 : y+1);
    }else {
        for(int v = 1; v <= 9; v++) {
            if(!row_has[x][v] && !cow_has[y][v] && !grid_has[(x-1)/3*3+(y-1)/3+1][v]) {
                Sudoku[x][y] = v;
                row_has[x][v] = 1;
                cow_has[y][v] = 1;
                grid_has[(x-1)/3*3+(y-1)/3+1][v] = 1;
                int flag = dfs(y == 9 ? x+1 : x, y ==9 ? 1 : y+1);
                if(flag) return 1;
                else  {
                    Sudoku[x][y] = 0;
                    row_has[x][v] = 0;
                    cow_has[y][v] = 0;
                    grid_has[(x-1)/3*3+(y-1)/3+1][v] = 0;
                }
            }
        }
    }
    return 0;
}

int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        memset(row_has, 0, sizeof row_has);
        memset(cow_has, 0, sizeof cow_has);
        memset(grid_has, 0, sizeof grid_has);
        for(int i = 1; i <= 9; i++) {
            for(int j = 1; j <= 9; j++) {
                scanf("%1d", &Sudoku[i][j]);
                row_has[i][Sudoku[i][j]] = 1;
                cow_has[j][Sudoku[i][j]] = 1;
                grid_has[3*((i-1)/3)+(j-1)/3+1][Sudoku[i][j]] = 1;
            }
        }
        dfs(1, 1);
        for(int i = 1; i <= 9; i++) {
            for(int j = 1; j <= 9; j++) {
                cout << Sudoku[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/82597836