(搜索) poj 2676

M - Sudoku

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 2676

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int tt,a[10][10],row[10][10],col[10][10],grid[10][10];
bool flag;
char s[10][10];
void print()
{
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<9;j++)
        {
            printf("%d",a[i][j]);
        }
        printf("\n");
    }
}
void dfs(int x,int y)
{
    if(x==9)
    {
        flag=true;
        return ;
    }
    if(a[x][y]!=0)
    {
        if(y==8)
            dfs(x+1,0);
        else
            dfs(x,y+1);
    }
    else
    {
        int ret=3*(x/3)+y/3;
        for(int i=1;i<=9;i++)
        {
            if(row[x][i]||col[y][i]||grid[ret][i]) continue;
            a[x][y]=i;
            row[x][i]=1,col[y][i]=1,grid[ret][i]=1;
            if(y==8)
                dfs(x+1,0);
            else
                dfs(x,y+1);
            if(flag) return ;
            a[x][y]=0;
            row[x][i]=0,col[y][i]=0,grid[ret][i]=0;
        }
    }
}
int main()
{
    scanf("%d",&tt);
    while(tt--)
    {
        flag=false;
        memset(a,0,sizeof(a));
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        memset(grid,0,sizeof(grid));
        for(int i=0;i<9;i++)
            scanf("%s",s[i]);
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                a[i][j]=s[i][j]-‘0‘;
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                if(a[i][j]!=0)
                {
                    row[i][a[i][j]]=1;
                    col[j][a[i][j]]=1;
                    grid[3*(i/3)+j/3][a[i][j]]=1;
                }
            }
        }
        dfs(0,0);
        print();
    }
    return 0;
}

  

时间: 2024-11-25 05:45:17

(搜索) poj 2676的相关文章

POJ 2676 Sudoku (搜索,Dancing Links)

题目: http://poj.org/problem?id=2676 题意: 数独,每行1-9,每列1-9,每3*3小格1-9,填数,不能重复 方法:Dancing Links(16ms)或者DFS暴搜(400-900ms) Dancing Links(DLX) 是为了解决矩阵精确覆盖问题的算法,算法效率非常高 使用DLX解决的问题必须转化为矩阵精确覆盖问题: 1.DLX详解: http://wenku.baidu.com/view/d8f13dc45fbfc77da269b126.html 2

poj 2676 Sudoku (dfs)

链接:poj 2676 题意:给定一个未完成的数独,0是待填位置,其他均为已填入的数字.如果能将其 补充完整,则输出补充完整的数独(有多组答案输出任意一组),否则原样输出 数独:一个9行9列的网格,包括9个3*3的子网格,要求每行.每列.每个子网格内 都只能使用一次1-9中的一个数字, 即每行.每列.每个子网格内都不允许出现相同的数字. 分析:对于每一个未填的格,依次判断它所在行.列.子网格是否填了1-9, 若都未填,先填上该值,继续搜索, 若无法填写了,再回溯,填上其他可能的值,继续搜索 看别

POJ 2676 Sudoku (数独)

经典搜索问题,主要是时间上的优化,我用了三个辅助数组记录信息 row[i][k] = 1表示第i行数字k已经被使用,col[j][k] = 1表第j列数字k已经被使用,blo[i][k]表示第i个小九宫格中数字k已经被使用 还有很重要的一个优化(没有优化的话可能会超时,或者非常慢,像POJ讨论区里有很多说正着搜超时,倒着搜0ms,这的确是一个可以用的方法,但是有一定的随机性),每次填数字时,先扫描一遍整个矩阵,找出可选数字情况最少的那个0所在的地方,优先填这里,这样会使得搜索树尽可能的"瘦&qu

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o

POJ 2676 数码问题DLX

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13023   Accepted: 6455   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.

poj 2676 如何填满九宫格

Sudoku Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main Special Judge [Submit] [Status] [Discuss] Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to

ACM : POJ 2676 SudoKu DFS - 数独

SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 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 c

搜索 --- 数独求解 POJ 2676 Sudoku

Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直接DFS,注意从后往前搜索,时间比正向搜快很多.16ms水过 Time complexity: O(n) Source code:  // Memory Time // 1347K 0MS // by : crazyacking // 2015-04-10-14.30 #include<map>

POJ 2676 Sudoku

题目链接:http://poj.org/problem?id=2676 数独问题.用dancing links解决. 建图参考 http://wenku.baidu.com/link?url=3Tk5gVYew3mSQ2f2LxDODxPg3v-yqJPUaEkuZpfkHTxfSPQuM_n8TGl2Swp68XQY9MYN2BENZ-pmv9dpoh3Ulqt1lT-ZNo90jcJyi1eXasm 要注意的就是搜索的时候 要先搜索 那一列中节点最少的,否则时间会很长. 1 #include