HDU 1426 Sudoku Killer DFS Simple questions

Source: Internet
Author: User

Give a part of the Sudoku and then ask us to complete the Sudoku.

InputThe subject contains multiple sets of tests, separated by a blank line between each group. Each set of tests gives you a 9*9 matrix, separated by a space of two elements adjacent to the same line. Where 1-9 represents the already filled number of the position, the question mark (?) indicates the number you want to fill.  

Outputfor each set of tests, output its solution, and the two numbers adjacent to the same row are separated by a single space. There is a blank line between the two sets of solutions.
for each set of test data it is guaranteed to have only one solution.  

Sample Input7 1 2? 6? 3 5 8? 6 5 2? 7 1? 4?? 8 5 1 3 6 7 29 2 4? 5 6? 3 75? 6??? 2 4 11? 3 7 2? 9? 5?? 1 9 7 5 4 8 66? 7 8 3? 5 1 98 5 9? 4?? 2 3 

Sample Output7 1 2 4 6 9 3 5 83 6 5 2 8 7 1 9 44 9 8 5 1 3 6 7 29 2 4 1 5 6 8 3 75 7 6 3 9 8 2 4 11 8 3 7 2 4 9 6 52 3 1 9 7 5 4 8 66 4 7 8 3 2 5 1 98 5 9 6 4 1 7 2 3This problem is mainly input very annoying. idea: Directly for each empty, enumerate every possible solution, and then DFS, keep filling it out, if you can fill it out, then output, and Mark Over=truepay attention to backtracking. DFS (int cnt) has already filled in the number of cnt-1, to fill the number of CNT.

1#include <cstdio>2#include <cstring>3 intmaze[ A][ A];4 inttot;5 BOOLOver ;6 struct Point7 {8     intx, y;9}point[ -];Ten intChangeintx) One { A     if(1<=x&&x<=3) -         return 1; -     if(4<=x&&x<=6) the         return 4; -     if(7<=x&&x<=9) -         return 7; - } + BOOLJudgeintCntintt) - { +      for(intI=1; i<=9; i++) A     { at         if(maze[point[cnt].x][i]==t| | maze[i][point[cnt].y]==t) -             return false; -     } -     intu=Change (point[cnt].x); -     intv=Change (POINT[CNT].Y); -      for(intjj=u;jj<=u+2; jj++) in     { -          for(intjjj=v;jjj<=v+2; jjj++) to             if(maze[jj][jjj]==t) +                 return false; -     } the     return true; * } $ voidDfsintCnt//Have been finish the number of CNTPanax Notoginseng { -     if(cnt==tot) the     { +          for(intI=1; i<=9; i++) A         { the              for(intj=1;j<9; j + +) +printf"%d", Maze[i][j]); -printf"%d\n", maze[i][9]); $         } $Over=true; -         return ; -     } the      for(intI=1; i<=9; i++) -     {Wuyi         if(Judge (Cnt,i) &&!Over ) the         { -maze[point[cnt].x][point[cnt].y]=i; WuDFS (cnt+1); -maze[point[cnt].x][point[cnt].y]=0; About         } $     } - } - intMain () - { A     Chars[3]; +     intp=0, i=1, j=1; thetot=1; -      while(SCANF ("%s", &s)! =EOF) $     { the         if(s[0]!='?') the         { themaze[i][j]=s[0]-'0'; the         } -         Else in         { themaze[i][j]=0; thepoint[tot].x=i; Aboutpoint[tot++].y=J; the         } theJ + +; the         if(j>9) +         { -i++; thej=1;Bayi         } the         if(i==Ten) the         { -             if(P) -printf"\ n"); thep++; theOver=false; theDfs1); thei=j=1; -tot=1; the         } the     } the     return 0;94}
My Code

HDU 1426 Sudoku Killer DFS Simple questions

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.