The rule of Sudoku is this: In a 9x9 box, you need to fill in the number 1-9 into a space, and make each row and column of the square contain 1-9 of these nine numbers. Also ensure that the space in the thick line divided into 9 3x3 squares also contains 1-9 of these nine numbers. For example, if you have such a problem, you can take a closer look at each line, each column, and each 3x3 square contains 1-9 of these nine numbers.
Examples:
Answer:
The input subject contains multiple sets of tests, separated by a blank line between each group. Each set of tests gives you a 9*9 matrix, separated by a space of two elements adjacent to the same line. Where 1-9 represents the already filled number of the position, the question mark (?) indicates the number you want to fill.
Output for each set of tests, print its solution, separated by a space of two numbers adjacent to the same row. There is a blank line between the two sets of solutions.
For each set of test data it is guaranteed to have only one solution.
Sample Input
7 1 2? 6? 3 5 8
? 6 5 2? 7 1? 4
? ? 8 5 1 3 6 7 2
9 2 4? 5 6? 3 7
5? 6??? 2 4 1
1? 3 7 2? 9? 5
? ? 1 9 7 5 4 8 6
6? 7 8 3? 5 1 9
8 5 9? 4?? 2 3
Sample Output
7 1 2 4 6 9 3 5 8
3 6 5 2 8 7 1 9 4
4 9 8 5 1 3 6 7 2
9 2 4 1 5 6 8 3 7
5 7 6 3 9 8 2 4 1
1 8 3 7 2 4 9 6 5
2 3 1 9 7 5 4 8 6
6 4 7 8 3 2 5 1 9
8 5 9 6 4 1 7 2 3
PS: Handle characters with caution!!! (Because this has been tle .....)
#include <cstdio> #include <cstring> #include <stack> #include <vector> #include <queue># include<cmath> #include <cstdlib> #include <iostream> #include <algorithm>using namespace std; const int OO = 1e9+7;const int maxn = 1e6+7;typedef long long ll;struct da{int x, y;} as[1212];/**< store the position of the lattice that needs to be filled */c Har str[20];int maps[20][20], OK, cnt;int judge (int num, int cur)/**< determine if num can be filled */{int i, J; for (i = 0; i < 9; i++)/**< determines whether rows and columns can fill this number */{if (maps[as[cur].x][i] = = num | | maps[i][as[cur].y] = = num) re Turn 0; int x = as[cur].x/3*3;/**< the row and column range of the 3*3 to which the current position belongs */int y = as[cur].y/3*3; for (i = 0; i < 3; i++)/**< determines whether the 3*3 can fill this number */for (j = 0; J < 3; J + +) if (maps[i+x][j+y] = num) r Eturn 0; return 1;} void Dfs (int x) {int i, J; if (x = = CNT)///All squares are filled out {ok = 1; for (i = 0; i < 9; i++) {for (j = 0; J < 8; j + +) printf ("%d",MAPS[I][J]); printf ("%d\n", maps[i][8]); } return; } for (i = 1; I <= 9; i++) {if (Judge (I, X) &&!ok)/**<!ok condition judgment can save several times (only one fill is allowed) */ {MAPS[AS[X].X][AS[X].Y] = i; DFS (X+1); MAPS[AS[X].X][AS[X].Y] = 0; }}//return;} int main () {int I, J, Cas=0, K; while (scanf ("%s", str)!=eof)///data read-in is disgusting .... {OK = cnt = k = 0; memset (maps, 0, sizeof); if (str[0] = = '? ') {as[cnt].x = 0; AS[CNT].Y = 0; cnt++; } if (str[0] >= ' 0 ' && str[0] <= ' 9 ') maps[0][0] = str[0]-' 0 '; for (i = 0; i < 9; i++) {for (j = 0; J < 9; J + +) {if (i = = 0 && J = = 0) continue; scanf ("%s", str); if (str[0] = = '? ') {as[cnt].x = i; As[cnt].y = j; cnt++; } else Maps[i][j] = str[0]-' 0 '; }} if (cas++) printf ("\ n"); DFS (0); } return 0;}
)
Sudoku Killer (hdu 1426 Sudoku)