题解 | #Sudoku#
Sudoku
https://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
#include <stdio.h> int data[9][9] = {0}; int finishh = 0; int isvaild(int x,int y,int k) { for(int i = 0;i<9;i++) { if(data[x][i] == k || data[i][y] == k) { return 0; } } int start_x = x/3*3; int start_y = y/3*3; for(int i = start_x;i<start_x+3;i++) { for(int j =start_y;j<start_y+3;j++) { if(data[i][j] == k) { return 0; } } } return 1; } void sudo(int x,int y) { if(y > 8) { y=0; x++; } if(x>8) { finishh = 1; } if(data[x][y] == 0) { for(int k = 1;k<=9;k++) { if(isvaild(x, y, k)) { data[x][y] = k; sudo(x,y+1); if(finishh == 1) { return; } data[x][y] = 0; } } }else { sudo(x, y+1); } } int main() { for(int i = 0;i<9;i++) { for(int j = 0;j<9;j++) { scanf("%d",&data[i][j]); } } sudo(0, 0); for(int i =0;i<9;i++) { for(int j = 0;j<9;j++) { printf("%d ",data[i][j]); } printf("\n"); } }