Title: Sudoku
Test instructions: Solving Sudoku. From the examples and results, it should be a simple difficulty Sudoku
Idea: DFS
Set 3 arrays, Row[i][j] Determine whether the line I put a J number, Col[i][j] to determine whether the column I put J number. SQUARE[I/3][J/3][X] To determine whether the J/3 of the I/3 row is placed X-number;
#include <iostream>#include<algorithm>#include<stdlib.h>#include<time.h>#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<queue>#include<stack>#include<Set>#defineC_false Ios_base::sync_with_stdio (FALSE); Cin.tie (0)#defineINF 0x3f3f3f3f#defineINFL 0x3f3f3f3f3f3f3f3f#defineZero_ memset (x, y, sizeof (x))#defineZero (x) memset (x, 0, sizeof (x))#defineMAX (x) memset (x, 0x3f, sizeof (x))#defineSWA (x, y) {LL s;s=x;x=y;y=s;}using namespacestd;#defineN 50005Const DoublePI = ACOs (-1.0); typedefLong LongLL;BOOLcol[Ten][Ten], row[Ten][Ten], square[5][5][Ten];Charmapp[Ten];intmap[Ten][Ten];intN;BOOLDfsintz) { if(z>=Bayi)return true; intx = z/9; inty = z%9; if(Map[x][y])returnDFS (z+1); for(inti =1; i<=9; i++){ if(!row[x][i] &&!col[y][i] &&!square[x/3][y/3][i]) {Map[x][y]=i; Row[x][i]= Col[y][i] = square[x/3][y/3][i] =1; if(Dfs (z+1)) return true; Map[x][y]=0; Row[x][i]= Col[y][i] = square[x/3][y/3][i] =0; } } return false;}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);scanf"%d", &N); while(n--){ //memset (map, 0, sizeof (map));memset (Row,false,sizeof(row)); memset (col,false,sizeof(col)); memset (Square,false,sizeof(square)); for(inti =0; i<9; i++) {scanf ("%s", Mapp); for(intj =0; j<9; J + +) {Map[i][j]= Mapp[j]-'0'; intc =Map[i][j]; if(Map[i][j]) {Row[i][c]= Col[j][c] = square[i/3][j/3][C] =1; }}} DFS (0); for(intI=0;i<9; i++){ for(intj=0;j<9; j + +) printf ("%d", Map[i][j]); printf ("\ n"); } } return 0;}
DFS (POJ 2676)