Test instructions
Give you a 9*9 matrix that separates the two elements adjacent to the same row with a single space. Where 1-9 represents the already filled number of the position, the question mark (?) indicates the number you want to fill. The solution of this Sudoku is output and there is only one solution for each group.
Ideas:
Record the vacant place, each vacant place has 9 in the state, DFS + pruning processing other, with scanf input, get () tle to die ....
Code:
#include <cstdio>#include<iostream>#include<cstring>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespacestd;Const intMAXN = One;intGRA[MAXN][MAXN];//large Pattern with 9 * 9 Storageintans[ the];//a mapping of vacant locations to be filled (i-1) * 9 + Jintrowline[ One][ One];//rowline[z = (i-1)/3 * 3 + (J-1)/3][k] for Z (0 1 2 3 4 5 6 7 8) Whether K-numbers appear in Squares (0/1)intline[ One][ One];//Line[i][k] Indicates whether the number of the first row K has occurredintrow[ One][ One];//Row[j][k] Indicates whether the number of K in column J has occurredintN//the total number of squares to fillintGetX (intK//counter-Eject coordinates X according to the number of k in the array that holds the vacant position{ return(Ans[k]-1) /9+1;}intGetY (intK//counter-Eject coordinates Y according to the number of k in the array that holds the vacant position{ returnANS[K]%9==0?9: ans[k]%9;}voidDealintIintJintKintNum//when k (num = 1)/remove K (num =-1) is placed in the Gra (i, j) position, the array to be used for marking duplicates is maintained{line[i][K]+ = num;//Maintaining row Arraysrow[j][K] + = num;//Maintaining column Arraysrowline[(I-1) /3*3+ (J-1) /3][K] + = num;//maintaining an array of marker 3 * 3}intCheckintKintN//determine if k is legally placed in the nth vacant place{ intx =GetX (n); inty =GetY (n); if(Line[x][k] = =1)return 0;//the same line has duplicate if(Row[y][k] = =1)return 0;//the same column has duplicate if(Rowline[(X-1) /3*3+ (Y-1) /3][k] = =1)return 0;//duplicate in the same 3 * 3 square lattice return 1;}voidpf () { for(inti =1; I <=9; i++) { for(intj =1; J <=9; J + +) printf (J==1?"%d":"%d", Gra[i][j]); cout<<Endl; }}intFlag;voidBacktrackintk) { if(k > N) {PF (); flag =1;return;} for(inti =1; I <=9; i++)//There are 9 ways to fill each vacant position (status) { intx = GetX (k);inty =GetY (k); if(Check (i, K))//judging legality{gra[x] [y]=i; Deal (x, y, I,1);//array of flags that need to be maintained because of the number of new joinsBacktrack (k +1); //if (flag) return;Deal (x, Y, I,-1);//Recovery Sitegra[x [y] =0; } }}//Initializevoidinit () {N=0; Flag=0; memset (Gra,-1,sizeof(Gra)); memset (Rowline,0,sizeof(Rowline)); memset (Ans,0,sizeof(Ans)); memset (line,0,sizeof(line)); memset (Row,0,sizeof(Row));}intMain () {//freopen ("In.txt", "R", stdin); Chars[3]; intln =0; while(~SCANF ("%s", s)) { if(ln++) printf ("\ n"); Init (); if(s[0] =='?') {gra[1][1] =0; Ans[++n] =1;} Else{gra[1][1] = s[0] - -; Deal1,1, gra[1][1],1);} for(inti =0; I <9; i++) { for(intj =0; J <9; J + +) { if(i = =0&& J = =0)Continue; scanf ("%s", s); if(s[0] =='?') {gra[i +1][j +1] =0; Ans[++n] = i *9+ j +1;} Else{gra[i +1][j +1] = s[0] - -; Deal (i +1+ M +1, Gra[i +1][j +1],1);} }} Flag=0; Backtrack (1); } return 0;}
HDU 1426 Sudoku Killer (backtracking + pruning)