Dancing Links---- E - Sudoku

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. 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. 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–OE–
F-M–D--L-K-A
-C--------O-I-LH-
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

和上一道3阶数独题是一样的,但是这里有坑
1>virtual judge上的显示有问题,不是每行16个,应该是爬数据的问题
2>一直segment fault是因为空间开太大了
3>这道题是多组输入而且数据间得有换行,没有多组输入就wa,没有换行就pe

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const double eps = 1e-8;
const double PI = acos(-1);

//最大行数
const int MN = 4100;
//最大列数
const int MM = 1100;
//最大点数
const int MNN = 16 * 16 * 16 * 4 + 100;

struct DLX
{
    //一共n行m列,s个节点
    int n,m,s;
    //交叉十字链表组成部分
    //第i个节点的上U下D左L右R,所在位置row行col列
    int U[MNN],D[MNN],L[MNN],R[MNN],row[MNN],col[MNN];
    //H数组记录行选择指针,S数组记录覆盖个数
    int H[MN],S[MM];
    //res记录行个数,ans数组记录可行解
    int res,ans[MN];
    //初始化空表
    void init(int x,int y)
    {
        n = x,m = y;
        //其中0节点作为head节点,其他作为列首节点
        for(int i = 0;i <= m;++i){
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;L[0] = m;
        s = m;
        memset(S,0,sizeof(S));
        memset(H,-1,sizeof(H));
    }
    void Insert(int r,int c)
    {
        //cout << c << endl;
        //节点数加一,设置s节点所处位置,以及S列覆盖个数加一
        s++;row[s] = r;col[s] = c;S[c]++;
        //将s节点插入对应列中
        D[s] = D[c];U[D[c]] = s;
        U[s] = c;D[c] = s;
        if(H[r] < 0){//如果该行没有元素,H[r]标记该行起始节点
            H[r] = L[s] = R[s] = s;
        }else{
            //将该节点插入该行第一个节点后面
            R[s] = R[H[r]];
            L[R[H[r]]] = s;
            L[s] = H[r];
            R[H[r]] = s;
        }
    }
    //精确覆盖
    void Remove(int c)
    {
        //删除c列
        L[R[c]] = L[c];R[L[c]] = R[c];
        //删除该列上的元素对应的行
        for(int i = D[c];i != c;i = D[i]){//枚举该列元素
            for(int j = R[i];j != i;j = R[j]){//枚举列的某个元素所在行遍历
                U[D[j]] = U[j];
                D[U[j]] = D[j];
                //将该列上的S数组减一
                --S[col[j]];
            }
        }
    }
    void resume(int c)
    {
        //恢复c列
        for(int i = U[c];i != c;i = U[i]){//枚举该列元素
            for(int j = L[i];j != i;j = L[j]){
                U[D[j]] = j;D[U[j]] = j;
                ++S[col[j]];
            }
        }
        L[R[c]] = c;R[L[c]] = c;
    }
    bool dance(int deep)
    {
        //当矩阵为空时,说明找到一个可行解,算法终止
       // cout << "R:" << R[0] << endl;
        if(R[0] == 0){
            res = deep;
            return true;
        }
        //找到节点数最少的列,枚举这列上的所有行
        int c = R[0];
        for(int i = R[0];i != 0;i = R[i]){
            if(S[i] < S[c]){
                c = i;
            }
        }
        //cout << c << endl;
        //删除节点数最少的列
        Remove(c);
        for(int i = D[c];i != c;i = D[i]){
            //将行r放入当前解
            ans[deep] = row[i];
            //行上节点对应的列上进行删除
            for(int j = R[i];j != i;j = R[j])
                Remove(col[j]);
            //进入下一层
            if(dance(deep + 1)) return true;
            //对行上的节点对应的列进行恢复
            for(int j = L[i];j != i;j = L[j])
                resume(col[j]);
        }
        //恢复节点数最少列
        resume(c);
        return false;
    }

    //重复覆盖
    //将列与矩阵完全分开
    void Remove1(int c)
    {
        for(int i = D[c];i != c;i = D[i]){
            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }
    }
    void resume1(int c)
    {
        for(int i = D[c];i != c;i = D[i]){
            L[R[i]] = R[L[i]] = i;
        }
    }
    int vis[MNN];
    //估价函数,模拟删除列,H(),函数返回的是至少还需要多少行才能完成重复覆盖
    int A()
    {
        int dis = 0;
        for(int i = R[0];i != 0;i = R[i]) vis[i] = 0;
        for(int i = R[0];i != 0;i = R[i]){
            if(!vis[i]){
                dis++;vis[i] = 1;
                for(int j = D[i];j != i;j = D[j]){
                    for(int k = R[j];k != j;k = R[k]){
                        vis[col[k]] = 1;
                    }
                }
            }
        }
        return dis;
    }

    bool dfs(int deep)
    {
        if(deep + A() > res) return false;
        if(!R[0]) return true;
        int c = R[0];
        for(int i = R[0];i != 0;i = R[i]){
            if(S[i] < S[c]){
                c = i;
            }
        }
        for(int i = D[c];i != c;i = D[i]){
            //每次将第i列其他节点删除,只保留第i节点,为了找该行的节点
            Remove1(i);
            //将列上的节点完全与矩阵脱离,只删列首节点是不行的
            for(int j = R[i];j != i;j = R[j]){
                Remove1(j);
            }
            if(dfs(deep + 1)) return true;
            for(int j = L[i];j != i;j = L[j]){
                resume1(j);
            }
            resume1(i);
        }
        return false;
    }
    int IDEA(int k)
    {
        res = 0;
        while(true)
        {
            if(res > k) break;
            //cout << res << endl;
            if(dfs(0)) return res;
            res++;
        }
        return -1;
    }
}dlx;

const int N = 20;
char s[N][N];
int p[N][N];
pair<pair<int,int>,int>ve[4100];

void init()
{
    dlx.init(16 * 16 * 16,4 * 16 * 16);
    int cnt = 1;
    for(int x = 0;x < 16;++x){
        for(int y = 0;y < 16;++y){
            if(s[x][y] != '-'){
                //cout << s[x][y] << endl;
                int k = s[x][y] - 'A' + 1;
                p[x][y] = k;
                //cout << x << " " << y << " " << k << endl;
                int z = (x / 4) * 4 + (y / 4);
                dlx.Insert(cnt,x * 16 + y + 1);
                dlx.Insert(cnt,256 + x * 16 + k);
                dlx.Insert(cnt,2 * 256 + 16 * y + k);
                dlx.Insert(cnt,3 * 256 + 16 * z + k);
                ve[cnt].fi = mp(x,y);ve[cnt].se = k;
                cnt++;
            }else{
                int z = (x / 4) * 4 + (y / 4);
                for(int j = 1;j <= 16;++j){
                    dlx.Insert(cnt,x * 16 + y + 1);
                    dlx.Insert(cnt,256 + x * 16 + j);
                    dlx.Insert(cnt,2 * 256 + 16 * y + j);
                    dlx.Insert(cnt,3 * 256 + 16 * z + j);
                    ve[cnt].fi = mp(x,y);ve[cnt].se = j;
                    cnt++;
                }
            }
        }
    }
    //cout << cnt << endl;
    dlx.res = inf;
    dlx.dance(0);
    //cout << -1 << endl;
    for(int i = 0;i < dlx.res;++i){
        p[ve[dlx.ans[i]].fi.fi][ve[dlx.ans[i]].fi.se] = ve[dlx.ans[i]].se;
    }
    for(int i = 0;i < 16;++i){
        for(int j = 0;j < 16;++j){
            printf("%c",p[i][j] + 'A' - 1);
        }
        printf("\n");
    }
}

int main()
{
    bool flag = false;
    while(~scanf("%s",s[0])){
        if(flag) printf("\n");
        flag = true;
        for(int i = 1;i < 16;++i){
            scanf("%s",s[i]);
            //printf("%s\n",s[i]);
        }
        init();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/82928220