Sudoku
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 8575 |
|
Accepted: 3074 |
Description
The game of Sudoku, you is given a large 9x9 grid divided into smaller 3x3 subgrids. For example,
| . |
2 |
7 |
3 |
8 |
. |
. |
1 |
. |
| . |
1 |
. |
. |
. |
6 |
7 |
3 |
5 |
| . |
. |
. |
. |
. |
. |
. |
2 |
9 |
| 3 |
. |
5 |
6 |
9 |
2 |
. |
8 |
. |
| . |
. |
. |
. |
. |
. |
. |
. |
. |
| . |
6 |
. |
1 |
7 |
4 |
5 |
. |
3 |
| 6 |
4 |
. |
. |
. |
. |
. |
. |
. |
| 9 |
5 |
1 |
8 |
. |
. |
. |
7 |
. |
| . |
8 |
. |
. |
6 |
5 |
3 |
4 |
. |
Given Some of the numbers in the grid, your goal are to determine the remaining numbers such that the numbers 1 through 9 a Ppear exactly once in (1) Each of the nine 3x3 Subgrids, (2) Each of the nine rows, and (3) each of the nine columns.
Input
The input test file would contain multiple cases. Each test case consists of a single line containing Bayi characters, which represent the bay squares of the Sudoku grid, give n one row at a time. Each character are either a digit (from 1 to 9) or a period (used to indicate an unfilled square). Assume that all puzzle in the input would have exactly one solution. The end-of-file is denoted by a, containing the word "end".
Output
For each test case, print a line representing the completed Sudoku puzzle.
Sample Input
.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.......52..8.4......3...9...5.1...6..2..7 ........ 3.....6...1..........7.4.......3.end
Sample Output
5273894168194267354367518293756921841945382672681745936432179589518436727829653414168375299824653717351294685712986432937 46185864351297647913852359682714128574936
Source
Stanford Local 2006
Turn the Sudoku problem into a precise cover, the lesson set to do this is to learn the next DLX
/************************************************************************* > File Name:sudoku.cpp > Author:a Lex > Mail: [email protected] > Created time:2015 January 06 Tuesday 14:46 52 Seconds ******************************** /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int n = 350;const int m = 800; const int inf = 0x3f3f3f3f;const int N = $ * 1000;bool mat[1000][1000];char su[100];int cnt[m];int most;bool ans[N];int Head;struct node{int up, down, left, Right;int col, Row;} Node[n];void init (int m) {memset (ans, 0, sizeof (ans)); for (int i = 0; I <= m; ++i) {node[i].left = I-1;node[i].right = i + 1;node[i].col = i;node[i].up = I;node[i].down = I;cnt[i] = 0;} Node[0].left = M;node[m].right = 0;} VoID remove (int c) {node[node[c].left].right = Node[c].right;node[node[c].right].left = node[c].left;for (int i = node[c]. Down I! = C; i = Node[i].down) {for (int j = node[i].right; J! = i; j = node[j].right) {--cnt[node[j].col];node[node[j].up].down = Node[j ].down;node[node[j].down].up = Node[j].up;}}} void Resume (int c) {node[node[c].left].right = C;node[node[c].right].left = c;for (int i = node[c].up; I! = c; i = Node[i]. UP) {for (int j = Node[i].left; J! = i; j = node[j].left) {++cnt[node[j].col];node[node[j].down].up = J;node[node[j].up].dow n = j;}}} BOOL Dacing_link_x () {if (node[324].right = = 324) {return true;} int mins = inf;int c;for (int i = node[324].right; I! = 324; i = node[i].right) {if (Cnt[i] < mins) {mins = Cnt[i];c = i; }}remove (c); for (int i = Node[c].down; I! = c; i = Node[i].down) {for (int j = node[i].right; J! = i; j = node[j].right) {RE Move (Node[j].col);} Ans[node[i].row] = 1;if (dacing_link_x ()) {return true;} for (int j = Node[i].left; J! = i; j = node[j].left) {Resume (Node[j]. Col);} Ans[node[i].row] = 0;} Resume (c); return false;} int In_grid (int i, int j) {++i;++j;if (1 <= i && i <= 3 && 1 <= J && J <= 3) {return 1;} if (1 <= i && i <= 3 && 4 <= J && J <= 6) {return 2;} if (1 <= i && i <= 3 && 7 <= J && J <= 9) {return 3;} if (4 <= i && i <= 6 && 1 <= J && J <= 3) {return 4;} if (4 <= i && i <= 6 && 4 <=j && J <= 6) {return 5;} if (4 <= i && i <= 6 && 7 <= J && J <= 9) {return 6;} if (7 <= i && i <= 9 && 1 <= J && J <= 3) {return 7;} if (7 <= i && I <= 9 && 4 <= J && J <= 6) {return 8;} if (7 <= i && i <= 9 && 7 <= J && J <= 9) {return 9;}} void Add (int i, int j, int k) {int x = (I * 9 + j) * 9 + k;mat[x][i * 9 + j] = 1;mat[x][81 + I * 9 + K] = 1;mat[x][162 + j * 9 + K] = 1;int g = In_grid (i, j); mat[x][243 + (g-1) * 9 + K] = 1;} int main () {//printf ("Please enter your sudoku\n"),//while (~scanf ("%s", su)) {if (!strcmp (su, "End")) {break;} most = 1000;memset (Mat, 0, sizeof (MAT)), init (324), int cur = 325;for (int i = 0; i < 9; ++i) {for (int j = 0; j < 9; ++J) {if (Su[i * 9 + j] = = '. ') {for (int k = 0; k < 9; ++k) {Add (I, j, k);}} Else{int k = su[i * 9 + j]-' 1 '; Add (i, j, k);}} for (int i = 0; i < 729; ++i) {int s = cur;int pre = cur;for (int j = 0; j < 324; ++j) {if (!mat[i][j]) {continue;} int pos = J;node[cur].up = Node[pos].up;node[node[pos].up].down = Cur;node[cur].down = Pos;node[pos].up = CUR;NODE[CUR].R ow = I;node[cur].col = Pos;node[cur].left = Pre;node[pre].right = Cur;node[cur].right = S;cnt[pos]++;node[s].left = CUR;PR e = cur;cur++;}} Dacing_link_x (); for (int i = 0; i < Bayi; ++i) {for (int j = 0; J < 9; ++j) {if (Ans[i * 9 + j]) {printf ("%d", J + 1); Bre ak;}}} printf ("\ n");} return 0;}
POJ3074----Sudoku