zoukankan      html  css  js  c++  java
  • Sudoku POJ

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

     
     
    数独转化为精确覆盖问题
    https://www.cnblogs.com/grenet/p/3163550.html
    这个讲的很详细
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 1000005, INF = 0x7fffffff;
    int S[maxn], head[maxn], vis[maxn];
    int U[maxn], D[maxn], L[maxn], R[maxn];
    int C[maxn], X[maxn];
    int n, m, ans, ret, ans1;
    
    void init()
    {
        for(int i = 0; i <= m; i++)
            D[i] = i, U[i] = i, R[i] = i + 1, L[i] = i - 1;
        L[0] = m, R[m] = 0;
        mem(S, 0), mem(head, -1);
        ans = m + 1;
    }
    
    void delc(int 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[C[j]]--;
    
    }
    
    void resc(int 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[C[j]]++;
        L[R[c]] = c, R[L[c]] = c;
    }
    
    void add(int r, int c)
    {
        ans++, S[c]++, C[ans] = c, X[ans] = r;
        D[ans] = D[c];
        U[ans] = c;
        U[D[c]] = ans;
        D[c] = ans;
        if(head[r] < 0) head[r] = L[ans] = R[ans] = ans;
        else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
    }
    
    
    bool dfs(int sh)
    {
        if(!R[0])
        {
            sort(vis, vis + 81);
            int cnt = 0;
            for(int i = 0; i < 9; i++)
            {
                for(int j = 0; j < 9; j++)
                {
                    int num = vis[cnt++];
    
                    num=num - (i * 9 + j) * 9;
                    printf("%d", num);
                   // cout << 111 << endl;
    
                }
                printf("
    ");
            }
    
            return true;
        }
        int c = R[0];
        for(int i = R[0]; i; i = R[i]) if(S[c] > S[i]) c = i;
        delc(c);
        for(int i = D[c]; i != c; i = D[i])
        {
            vis[sh] = X[i];
            for(int j = R[i]; j != i; j = R[j])
                delc(C[j]);
            if(dfs(sh + 1)) return true;
            for(int j = L[i]; j != i; j = L[j])
                resc(C[j]);
        }
        resc(c);
        return false;
    }
    
    
    char str[10][10];
    
    
    void build(int x, int y, int k)
    {
        ans1 = (x * 9 + y - 1) * 9 + k;
        add(ans1, x * 9 + k);
        add(ans1, 81 + (y - 1) * 9 + k);
        add(ans1, 243 + x * 9 + y);
        int block = (y - 1) / 3 * 3 + x / 3;
        add(ans1, 162 + block * 9 + k);
    
    }
    
    
    int main()
    {
    
        int T;
        rd(T);
        while(T--)
        {
            //ans1 = 0;
            m = 9 * 9 * 4;
            init();
    
            for(int i = 0; i < 9; i++)
            {
                rs(str[i]);
                for(int j = 1; j <= 9; j++)
                {
                    if(str[i][j - 1] == '0')
                        for(int k = 1; k <= 9; k++) build(i, j, k);
                    else
                        build(i, j, str[i][j - 1] - '0');
                }
            }
            dfs(0);
    
    
        }
    
    
        return 0;
    }
     
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    4270. 【NOIP2015模拟10.27】魔道研究
    4269. 【NOIP2015模拟10.27】挑竹签
    NOIP2015模拟10.28B组
    JZOI5257. 小X的佛光
    4260. 最大子矩阵 (Standard IO)
    1010. 【CQOI2009】叶子的颜色
    【NOIP2015模拟10.22】最小代价
    JZOI 距离 (Standard IO) 题解
    统计和 luogu P2068 树状数组和线段树练手
    2020.7.15模拟赛
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10621134.html
Copyright © 2011-2022 走看看