# Include <iostream> # include <cstring> # include <string> # include <cstdio> # include <algorithm> # include <vector> # include <algorithm> using namespace std; // column: (row + column + block) * 9 possible + 9*9 grids // row: 9*9*9 indicates that k const int MAXN = (9 + 9 + 9) is entered in column j of row I) * 9 + 9*9*9*9*9*9*9*4 + 10; # define INF 0 xFFFFFF int size; int head, sz; int U [MAXN], D [MAXN], L [MAXN], R [MAXN]; int H [MAXN], ROW [MAXN], C [MAXN], S [MAXN], O [MAXN]; void remove (int c) {L [R [c] = L [C]; R [L [c] = R [c]; for (int I = D [c]; I! = C; I = D [I]) {for (int 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) {for (int I = U [c]; I! = C; I = U [I]) {for (int j = L [I]; j! = I; j = L [j]) {++ S [C [j]; U [D [j] = j; D [U [j] = j ;}l [R [c] = c; R [L [c] = c ;} bool dfs (int k) {if (R [head] = head) {sort (O, O + 9*9); int p = 0; for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {int num = O [p ++]; // cout <num <endl; num = num-(I * 9 + j) * 9; printf ("% d", num );}} printf ("\ n"); return true;} int s = INF, c; for (int t = R [head]; t! = Head; t = R [t]) {if (S [t] <s) {s = S [t]; c = t ;}} remove (c ); for (int I = D [c]; I! = C; I = D [I]) {O [k] = ROW [I]; for (int j = R [I]; j! = I; j = R [j]) remove (C [j]); if (dfs (k + 1) return true; for (int j = L [I]; j! = I; j = L [j]) resume (C [j]);} resume (c); return false;} void initDL (int n) {head = 0; for (int I = 0; I <= n; I ++) {U [I] = I; D [I] = I; L [I] = I-1; R [I] = I + 1; S [I] = 0;} R [n] = 0; L [0] = n; S [0] = INF + 1; sz = n + 1; memset (H, 0, sizeof (H);} void insert (int I, int j) {if (H [I]) {L [sz] = L [H [I]; R [sz] = H [I]; L [R [sz] = sz; R [L [sz] = sz;} else {L [sz] = sz; R [sz] = sz; H [I] = sz ;} U [sz] = U [j]; D [sz] = j; U [D [sz] = sz; D [U [sz] = sz; C [sz] = j; ROW [Sz] = I; ++ S [j]; ++ sz;} char str [200]; void build () {int p = 0; initDL (9*9*4); for (int I = 0; I <9; I ++) for (int j = 1; j <= 9; j ++, p ++) {int base = (I * 9 + J-1) * 9; if (str [p] = '. ') {for (int k = 1; k <= 9; k ++) {int r; r = base + k; // row I has a number k insert (r, I * 9 + k); // column j has a number k insert (r, 9*9 + (J-1) * 9 + k); // The k block has a number k int block = (J-1)/3*3 + I/3; insert (r, 9*9*2 + block * 9 + k); // There is a number in column j of row I (only one number is allowed for a grid) insert (r, 9*9*3 + I * 9 + j);} else {int k = st R [p]-'0'; int r = base + k; // row I has a number k insert (r, I * 9 + k ); // column j has the number k insert (r, 9*9 + (J-1) * 9 + k); // The k block has the number k int block = (J-1) /3*3 + I/3; insert (r, 9*9*2 + block * 9 + k ); // column j of row I has a number (only one number is allowed for a grid) insert (r, 9*9*3 + I * 9 + j );}}} int main () {size = 9; // 9*9 Sudoku while (~ Scanf ("% s", str) {if (strcmp (str, "end") = 0) break; build (); dfs (0 );} return 0 ;}