zoukankan      html  css  js  c++  java
  • Sudoku POJ 2676 [dfs]

    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

    给你一个9*9的方格,要求每行每列的9个数字都不相同,然后将这方格,分成9个3*3的小方格,要求每个小方格里面的数字也都不相同。现在给你一些数字,0代表你要添加的数字,使其形成一个数独。
    #include <bits/stdc++.h>
    using namespace std;
    //typedef long long ll;
    using ll = long long;
    const ll inf = 4e18+10;
    const int mod = 1000000007;
    const int mx = 200010; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, 0, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    ll  m, n,y,z,k,sum=0,ans=0,t,num;
    int vis[200][2], mp[10][10];
    bool check(int x, int y,int k) {
        re(i, 0, 9) {
            if (mp[i][y] == k)return 0;
            if (mp[x][i] == k)return 0;
        }
        int xx = (x / 3) * 3,yy=(y/3)*3;
        re(i, 0, 3)re(j, 0, 3)if (mp[i + xx][j + yy] == k)return 0;
        return 1;
    }
    int dfs(int dx) {
        if (dx < 0)return 1;
        re(i, 1, 10) {
            int x = vis[dx][0], y = vis[dx][1];
            if (check(x, y, i)) {
                mp[x][y] = i;
                if (dfs(dx - 1))return 1;
                mp[x][y] = 0;
            }
        }
        return 0;
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> t;
        while (t--)
        {
            num = 0;
            char c;
            re(i,0,9)
                re(j, 0, 9) {
                cin >> c;
                mp[i][j] = c - '0';
                if (mp[i][j] == 0) {
                    vis[num][0] = i;
                    vis[num++][1] = j;
                }
            }
            dfs(num - 1);
            re(i, 0, 9) {
                re(j, 0, 9)cout << mp[i][j]; cout << endl;
            }
        }    
        return 0;
    }
  • 相关阅读:
    贝叶斯统计
    Gamma函数
    归纳逻辑 贝叶斯主义
    向同一个模型的外键反向关联名称产生了冲突 Django迁移
    Transformation-Based Error-Driven Learning and Natural Language Processing: A Case Study in Part-of-Speech Tagging
    part of speech
    fnTBL is a customizable, portable and free source machine-learning toolkit
    Choose between the 64-bit or 32-bit version of Office
    How to Identify Gender in Datasets at Large Scales, Ethically and Responsibly | MIT Center for Civic Media
    英语单词的去重后按照长度降序、升序排列
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12717978.html
Copyright © 2011-2022 走看看