POJ3076-Sudoku

本文介绍了一个解决16x16数独问题的程序实现。通过使用舞蹈链算法来寻找唯一解,该程序能够读取特定格式的输入文件,并输出解决方案。

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

Sudoku
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 5250 Accepted: 2489

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



题意:和数独问题类似,只是变成16*16的方格

解题思路:舞蹈链的精确覆盖


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cctype>  
#include <map>  
#include <cmath>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  
  
using namespace std;  
  
#define LL long long  
const int INF = 0x3f3f3f3f;  
const int maxn = 300005;  
  
int n, m, a[20][20], tot, p[10000][3];  
char ch[20][20];  
  
struct DLX  
{  
    int L[maxn], R[maxn], U[maxn], D[maxn];  
    int row[maxn], col[maxn], ans[maxn], sum[maxn];  
    int n, m, num, cnt;  
    void add(int k, int l, int r, int u, int d, int x, int y)  
    {  
        L[k] = l;   R[k] = r;   U[k] = u;  
        D[k] = d;   row[k] = x;  col[k] = y;  
    }  
    void reset(int n, int m)  
    {  
        this->n = n;   this->m = m;  
        for (int i = 0; i <= m; i++)  
        {  
            add(i, i - 1, i + 1, i, i, 0, i);  
            sum[i] = 0;  
        }  
        L[0] = m, R[m] = 0, cnt = m + 1;  
    }  
    void insert(int x, int y)  
    {  
        int temp = cnt - 1;  
        if (row[temp] != x)  
        {  
            add(cnt, cnt, cnt, U[y], y, x, y);  
            U[D[cnt]] = cnt; D[U[cnt]] = cnt;  
        }  
        else  
        {  
            add(cnt, temp, R[temp], U[y], y, x, y);  
            R[L[cnt]] = cnt; L[R[cnt]] = cnt;  
            U[D[cnt]] = cnt; D[U[cnt]] = cnt;  
        }  
        sum[y]++, cnt++;  
    }  
    void remove(int k)  
    {  
        R[L[k]] = R[k], L[R[k]] = L[k];  
        for (int i = D[k]; i != k; i = D[i])  
            for (int j = R[i]; j != i; j = R[j])  
            {  
                D[U[j]] = D[j];  
                U[D[j]] = U[j];  
                sum[col[j]]--;  
            }  
    }  
    void resume(int k)  
    {  
        R[L[k]] = k, L[R[k]] = k;  
        for (int i = D[k]; i != k; i = D[i])  
            for (int j = R[i]; j != i; j = R[j])  
            {  
                D[U[j]] = j;  
                U[D[j]] = j;  
                sum[col[j]]++;  
            }  
    }  
    bool dfs(int k)  
    {  
        if (!R[0]) { num = k; return true; }  
        int now = R[0];  
        for (int i = now; i != 0; i = R[i])  
            if (sum[now] > sum[i]) now = i;  
        remove(now);  
        for (int i = D[now]; i != now; i = D[i])  
        {  
            ans[k] = row[i];  
            for (int j = R[i]; j != i; j = R[j]) remove(col[j]);  
            if (dfs(k + 1)) return true;  
            for (int j = L[i]; j != i; j = L[j]) resume(col[j]);  
        }  
        resume(now);  
        return false;  
    }  
    void display()  
    {  
        for (int i = 0; i < num; i++)  
            a[p[ans[i]][0]][p[ans[i]][1]] = p[ans[i]][2];  
        for (int i = 1; i <= 16; i++)  
        {  
            for (int j = 1; j <= 16; j++) printf("%c", 'A' + a[i][j] - 1);  
            printf("\n");  
        }  
        printf("\n");  
    }  
}dlx;  
  
void insert(int x, int y, int z)  
{  
    p[++tot][0] = x, p[tot][1] = y, p[tot][2] = z;  
    dlx.insert(tot, 16 * (x - 1) + y);  
    dlx.insert(tot, 256 + 16 * (x - 1) + z);  
    dlx.insert(tot, 512 + 16 * (y - 1) + z);  
    x = (x - 1) / 4 * 4 + (y - 1) / 4 + 1;  
    dlx.insert(tot, 768 + 16 * (x - 1) + z);  
}  
  
int main()  
{  
    while (~scanf("%s", ch[1] + 1))  
    {  
        tot = 0;  
        dlx.reset(4096, 1024);  
        for (int i = 2; i <= 16; i++) scanf("%s", ch[i] + 1);  
        for (int i = 1; i <= 16; i++)  
        {  
            for (int j = 1; j <= 16; j++)  
            {  
                if (ch[i][j] == '-') a[i][j] = 0;  
                else a[i][j] = ch[i][j] - 'A' + 1;  
                if (a[i][j]) insert(i, j, a[i][j]);  
                else  
                {  
                    for (int k = 1; k <= 16; k++)  
                        insert(i, j, k);  
                }  
            }  
        }  
        if (dlx.dfs(0)) dlx.display();  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值