尝试使用递归解决数独问题,但是总是在第50层递归左右的位置停止继续入栈
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_sudoku(int sudoku[9][9])
{
printf("The Sudoku contains:\n");
for (int j=0; j<9; j++)
{
for (int i=0; i<9;i++)
{
printf("%d ",sudoku[j][i]);
}
printf("\n");
}
}
int check_sudoku(int sudoku[9][9])
{
/*
* This function checks whether a Sudoku is properly
* solved. That means that each row, column, and
* 3x3 subgrid uses each digit only once.
*
* If *ignores* zeros, so you can use it to check
* that a partial solution is valid
*/
/*****
* TO DO:
* Complete this function
* -- BUT ONLY IF YOU WANT TO!
* Our testing script only checks your Sudoku
* solution, if you want to solve it without
* using this function, or if you want to write
* your own helper functions, that's ok!
* *****/
for(int i = 0; i < 9; i++){//row
for(int j = 0; j < 9; j ++){//column
if(sudoku[i][j] != 0){
for(int k = 0; k < 9; k++){
if(j != k && sudoku[i][j] == sudoku[i][k]){//row repeat check
return 0;
}
}
for(int k = 0; k < 9; k++){
if(i != k && sudoku[i][j] == sudoku[k][j]){//column repeat check
return 0;
}
}
int x = (i / 3) * 3;
int y = (j / 3) * 3;
for(int p = 0; p < 3; p++){
for(int q = 0; q < 3; q++){
if(x + p != i && y+ q != j && sudoku[x + p][y + q] == sudoku[i][j]){//chunk repeat check
return 0;
}
}
}
}
}
}
return 1;
return 0;
}
int solved(int sudoku[9][9])
{
/*
* This function checks whether a given Sudoku is
* completely solved (it has no zeros, all digits
* are used once per row, column, and sub-grid.
* It returns 1 if the Sudoku is solved, zero
* otherwise
*/
/*****
* TO DO
* Complete this function
* BUT ONLY IF YOU WANT TO!
* You can solve the Sudoku without using this function,
* or you can write a different helper function, whatever
* makes more sense to yoU! our checker won't look at this
* function,
* ****/
if(check_sudoku(sudoku) == 0){
return 0;
}
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j ++){
if(sudoku[i][j] == 0){
return 0;
}
}
}
return 1;
}
int rowclmrepeat(int sudoku[9][9], int row, int column, int num){//if repeat return 0, else(valid) return 1
for(int i = 0; i < 9; i ++){
if(sudoku[i][column] == num){
return 0;
}
}
for(int i = 0; i < 9; i++){
if(sudoku[row][i] == num){
return 0;
}
}
return 1;
}
int chunkrepeat(int sudoku[9][9], int row, int column, int num){//if repeat return 0; else(valid) return 1
int rstart, clstart;
rstart = (row/3)*3;
clstart = (column/3)*3;
for(int i = rstart; i < rstart + 3; i++){
for(int j = clstart; j < clstart + 3; j++){
if(row != i && column != j && sudoku[i][j] == num){
return 0;
}
}
}
return 1;
}
void solve_sudoku(int sudoku[9][9], int depth)
{
/*
* This function solves an input Sudoku, the
* input has a value of 0 for any entries that
* are not yet decided. If no solution exists
* the function returns the input array *exactly
* as it was* when the function was called.
*
* The 'depth' parameter is provided in case you
* want to use it to help debug and trace your
* code. You do not need to use it if you don't
* want to, and the problem can be solved without
* using the depth value at all.
*
* The automated checker won't look at the depth
* either.
*/
/*****
* TO DO:
* Complete this function
*****/
int row;
int column;
row = depth/9;
column = depth % 9;
printf("depth is: %d\n", depth);
for(int a = 0; a < 9; a++){
for(int b = 0; b < 9; b ++){
printf("%d ", sudoku[a][b]);
}
printf("\n");
}
printf("\n");
if(check_sudoku(sudoku) == 1){
if(depth >= 81){
return;
}
if(sudoku[row][column] == 0){
for(int i = 1; i < 10; i++){
if(rowclmrepeat(sudoku, row, column, i) == 1 && chunkrepeat(sudoku, row, column, i) == 1){
sudoku[row][column] = i;
solve_sudoku(sudoku, depth + 1);
}
sudoku[row][column] = 0;
}
}
else{
solve_sudoku(sudoku, depth + 1);
}
}
}
#ifndef __testing
int main()
{
// We'll provide you with the same test case
// from the Wiki, so you can check your output.
// But as always, be aware we will test your
// solution on different Sudoku puzzles.
int Sudoku[9][9]={{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("Input puzzle is:\n");
print_sudoku(Sudoku);
solve_sudoku(Sudoku,0);
printf("Solution is:\n");
print_sudoku(Sudoku);
}
#endif
上方递归入栈过程省略
depth is: 49
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 8 3 4
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
depth is: 50
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 8 3 4
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
depth is: 51
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 8 3 4
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
depth is: 33
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 8 3 4
1 9 8 3 4 2 5 6 7
8 5 9 7 6 4 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
depth is: 25
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 8 3 4
1 9 8 3 4 2 7 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
下方递归退栈(错误操作)过程省略
Solution is:
The Sudoku contains:
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
通过depth递归达到解决数独问题的效果,但是无论是用指针,还是直接修改sudoku内的数据,在main函数内调用的时候都只会输出未改变过的sudoku数据。
Solution is:
The Sudoku contains:
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
你既想要返回改变后的值
又不想中间结果被改变之后没办法返回上一步没改之前的样子
那你折腾参数类型就没有用,因为需求是矛盾的
你应该把最终结果以函数返回值的形式返回