zoukankan      html  css  js  c++  java
  • poj 2676(DLX)

    第一次做数独的题目, 最纠结的是建矩阵无疑。。。 想了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;
    }
    
    
     
  • 相关阅读:
    pandas Series和DataFrame数据类型
    numpy 统计函数与随机数
    numpy 索引
    numpy 数组复制与广播机制
    numpy 合并数组和切割数组
    numpy 添加删除去重及形状变换
    项目导入问题---讨厌的红色感叹号
    SpringMVC框架-----概述(2)
    SpringMVC框架-----概述(1)
    SpringBoot框架----概述(1)
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3000735.html
Copyright © 2011-2022 走看看