poj2676 Sudoku (DFS)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.