关闭

POJ 2676 dfs

标签: dfspoj
104人阅读 评论(0) 收藏 举报
分类:

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16403 Accepted: 8013 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
Source

Southeastern Europe 2005

题意:
给一个数独题,然后用程序填空,如果无法完成(完成要求横行,竖行,每个小九宫格内都没有重复数字)则输出原来的数独题;
解法:
搜索回溯就可以过。用了一个小剪枝就是如果已经找到一个可行解就立马结束,递归返回时其余的枝丫全部结束返回。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

typedef struct{
    int st,en;
}Po;

char G[12][12];
Po box[] = {{0,2},{3,5},{6,8}};

Po Get(void){
    int i,j,ok = 1;
    Po tem;
    for(i = 0;i<9;i++)
        for(j = 0;j<9;j++){
            if('0' == G[i][j]){
                tem.st = i;
                tem.en= j;
                return tem;
            }
        }
    tem.st = tem.en = -1;
    return tem;
}

int dfs(int x,int y){
    int a[12] = {0};
    int i,j;
    for(i = 0;i<9;i++){
        a[G[x][i] - '0'] = 1;
        a[G[i][y] - '0'] = 1;
    }
    int xx = x/3;
    int yy = y/3;
    for(i = box[xx].st;i<=box[xx].en;i++)
        for(j = box[yy].st;j<=box[yy].en;j++)
            a[G[i][j] - '0'] = 1;
    for(i = 1;i<=9;i++){
        if(a[i] == 0){
            G[x][y] = '0' + i;
            Po tem = Get();
            if(tem.st == -1&&tem.en == -1)
                return 1;
            if(dfs(tem.st,tem.en))
                return 1;
            G[x][y] = '0';
        }
    }
    return 0;
}

int main(){
//    freopen("data.in","r",stdin);
    int N;
    scanf("%d",&N);
    getchar();
    while(N--){
        int i,j;
        for(i = 0;i<9;i++){
            scanf("%s",G[i]);
            getchar();
        }
        Po S = Get();
        dfs(S.st,S.en);
        for(i = 0;i<9;i++)
            printf("%s\n",G[i]);
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30886次
    • 积分:2026
    • 等级:
    • 排名:第19695名
    • 原创:179篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论