#include<stdio.h>
#include<stdlib.h>
#define N 9
int array[N][N]={9,0,5,8,0,4,0,2,0,
8,0,0,1,0,5,0,0,9,
1,0,0,0,0,0,5,0,3,
0,0,0,3,9,0,8,0,0,
2,0,8,0,0,0,7,0,1,
3,0,9,0,8,1,0,0,0,
5,0,4,0,0,0,0,1,0,
7,0,0,6,0,0,0,0,0,
0,8,0,5,0,3,9,0,0};
int temp[N][N];
void print()
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
bool Test(int n,int m,int value)
{
bool b=true;
int i;
for(i=0;i<N;i++)
{
if(array[n][i]==value)b=false;
if(array[i][m]==value)b=false;
}
return b;
}
void swap(int row,int col)
{
int i,j,m;
if(row==N){print();system("PAUSE");}
i=row+(col+1)/N;
j=(col+1)%N;
if(array[row][col]!=0)swap(i,j);
for(m=1;m<=N;m++)
{
if(Test(row,col,m))
{
array[row][col]=m;
swap(i,j);
array[row][col]=0;
}
}
}
int main()
{
swap(0,0);
return 0;
}
这是一个求数独的问题!!!
为什么有的答案是重复的呢?如何在递归里避免重复的呢?