poj 2676(DLX)

本文介绍使用DLX算法快速解决数独问题的方法。通过构建特定的数据结构和高效的回溯搜索策略,实现对数独题目的求解。文章详细展示了如何建立数独问题的模型,并通过代码实例演示了解决过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次做数独的题目, 最纠结的是建矩阵无疑。。。 想了N久才把这个矩阵给弄好, 真的是很麻烦。 但是确实DLX 好快,解数独几乎是秒杀。。。

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

 

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 300000
#define INF 0x3fffffff

char g[10][10];
int ans[1000];
int u[5000],d[5000],r[5000],l[5000],num[5000],H[1000],save[5000],save1[5000];
int flag,head;
const int n=729;
const int m=324;
int id;

void prepare()
{
    for(int i=0;i<=m;i++)
    {
        num[i]=0;
        d[i]=i;
        u[i]=i;
        r[i]=i+1;
        l[i+1]=i;
    }
    r[m]=0;
    memset(H,-1,sizeof(H)); // 记录每一行的第一个点
}

void link(int tn,int tm)
{
    id++;
    save1[id]=tn; // 记录行
    ++num[save[id]=tm]; // 记录列
    d[id]=d[tm];
    u[ d[tm] ]=id;
    u[id]=tm;
    d[tm]=id;
    if(H[tn]<0) H[tn]=l[id]=r[id]=id;
    else
    {
        r[id]=r[H[tn]];
        l[ r[H[tn]] ]=id;
        r[ H[tn] ]=id;
        l[id]=H[tn];
    }
}

void build()
{
    id=m;
    int sum;
    prepare();
    int tn=0;
    for(int i=1;i<=81;i++)
    {
        for(int j=1;j<=9;j++)
        {
            ++tn;
            link(tn,i);
        }
    }
    sum=81;
    /////////////////
    for(int i=1;i<=9;i++) // 每一行
    {
        tn=(i-1)*81;
        for(int k=1;k<=9;k++)
        {
            int tk=tn+k;
            for(int j=1;j<=9;j++)
            {
                link(tk,sum+(i-1)*9+k);
                tk+=9;
            }
        }
    }
    sum+=81;
    ///////////////////////
    for(int i=1;i<=9;i++)
    {
        tn=(i-1)*9;
        for(int k=1;k<=9;k++)
        {
            int tk=tn+k;
            for(int j=1;j<=9;j++)
            {
                link(tk,sum+(i-1)*9+k);
                tk+=81;
            }
        }
    }
    sum+=81;
    /////////////////////////
    int tt=0;
    for(int i1=1;i1<=3;i1++)
    {
        for(int j1=1;j1<=3;j1++)
        {
            tn=(i1-1)*81*3+9*3*(j1-1);
            for(int k=1;k<=9;k++)
            {
                ++tt;
                int tk;
                for(int i=1;i<=3;i++)
                {
                    for(int j=1;j<=3;j++)
                    {
                        tk=tn+(i-1)*81+9*(j-1)+k;
                        link(tk,sum+tt);
                    }
                }
            }
        }
    }
}

void remove(int s)
{
    l[ r[s] ]=l[s];
    r[ l[s] ]=r[s];
    for(int i=d[s];i!=s;i=d[i])
        for(int j=r[i];j!=i;j=r[j])
        {
            u[d[j]]=u[j];
            d[u[j]]=d[j];
            num[save[j]]--;
        }
}

void resume(int s)
{
    r[l[s]]=s;
    l[r[s]]=s;
    for(int i=u[s];i!=s;i=u[i])
        for(int j=l[i];j!=i;j=l[j])
        {
            u[d[j]]=j;
            d[u[j]]=j;
            num[save[j]]++;
        }
}

void dfs(int s)
{
    if(flag) return ;
    if(r[head]==head)
    {
        flag=1;
        for(int i=0;i<s;i++)
        {
            int ti,tj,tk;
            int tans=save1[ans[i]]-1;
            ti= (tans)/81+1;
            tj= (tans%81)/9+1;;
            tk= (tans%81)%9+1;
            //printf("<%d %d> ",ti,tj);
            g[ti][tj]=tk+'0';
        }
        return ;
    }
    int mi=INF,tu;
    for(int i=r[head];i!=head;i=r[i])
        if(mi>num[i])
        {
            mi=num[i];
            tu=i;
        }
    remove(tu);
    for(int i=d[tu];i!=tu;i=d[i])
    {
        for(int j=r[i];j!=i;j=r[j])
            remove(save[j]);
        ans[s]=i;
        dfs(s+1);
        for(int j=l[i];j!=i;j=l[j])
            resume(save[j]);
    }
    resume(tu);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        build();
        int tu=0;
        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=9;j++)
            {
                cin>>g[i][j];
                if(g[i][j]!='0')
                {
                    int kk=g[i][j]-'0';
                    remove( save[ H[tu+kk] ] );
                    for(int i1=r[ H[tu+kk] ];i1 != H[tu+kk];i1=r[i1])
                    {
                        remove( save[i1] );
                    }
                }
                tu+=9;
            }
        }
        flag=0;
        dfs(0);
        printf("\n");
        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=9;j++)
                printf("%c",g[i][j]);
            printf("\n");
        }
    }
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值