Topic Link http://acm.hdu.edu.cn/showproblem.php?pid=5547
Sudoku to ensure that each row has 1,2,3,4
and 4 small squares of 2 * 2 must be 1,2,3,4.
Input:
3****234141233214*243*312*421*134*41***3*2*414*2*
Output:
Case #1:
1432234141233214Case #2:1243431234212134Case #3:3412123423414123
#include <stdio.h>#include<string.h>using namespacestd; Chara[ -][ -]; intPanduan (intRowintCol) { for(intI=0;i<4; i++) { if(a[row][i]==a[row][col]&&i!=Col)return 0; } ///determine if the number can exist in this line for(intI=0;i<4; i++) { if(a[i][col]==a[row][col]&&i!=row)return 0; } ///determine if the number can exist in this column intmrow=Row; intMcol=Col; if(row%2==1) row-=1; if(col%2==1) col-=1; for(inti=row;i<=row+1; i++) { for(intj=col;j<=col+1; j + +) { { if(A[i][j]==a[mrow][mcol]&&i!=mrow&&j!=mcol)return 0; } } } ///Judging 2*2 's small box is not 1,2,3,4 return 1; } voidDfsintcur) { if(cur==4*4)///if the last point is reached, the output is all { for(intI=0;i<4; i++) { for(intj=0;j<4; j + +) {printf ("%c", A[i][j]); } printf ("\ n"); } return ; } introw=cur/4;///Line intcol=cur%4;///column if(a[row][col]=='*') { for(intj=1; j<=4; j + +)///1,2,3,4, try every number.{A[row][col]=j+'0'; if(Panduan (row,col) = =1)///meet the criteria and go to the next point{DFS (cur+1); } A[row][col]='*';///Cancellation Token } } Else{DFS (cur+1); } } intMain () {intKase=0; intT; scanf ("%d",&t); while(t--) { for(intI=0;i<4; i++) {scanf ("%s", A[i]); } printf ("Case #%d:\n",++Kase); DFS (0); } return 0; }
Search----HDU 5547