| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 19635 | Accepted: 9407 | Special Judge | ||
Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a PRogram to solve a given Sudoku-task.
Input
The input data will start with the number of 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 is given, corresponding to the cells in this line. If a cell is empty it is 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 have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.Sample Input
1103000509002109400000704000300502006060000050700803004000401000009205800804000107Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127就是數(shù)獨(dú);爆搜。。。
關(guān)鍵是懂得搜索姿勢(shì)還有標(biāo)記方法。。。 book_row book_col記錄的是第幾行的哪個(gè)數(shù)有沒(méi)有用過(guò)。。 Book[][][]前兩維記錄的是分塊,后面記錄的是數(shù)字,初始化:if(val) book_row[i][val] = book_col[j][val] = book[i/3][j/3][val] = 1;這樣就必須保證i從0開(kāi)始,0/3,1/3,2/3 = 0,但是如果
從1開(kāi)始 1/3,2/3 = 0.3/3 = 1,就達(dá)不到標(biāo)記的情況了。。。另外像這種二位搜索,一般都是這樣。。。
if(flag)return ;
if(y == 9) x++, y = 0;
if(x == 9) { flag = 1;return ; }
if(maze[x][y]) dfs(x, y+1);
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[15][15];int maze[15][15], book_row[10][10], book_col[10][10], book[5][5][10];int flag;void dfs(int x, int y){ if(flag) return ; if(y == 9) x++, y = 0; if(x == 9) { flag = 1; return ; } if(maze[x][y]) dfs(x, y+1); else { for(int i = 1; i <= 9; i++) { if(!book_row[x][i] && !book_col[y][i] && !book[x/3][y/3][i]) { maze[x][y] = i; book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 1; dfs(x, y+1); if(flag) return; maze[x][y] = book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 0; } } }}int main(void){ //freopen("in.txt", "r", stdin); int t; cin >> t; while(t--) { flag = 0; memset(book_row, 0, sizeof(book_row)); memset(book_col, 0, sizeof(book_col)); memset(book, 0, sizeof(book)); for(int i = 0; i < 9; i++) { scanf("%s", str[i]); for(int j = 0; j < 9; j++) { int val = str[i][j]-'0'; maze[i][j] = val; if(val) book_row[i][val] = book_col[j][val] = book[i/3][j/3][val] = 1; } } dfs(0, 0); for(int i = 0; i < 9; i++) for(int j = 0; j < 9; j++) { printf("%d", maze[i][j]); if(j == 8) printf("/n"); } } return 0;}我一開(kāi)始姿勢(shì)不對(duì)。。。 這樣寫(xiě)的,仔細(xì)想想,這樣不能回溯所有的。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int book_row[10][10], book_col[10][10], book[10][10][10], maze[10][10], flag;char str[15][15];void dfs(int x){ if(flag) return; if(x == 10) { for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { printf("%d", maze[i][j]); } printf("/n"); } flag = 1; } for(int y = 1; y <= 9; y++) for(int i = 1; i <= 9; i++) { if(!book_row[x][i] && !book_col[y][i] && !book[x/3][y/3][i]) { book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 1; maze[x][y] = i; dfs(x+1); book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 0; } }}int main(){ int t; scanf("%d", &t); while(t--) { memset(book_row, 0, sizeof(book_row)); memset(book_col, 0, sizeof(book_col)); memset(book, 0, sizeof(book)); flag = 0; for(int i = 1; i <= 9; i++) { scanf("%s", str[i]+1); for(int j = 1; j <= 9; j++) maze[i][j] = str[i][j] - '0'; } dfs(1); } return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注