码迷,mamicode.com
首页 > 其他好文 > 详细

Sudoku Solver

时间:2014-09-02 22:46:45      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   ar   for   div   

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.

bubuko.com,布布扣

A sudoku puzzle...

bubuko.com,布布扣

...and its solution numbers marked in red.

思路:使用DFS求解。循环遍历整个数独矩阵,确定能够唯一确定的格;然后取可能元素最少的格依次试探其所有的可能值。

 1 class Solution {
 2 public:
 3     typedef vector<short> NumMark;
 4     void solveSudoku( vector<vector<char> > &board ) {
 5         int width = board.size();
 6         NumMark rowMark( width, 0 ), colMark( width, 0 ), squareMark( width, 0 );
 7         for( int i = 0; i < width; ++i ) {
 8             for( int j = 0; j < width; ++j ) {
 9                 if( board[i][j] != . ) {
10                     rowMark[i] |= ( 1 << (board[i][j]-1) );
11                     colMark[j] |= ( 1 << (board[i][j]-1) );
12                     squareMark[i/3*3+j/3] |= ( 1 << (board[i][j]-1) );
13                 }
14             }
15         }
16         solve( rowMark, colMark, squareMark, board );
17         board = this->board;
18         return;
19     }
20 private:
21     bool solve( NumMark rowMark, NumMark colMark, NumMark squareMark, vector<vector<char>> board ) {
22         int width = board.size(), ii = 0, jj = 0;
23         short mark = 0, cnt = 10;
24         bool flag = true;
25         while( flag ) {
26             mark = 0, cnt = 10, flag = false;
27             for( int i = 0; i < width; ++i ) {
28                 for( int j = 0; j < width; ++j ) {
29                     if( board[i][j] != . ) { continue; }
30                     short curMark = 0, curCnt = 0, num = -1;
31                     for( int k = 0; k < 9; ++k ) {
32                         if( ( rowMark[i] & (1 << k) ) || ( colMark[j] & (1 << k) ) || ( squareMark[i/3*3+j/3] & (1 << k) ) ) { continue; }
33                         ++curCnt;
34                         curMark |= (1 << k);
35                         num = k;
36                     }
37                     if( curCnt == 0 ) {
38                         return false;
39                     } else if( curCnt == 1 ) {
40                         board[i][j] = num+1;
41                         rowMark[i] |= (1 << num);
42                         colMark[j] |= (1 << num);
43                         squareMark[i/3*3+j/3] |= (1 << num);
44                         flag = true;
45                     } else {
46                         if( cnt > curCnt ) {
47                             mark = curMark;
48                             cnt = curCnt;
49                             ii = i; jj = j;
50                         }
51                     }
52                 }
53             }
54         }
55         if( mark ) {
56             for( int k = 0; k < 9; ++k ) {
57                 if( mark & (1 << k) ) {
58                     board[ii][jj] = k+1;
59                     rowMark[ii] |= (1 << k);
60                     colMark[jj] |= (1 << k);
61                     squareMark[ii/3*3+jj/3] |= (1 << k);
62                     if( solve( rowMark, colMark, squareMark, board ) ) { return true; }
63                     board[ii][jj] = .;
64                     rowMark[ii] &= ~(1 << k);
65                     colMark[jj] &= ~(1 << k);
66                     squareMark[ii/3*3+jj/3] &= ~(1 << k);
67                 }
68             }
69             return false;
70         }
71         this->board = board;
72         return true;
73     }
74     vector<vector<char>> board;
75 };

 

Sudoku Solver

标签:style   blog   http   color   io   使用   ar   for   div   

原文地址:http://www.cnblogs.com/moderate-fish/p/3951682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!