1162. Sudoku Constraints
Time limit:1 secs, Memory limit:32 MB, Special Judge
Description
Sudoku is a very simple task. A Square Table with 9 rows and 9 columns are divided to 9 smaller squares 3x3 as shown on the figure. In some of the cells is written decimal digits from 1 to 9. The other cells is empty. The goal is to fill the empty cells with a decimal digits from 1 to 9, one digit per cell, and in such. Each column and in each marked a 3x3 subsquare, all of the digits from 1 through 9 to appear. Write a program to solve a given sudoku-task.
Input
The input data would start with the number of the the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits are given, corresponding to the cells in this line. If a cell is empty it's represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells has the filled according to the rules. If Solutions is isn't unique, then the program could print any one of the them.
Sample Input
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
#include <iostream> #include <vector> #include <string.h> #include <cstring> #include < Stdio.h> #include <algorithm>using namespace std;//sudoku, deep Search, which is pruning char ans[10][10];//used to store the final answer bool Num_in_row[ 10][10], num_in_col[10][10], num_in_blo[10][10];//here the array [i][j] means that the number of J is already present in row I/column/block (sometimes true) bool is_ok;// Did you find the answer to the int blank_num;//blank number struct blank {int Pos_row, pos_col, Pos_blo, possibility;//where possibility is the number of possible numbers}bla Nk[85];int find_block (int x, int y) {//returns the block number that belongs to int block[10][10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 4, 4, 4, 5, 5, 5, 6, 6 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9}; return block[x][y];} void Dfs (int blank_now) {//blank_now refers to the empty numbered if (Is_ok = = True) that is currently being filled in//returns if the answer is found; if (bLank_now = = Blank_num) {//If the Blank_now exceeds the total number of blanks, that is, the blank is filled out then return IS_OK = true; Return } for (int possible = 1; possible <= 9; possible++) {//There are 9 possible if on an empty (!num_in_row[blank[blank_now ].pos_row][possible] &&!num_in_col[blank[blank_now].pos_col][possible] &&!num_in_blo[blank[blank _now].pos_blo][possible]) {Ans[blank[blank_now].pos_row][blank[blank_now].pos_col] = possible + ' 0 ';//First fill in the answer, even if not, later fill can also overwrite num_in_row[blank[blank_now].pos_row][possible] = true;//and update the blank limit information Num_in_col[blank[blank_now].pos_col][possible] = true; Num_in_blo[blank[blank_now].pos_blo][possible] = true; DFS (Blank_now + 1);//Deep Search if (IS_OK)//Find the answer and return to the finished return; Num_in_row[blank[blank_now].pos_row][possible] = false;//program Run to this note the previous assumptions did not find the answer, so restore this blank limit information Num_in_col[blank[blan K_now].pos_col][posSible] = false; Num_in_blo[blank[blank_now].pos_blo][possible] = false; }}}void set_blank (int k, int i, int j) {blank[k].possibility = 0; Blank[k].pos_row = i; Blank[k].pos_col = j; Blank[k].pos_blo = Find_block (i, j);} void calculate (int k) {//Here is the number of possible counts for (int temp = 1; temp <= 9; temp++) {if (!num_in_row[blank[k].pos_ro W][temp] &&!num_in_col[blank[k].pos_col][temp] &&!num_in_blo[blank[k].pos_blo][temp]) {blank [k].possibility++; }}}bool CMP (const blank &a, const blank &b) {//Sort return a.possibility < b.possibility in order from small to large;} int main () {int case_num, I, J; scanf ("%d", &case_num); while (case_num--) {blank_num = 0; IS_OK = false; Memset (Num_in_row, False, sizeof (Num_in_row)); Memset (Num_in_col, False, sizeof (NUM_IN_COL)); Memset (Num_in_blo, False, sizeof (Num_in_blo)); for (i = 1; I <= 9; i++) { scanf ("%s", Ans[i] + 1); } for (i = 1; I <= 9, i++) {for (j = 1; J <= 9; j + +) {if (ans[i][j]! = ' 0 ') {//not blank on update restriction information NUM_IN_ROW[I][ANS[I][J]-' 0 '] = true;//Update restriction information NUM_IN_COL[J][ANS[I][J]-' 0 '] = true; Num_in_blo[find_block (i, J)][ans[i][j]-' 0 '] = true; } else {Set_blank (Blank_num, I, J); blank_num++; }}} for (i = 0; i < Blank_num; i++) {//Calculate possibility calculate (i); } sort (blank, blank + blank_num, CMP);//Sort, i.e. pruning dfs (0); for (i = 1; I <= 9; i++) {for (j = 1; J <= 9; j + +) {printf ("%c", Ans[i][j]); } printf ("\ n"); }} return 0;}
Sicily 1162. Sudoku