#include <iostream>using namespace Std;bool heng (int **sudo, int A, int b, int value) {BOOL flag = true; for (int i=0; i<9; i++) {if (sudo[a][i]==value) {flag = false; }} return flag;} BOOL Shu (int **sudo, int A, int b, int value) {BOOL flag = true; for (int i=0; i<9; i++) {if (sudo[i][b]==value) {flag = false; }} return flag;} BOOL Fang (int **sudo, int A, int b, int value) {BOOL flag = true; int x = a/3*3; int y = b/3*3; for (int i = 0, i<3; i++) {for (int j=0; j<3; J + +) {if (Sudo[x+i][y+j]==value) {flag = false; }}} return flag;} void Dfs (int a, int b, int **sudo, bool &flag) {if (a==9 && b==0) {for (int i=0; i<9; i++) {for (int j=0; j<8; J + +) {cout<<sudo[i][j]<< "; } cout<<sudo[i][8]<<endl; } flag = False;//find return; } else {if (flag) {if (sudo[a][b]==0) {for (int i=1; i<=9; i++ ) {if (Flag && Heng (sudo, a, B, i) && shu (sudo, A, b,i) && Fang ( sudo, a, B, i)) {sudo[a][b] = i; DFS (B==8) a+1:a, (b==8) 0:b+1, sudo, flag); SUDO[A][B] = 0; }}}} else {DFS (b==8) a+1:a, (b==8) 0:b+1, sudo, flag); }} else {return; }}}int Main () {int **sudo; sudo = new int* [9]; for (int i=0; i<9; i++) {sudo[i] = new int [9]; } for (int i=0, i<9; i++) {for (int j=0; j<9; J + +) {cin>>sudo[i][j]; }}//DFS BOOL flag = true; DFS (0,0,sudo, flag); for (int i=0; i<9; i++) {delete [] sudo[i]; } delete [] sudo; return 0;}
Describe |
Problem Description: Sudoku (Sudoku) is a popular digital logic game. The player needs to calculate the number of all remaining spaces according to the known numbers on the 9x9 disk, and the numbers in each row, each column, and in each of the rough lines are 1-9 and do not repeat. Input: An array of 9x9 disks that contain known numbers [vacant bits are represented by the number 0] Output: Complete array of 9x9 disks |
Knowledge points |
Find, search, sort |
Run time limit |
10M |
Memory limit |
128 |
Input |
An array of 9x9 disks that contain known numbers [vacant bits are represented by the number 0] |
Output |
Complete array of 9x9 disks |
Sample input |
0 9 2 4 8 1 7 6 3 4 1 3 7 6 2 9 8 5 8 6 7 3 5 9 4 1 2 6 2 4 1 9 5 3 7 8 7 5 9 8 4 3 1 2 6 1 3 8 6 2 7 1 5 3 8 6 4 9 3 8 6 9 1 4 2 5 7 0 4 5 2 7 6 8 3 1 |
Sample output |
5 9 2 4 8 1 7 6 3 4 1 3 7 6 2 9 8 5 8 6 7 3 5 9 4 1 2 6 2 4 1 9 5 3 7 8 7 5 9 8 4 3 1 2 6 1 3 8 6 2 7 1 5 3 8 6 4 9 3 8 6 9 1 4 2 5 7 9 4 5 2 7 6 8 3 1 |
Sudoku Sudoku Game