vuoi
o PayPal
tutte le volte che vuoi
Programma in c++ che risolve i sudoku.
La tabella inziale è letta da file (riga 225 del programma):
il file si chiama start.txt e ha il seguente formato:
riga colonna valore
dove riga e colonna vanno da 0 a 8 e valore è il valore numerico.
Ad esempio
5 0 4
0 1 3
Fissa il numero 4 alla 6° colonna della 1° riga, e il numero 3 alla 2° colonna della 1° riga.
Gli utenti windows dovrebbero cancellare le righe 210 e 218, che fanno riferimento a chiamate di sistema per sistemi operativi unix.
#include <iostream>
#include <fstream>
class element{
private:
int i, j;
bool is_fixed [9][9];
int start[9][9];
int guess[9][9];
public:
void Next();
void Previus();
bool CheckRow() const;
bool CheckCol() const;
bool CheckSqr() const;
bool Check() const;
void Read(const std::string&);
void Reset();
void PrintStart() const;
void PrintGuess() const;
bool IsFixed() const;
bool IsFirst() const;
bool IsLast() const;
int GetI() const;
int GetJ() const;
int GetStartVal() const;
void Try(int N);
void GuessIs(int N);
void RemoveGuess();
int GetGuess() const;
void Finished() const;
};
void element::Try(int N){
//PrintGuess();
while (IsFixed()){
Next();
}
GuessIs(N);
const bool ch = Check();
if ((ch) && (IsLast())){
Finished();
}
if (ch && (!IsLast())) {
Next();
Try(1);
}
if (!ch){
if (N<9) {
N++;
Try(N);
}
else {
do {
RemoveGuess();
Previus();
} while ((GetGuess()+1)>9);
Try(GetGuess()+1);
}
}
}
void element::GuessIs(int N){
if (IsFixed()) {
std::cerr<<" Error: try to guess a fix \n";
return;
}
guess[i][j] = N;
}
void element::RemoveGuess(){
guess[i][j] = 0;
}
int element::GetGuess() const{
return (guess[i][j]);
}
void element::Finished() const{
PrintGuess();
std::cout<<"Sudoku Solved! \n";
}
void element::Next(){
do {
j = (i+1<9) ? j : j+1;
i = (i+1<9) ? i+1 : 0;
} while ((is_fixed[i][j]) && (!((i==8)&&(j==8))));
if (j>8){
i=8;
j=8;
}
}
void element::Previus(){
if (!((i==0)&&(j==0))) {
do {
j = (i-1>=0) ? j : j-1;
i = (i-1>=0) ? i-1 : 8;
} while ((is_fixed[i][j]) && (!((i==0)&&(j==0))));
}
}
bool element::CheckRow() const{
for (int x=0; x<9; x++){
if ((guess[i][j]==guess[x][j]) && (x!=i) && (guess[i][j]!=0))
return false;
}
return true;
}
bool element::CheckCol() const{
for (int y=0; y<9; y++){
if ((guess[i][j]==guess[i][y]) && (y!=j) && (guess[i][j]!=0))
return false;
}
return true;
}
bool element::CheckSqr() const{
const int sqi = i/3;
const int sqj = j/3;
for (int x=0; x<3; x++){
for (int y=0; y<3; y++){
if ((guess[i][j]==guess[3*sqi+x][3*sqj+y]) && (!
((j==3*sqj+y)&&(i==3*sqi+x))) && (guess[i][j]!=0))
return false;
}
}
return true;
}
bool element::Check() const{
return ((CheckRow()) && (CheckCol()) && (CheckSqr()));
}
bool element::IsFirst() const{
return ((i==0)&&(j==0));
}
bool element::IsLast() const{
return ((i==8) && (j==8));
}
bool element::IsFixed() const{
return (is_fixed[i][j]);
}
int element::GetI() const{
return i;
}
int element::GetJ() const{
return j;
}
int element::GetStartVal() const{
return start[i][j];
}
void element::Reset(){
i = 0;
j = 0;
for (int x=0; x<9; x++){
for (int y=0; y<9; y++){
is_fixed[x][y] = 0;
start[x][y] = 0;
guess[x][y] = 0;
}
}
}
void element::Read(const std::string& filename){
std::ifstream f;
f.open(filename.c_str());
if (!f){
std::cerr<<"File "<<filename<<" not found \n";
return;
}
while (!f.eof()){