최근 스도쿠 솔버를 C++로 만들었습니다. 문제를 해결하기 위해 역 추적 알고리즘을 사용했지만 일부 경우에는 5 행까지만 해결합니다.스도쿠 솔버 C++ 부분적으로 해결
작업 경우 : [6] [5] = 2, [4] [5] = 1
케이스 5 행 후에 실패 [1] [1] = 1
I가 없어 그것은 부분적으로 어떤 경우에 대한 sudoko을 해결하는 이유가 무엇인지 알고있는 값을 반환하지 않고 그 경우 sudoko(int, int)
에서
using namespace std;
#include<iostream>
int a[9][9],b[9][9];
bool searchrow(int i,int w,int p){
int q=0;
for(int j=0;j<9;j++){
if(j==w){
continue;
}
if(a[i][j]==p){
q=1;break;
}
}
if(q==1){
return false;
}
else
return true;
}
bool searchcoloumn(int i,int w,int p){
int q=0;
for(int j=0;j<9;j++){
if(j==w){
continue;
}
if(a[j][i]==p){
q=1;break;
}
}
if(q==1){
return false;
}
else
return true;
}
bool searchmatrix(int i,int j,int p){
int m,n,x,y,l,k,q;
m=(i/3)*3;
n=(j/3)*3;
x=m+2;
y=n+2;
q=0;
for(l=m;l<=x;l++){
for(k=n;k<=y;k++){
if(l==i&&k==j){ //skip the current location
continue;
}
if(a[l][k]==p){
q=1;
break;
}
}
}
if(q==0){
return true;
}
else
return false;
}
bool place(int i,int j,int p){
if(searchrow(i,j,p)&&searchcoloumn(j,i,p)&&searchmatrix(i,j,p)){
return true;
}
else{
return false;
}
}
bool sudoko(int i,int j){
int w,x;
for(int p=1;p<10;p++){
x=0;
if(place(i,j,p)){
if(b[i][j]==0){
a[i][j]=p;
}
if((i==8)&&(j==8)){
return true;
}
else if(j==8){
sudoko(i+1,0);
}
else{
sudoko(i,j+1);//move to next coloumn
}
}
}
}
int main(){
int i,j,t,data;
cout<<"\nEnter how many no. to add to sudoko\n";
cin>>t;//t is the no of element which are initially present in sudoko and user should give as input
cout<<"\nEnter row , coloumn and then data at the respective location\n";
for(int m=0;m<9;m++){
for(int n=0;n<9;n++){
a[m][n]=0;
b[m][n]=0;
}
}
while(t--){
cout<<"Enter row";
cin>>i;
cout<<"Enter coloumn";
cin>>j;
cout<<"Enter data";
cin>>data;
a[i][j]=data;
b[i][j]=data;
}
if(sudoko(0,0));//used a semicolon here so that to display result
for(int p=0;p<9;p++){
cout<<"\n"
for(int q=0;q<9;q++){
cout<<a[p][q]<<"\t";
}
}
}
디버거를 사용 했습니까? – sashoalm