leetcode Sudoku Solver

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

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.


A sudoku puzzle...


...and its solution numbers marked in red.

方法一:简单搜索(居然能过,我也是醉了)

32ms

 Solution {
public:
    struct Point
    {
      int x,y;
      Point(int _x,int _y):x(_x),y(_y){}
      Point(){}
    };
    vector v;
    bool h[10][10];
    bool c[10][10];
    bool ge[10][10];
    bool dfs(int cur,vector > &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 > &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);
    }
};

方法二:DLX精确覆盖(估计只有acmer才会用)

4ms

#define MAXM 10
#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 > &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, 81 + (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, 81 + (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++;
            }
        }
    }
};



你可能感兴趣的:(leetcode,DLX)