poj 3076(DLX)

16*16的数独, 跟9*9的类似。。。

 

Sudoku
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 3683 Accepted: 1799

Description

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.  
 
Write a Sudoku playing program that reads data sets from a text file.

Input

Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i-th string stands for the i-th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,…,P,-}, where – (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file.

Output

The program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK

Source

 

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

char g[20][20];
int ans[5000];
int u[N],d[N],r[N],l[N],num[N],H[N],save[N],save1[N];
int flag,head;
const int n=4096;
const int m=1024;
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<=256;i++)
    {
        for(int j=1;j<=16;j++)
        {
            ++tn;
            link(tn,i);
        }
    }
    sum=256;
    /////
    for(int i=1;i<=16;i++) // 每一行
    {
        tn=(i-1)*256;
        for(int k=1;k<=16;k++)
        {
            int tk=tn+k;
            for(int j=1;j<=16;j++)
            {
                link(tk,sum+(i-1)*16+k);
                tk+=16;
            }
        }
    }
    sum+=256;
    ///
    for(int i=1;i<=16;i++)
    {
        tn=(i-1)*16;
        for(int k=1;k<=16;k++)
        {
            int tk=tn+k;
            for(int j=1;j<=16;j++)
            {
                link(tk,sum+(i-1)*16+k);
                tk+=256;
            }
        }
    }
    sum+=256;
    /
    int tt=0;
    for(int i1=1;i1<=4;i1++)
    {
        for(int j1=1;j1<=4;j1++)
        {
            tn=(i1-1)*256*4+16*4*(j1-1);
            for(int k=1;k<=16;k++)
            {
                ++tt;
                int tk;
                for(int i=1;i<=4;i++)
                {
                    for(int j=1;j<=4;j++)
                    {
                        tk=tn+(i-1)*256+16*(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)/256+1;
            tj= (tans%256)/16+1;
            tk= (tans%256)%16+1;
            //printf("<%d %d> ",ti,tj);
            g[ti][tj]=tk+'A'-1;
        }
        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()
{
    while(scanf("%s",g[1]+1)!=EOF)
    {
        int tu=0;
        build();
            for(int j=1;j<=16;j++)
            {
                if(g[1][j]!='-')
                {
                    int kk=g[1][j]-'A'+1;
                    remove( save[ H[tu+kk] ] );
                    for(int i1=r[ H[tu+kk] ];i1 != H[tu+kk];i1=r[i1])
                    {
                        remove( save[i1] );
                    }
                }
                tu+=16;
            }
        for(int i=2;i<=16;i++)
        {
            scanf("%s",g[i]+1);
            for(int j=1;j<=16;j++)
            {
                if(g[i][j]!='-')
                {
                    int kk=g[i][j]-'A'+1;
                    remove( save[ H[tu+kk] ] );
                    for(int i1=r[ H[tu+kk] ];i1 != H[tu+kk];i1=r[i1])
                    {
                        remove( save[i1] );
                    }
                }
                tu+=16;
            }
        }
        flag=0;
        dfs(0);
        for(int i=1;i<=16;i++)
        {
            for(int j=1;j<=16;j++)
                printf("%c",g[i][j]);
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值