Did a long time or refer to other people's answer orz, in fact, it is not difficult ah. I'm going to start learning how to write a search ...
Title Link: poj2676 Sudoku
Puzzle: Brute force search, DFS each blank grid number.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <vector>6 using namespacestd;7 BOOLrow_f[9][Ten];//Row_f[i][j]=1 says the number J has been placed on line I8 BOOLcol_f[9][Ten];9 BOOLblock_f[9][Ten];Ten intg[9][9]; One structblank{ A intI, J; -BlankintIintj): I (i), J (j) {} - }; theVector<blank>BK; - intGet_blockid (intIintj) { -I/=3; -J/=3; + returnI3+J; - } + BOOLOkintIintJintx) { A return!ROW_F[I][X] &&!col_f[j][x] &&!Block_f[get_blockid (i, J)][x]; at } - voidSetnum (intIintJintXintf) { -ROW_F[I][X] =F; -COL_F[J][X] =F; -Block_f[get_blockid (i, j)][x] =F; - } in BOOLDfsintN) {//Handle the first n blank spaces - if(N <0)return true; to intR =bk[n].i; + intc =BK[N].J; - for(intx =1; X <=9; ++x) { the if(OK (r, c, x)) { *Setnum (R, c, X,1); $G[R][C] =x;Panax Notoginseng if(Dfs (N-1))return true; -Setnum (R, c, X,0); the } + } A return false; the } + intMain () { - intT, I, J; $ CharC; $scanf"%d", &t); - while(t--){ - bk.clear (); thememset (Row_f,0,sizeof(Row_f)); -memset (Col_f,0,sizeof(Col_f));Wuyimemset (Block_f,0,sizeof(Block_f)); the for(i =0; I <9; ++i) { - for(j =0; J <9; ++j) { WuCIN >>C; -G[I][J] = C-'0'; About if(!G[i][j]) $ Bk.push_back (Blank (i, j)); - Else -Setnum (i, J, G[i][j],1); - } A } + if(Dfs (Bk.size ()-1)){ the for(i =0; I <9; ++i) { - for(j =0; J <9; ++j) $printf"%c", G[i][j] +'0'); theprintf"\ n"); the } the } the } - return 0; in}
View Code
poj2676 Sudoku (DFS)