Leetcode Sudoku Solver

Source: Internet
Author: User
Tags bool int size

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells is indicated by the character '.

Assume that there would be is only one unique solution.


A Sudoku Puzzle ...


... and its solution numbers marked in red.

Method One: Simple search (Incredibly, I was drunk too)

32ms

 Solution {public:struct Point {int x, y;
    Point (int _x,int _y): X (_x), Y (_y) {}-point () {}};
    Vector<point> v;
    BOOL H[10][10];
    BOOL C[10][10];
    BOOL GE[10][10];
        BOOL Dfs (int cur,vector<vector<char> > &AMP;MP) {if (cur >= v.size ()) return true;
        Point P = v[cur];
            for (int i = 1;i <= 9; i++) {if (!h[p.x][i] &&!c[p.y][i] &&!ge[p.x/3*3+p.y/3][i])
                {H[p.x][i] = c[p.y][i] = Ge[p.x/3*3+p.y/3][i] = 1;
                MP[P.X][P.Y] = char (i + ' 0 ');
                if (Dfs (CUR+1,MP)) return true;
            H[p.x][i] = C[p.y][i] = Ge[p.x/3*3+p.y/3][i] = 0;
    }} return false;
        } void Solvesudoku (Vector<vector<char> > &board) {memset (h,0,sizeof (h));
        Memset (C,0,sizeof (c));
        memset (Ge,0,sizeof (GE));
for (int i = 0;i < 9;i++) for (int j = 0;j < 9; j + +)        {if (board[i][j] = = '. ') V.push_back (Point (I,j));
                else {int x = board[i][j]-' 0 ';
                H[I][X] = 1;
                C[J][X] = 1;
            GE[I/3*3+J/3][X] = 1;
    }} dfs (0,board); }
};

Method Two: DLX accurate coverage (estimated only acmer will be used)

4ms

#define MAXM #define MAXL 324 #define MAXN 10000 #define INF 0x7FFFFFFF Char SD[MAXM][MAXM];
int L[MAXN], R[MAXN], U[MAXN], D[MAXN], H[MAXN];
int size, C[MAXN], S[MAXN], X[MAXN], Q[MAXN], VIS[MAXL * 3];
    void Init () {int i;
        for (i = 0; I <= MAXL; i++) {l[i + 1] = i;
        R[i] = i + 1;
        U[i] = d[i] = i;
    S[i] = 0;
	} R[maxl] = 0;
    memset (h,-1,sizeof (H));
Size = MAXL + 1;
    } void Remove (int c) {int i, J;
    L[R[C]] = l[c];
    R[L[C]] = r[c];
            for (i = d[c], i = c; i = D[i]) {for (j = r[i]; j = i; j = R[j]) {U[d[j]] = u[j];
            D[U[J]] = d[j];
        s[c[j]]--;
    }}} void Resume (int c) {int i, J;
    L[R[C]] = c;
    R[L[C]] = c;
            for (i = d[c], i = c; i = D[i]) {for (j = r[i]; j = i; j = R[j]) {U[d[j]] = j;
            D[U[J]] = j;
        s[c[j]]++; }}} inline void Link (int r, int c) {D[size] = D[c];
    U[size] = c;
    U[D[C]] = size;
    D[C] = size;
    if (H[r] < 0) H[r] = l[size] = r[size] = size;
        else {l[size] = H[r];
        R[size] = R[h[r]];
        L[R[H[R]] = size;
    R[H[R]] = size;
    } s[c]++;
    X[size] = r;
c[size++] = c;
    } bool Dance (int now) {int I, j, c, temp;
    if (r[0] = = 0) return true;
            for (temp = Inf,i = r[0]; i; i = R[i]) {if (S[i] < temp) {temp = s[i];
        c = i;
    }} Remove (c);
        for (i = d[c]; i = c; i = D[i]) {Vis[x[i]] = true;
        for (j = r[i]; J! = i; j = r[j]) Remove (C[j]);
        if (Dance (now + 1)) return true;
        for (j = l[i]; J! = i; j = l[j]) Resume (C[j]);
    Vis[x[i]] = false;
    } Resume (c);
return false;
       } class Solution {Public:void Solvesudoku (vector<vector<char> > &board) {Init (); for (int i = 1;i <= 9;
       i++) {for (int j = 1;j <= 9; j + +) Sd[i][j] = board[i-1][j-1];
         } int I, j, K, R; for (r = 0, i = 1; i < MAXM; i++) {for (j = 1; j < Maxm; J + +) {if
                (Sd[i][j] = = '. ')
						{for (k = 1; k < MAXM; k++) {//H[++r] =-1;
                        r++;
                        Q[r] = k;
                        Link (R, (i-1) * 9 + k);
                        Link (R, Bayi + (j-1) * 9 + k);
                        Link (R, 162 + ((i-1)/3 * 3 + (J-1)/3) * 9 + k);
                    Link (R, 243 + (i-1) * 9 + j);
					}} else {//h[++r] =-1;
                    r++;
                    K = sd[i][j]-' 0 ';
                    Q[r] = k;
                    Link (R, (i-1) * 9 + k);
                    Link (R, Bayi + (j-1) * 9 + k); Link (R, 162 + ((i-1)/3 * 3 + (J-1)/3) * 9 + k);
                Link (R, 243 + (i-1) * 9 + j);
        }}} memset (Vis, false, sizeof (VIS));
        Dance (0);
        int p = 0;
                for (i=1;i<=r;i++) {if (Vis[i]) {board[p/9][p%9] = char (q[i] + ' 0 ');
            p++; }
        }
    }
};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.