POJ 2676 Sudoku 数独(dfs)

**

POJ 2676 Sudoku 数独(dfs)

**

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

题意

9x9的小方格里面,每一行、每一列、每一个九宫格里面都含1-9里面的不同的数字,给出一个矩阵,0表示空,其他表示已有数字,找出其中一个可能的解。

思路

找可行解无疑dfs比bfs快,对于每一个小方格,我们尝试每一个可能的解,如果可行则继续下去,如果不合适则回溯,直至所有方格都填满,题目不难,就是数据输入与处理的时候需要注意。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
int m[10][10]//存储数独表;
int vish[10][10], visl[10][10], visz[10][10]//分别表示行、列、九宫格里面的数字是否已经用过;
using namespace std;

bool dfs(int r, int c){
	if( r == 10 ) return true;
	bool flag = false;
	if(m[r][c]){
        if(c==9) flag = dfs(r+1,1); //如果这一行已经搜完,则搜下一行
        else flag = dfs(r, c+1);
        return flag;
	}
	int h = (r-1)/3;
	h *= 3;
	int l =(c+2)/3;
    for(int k = 1; k <= 9; k++){
        if(!vish[r][k]&&!visl[c][k]&&!visz[h+l][k]){
            m[r][c] = k;
            vish[r][k] = 1;
            visl[c][k] = 1;
            visz[h+l][k] = 1;
            if(c == 9) flag = dfs(r+1, 1);
            else flag = dfs(r, c+1);
            if(flag == true) return true;
            m[r][c] = 0;
            vish[r][k] = 0;
            visl[c][k] = 0;
            visz[h+l][k] = 0;
        }
    }
    return false;
}

int main(){
    int i, j, T;
    char c;
    scanf("%d",&T);
    while(T--){
        memset(vish,0,sizeof(vish));
        memset(visl,0,sizeof(visl));
        memset(visz,0,sizeof(visz));
        for(i = 1; i <= 9; i++){
            for(j = 1; j <= 9; j++){
                cin >> c;
                m[i][j] = c - '0';
                vish[i][m[i][j]] = 1;
                visl[j][m[i][j]] = 1;
                int h = (i-1)/3;
                h *= 3;
                int l = (j+2)/3;
                visz[h+l][m[i][j]] = 1;
            }
        }
        dfs(1, 1);
        for(i = 1; i <= 9; i++){
            for(j = 1; j <= 9; j++){
                printf("%d",m[i][j]);
            }
            printf("\n");
        }
    }
}

你可能感兴趣的:(dfs,dfs,搜索)