POJ 2676 dfs

原创 2015年11月17日 16:25:43

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;
}
版权声明:博客迁移到 yaokun.wiki

相关文章推荐

poj2676 sudoku dfs

传送门:poj2676 题意就是解数独呗。。 解题的关键在于book数组,一开始我的book[i][j][k]是用来记录i,j点上可不可以用k这个数字,然后dfs的时候每在i,j点填入一个值k,就将第...

POJ 2676 Sudoku dfs

题意:九宫格游戏。给定一个九宫格,他的每一行每一列以及每一个3*3的子格都必须由1,2,3,4,5,6,7,8,9组成。不能多也不能少。求出一种满足条件的填充方案。 题解:貌似正搜得效率比反搜差不少...
  • Tsaid
  • Tsaid
  • 2011年09月08日 01:34
  • 388

POJ 2676 Sudoku(DFS)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16024   Accepted: 7824   S...

POJ-2676-Sudoku-DFS

题意:每行每列每个九空格数字不同,输出一种符合题意即可 思路: DFS&暴力 #include #include #include int mp[10][10]; int mx[10][10]; in...

POJ 2676-Sudoku(DFS)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14120   Accept...

[DFS]poj 2676 Sudoku 数独问题

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14917   Accepted: 7372   S...

POJ-2676 Sudoku(DFS)

题意:将数字1到9,填入9x9矩阵中的小方格,使得矩阵中的每行,每列,每个3x3的小格子内,9个数字都会出现。 思路:dfs  挨个尝试 #include #include #include u...

POJ 2676 sudoku DFS

E - Sudoku Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status...

【POJ 2676】Sudoku数独(DFS)

思路:DFS遍历解数独,很有意思的题目,由于行、列、每一个3*3小方格都不能重复,故需要三个数组row,col,dig来标记某1~9数字是否出现。DFS遍历,具体思路见注释。...

poj 2676 Sudoku (基础DFS)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15456   Accept...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2676 dfs
举报原因:
原因补充:

(最多只允许输入30个字)