POJ Sudoku 【DFS】


Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9679   Accepted: 4791   Special Judge

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

思路:

要填的数字有 1~9 ,判断是否可以填的条件有,两个,1, 3*3的矩阵内是否会重复,2,纵向和横向是否会重复,只需要把每一个3*3的矩阵内的数字用一个数组标记下,即非零的部分标记为 1 ,否则为0,纵横向直接遍历判断即可;


#include<iostream>
#include<string>
#include<map>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;

int book[10][10],mp[10][10],flag;

int check(int x_,int y_,int n);
void dfs(int x,int y) {
    if(x == 8 && y == 8) {
        flag = 1;
        return;
    }
    int p_x = -1,p_y = -1;
    //找出为空的 坐标然后退出下面这个双重循环
    for(int i = x ; i < 9 ; ++i) {
        for(int j = 0 ; j < 9 ; ++j) {
                if(!mp[i][j]) {
                        p_x = i;p_y = j;
                        break;
            }
        }
        if(p_x != -1 && p_y != -1) break;
    }
    // 如果上面的循环都没能找到一个为空的坐标,说明已经把空填满了;
    if(p_x == -1 && p_y == -1) {flag = 1; return;}
    for(int k = 1 ; k <= 9 ; ++k) {
        if(!book[(p_x/3)*3+p_y/3+1][k] && check(p_x,p_y,k)) {
            mp[p_x][p_y] = k;  // 更新坐标
            book[(p_x/3)*3+p_y/3+1][k] = 1; //3*3矩阵的标记
            dfs(p_x,p_y);
            if(flag == 1) return;
            book[(p_x/3)*3+p_y/3+1][k] = 0;
            mp[p_x][p_y] = 0; // 还原坐标
        }
    }
}
// 检查 横向和纵向是否存在相同的数字,是这返回0否则1;
int check(int x_,int y_,int n) {
    for(int i = 0 ; i < 9 ; ++i) if(mp[i][y_] == n) return 0;
    for(int i = 0 ; i < 9 ; ++i) if(mp[x_][i] == n) return 0;
    return 1;
}

int main(void)
{
    int n;
    cin >> n;
        while(n--) {
            memset(book,0,sizeof(book));
            for(int i = 0 ; i < 9 ; ++i) {
                for(int j = 0 ; j < 9 ; ++j) {
                    scanf("%1d",&mp[i][j]);
                    // (i/3)*3 + j/3 + 1 是判定 3*3矩阵的区域,
                    // 每层三个 3*3的区域块,从左至右开始编号 123/456/789,一个矩阵内的非0数
                    // 用book来标记,非0则标记为 1 否则为0;
                    if(mp[i][j]) book[(i/3)*3+j/3+1][mp[i][j]] = 1;
                }
            }
            flag = 0;
            dfs(0,0);
            for(int i = 0 ; i < 9 ; ++i) {
                for(int j = 0 ; j < 9 ; ++j)
                    cout << mp[i][j];
                cout << endl;
            }
        }
    return 0;
}








猜你喜欢

转载自blog.csdn.net/godleaf/article/details/80581564