#include <iostream>
#include <vector>
#include "sudoku.h"
using namespace std;
int main()
{
vector< vector<char> > rows;
// Load grid
rows=load();
// Check if grid is consistent.
if (checkall(rows))
{
cout << "Valid grid." << endl;
}
else
{
cout << "Invalid grid; exiting";
return 0;
}
// Print initial grid
print(rows);
// Solve grid.
solve(rows);
// Check solution.
cout << "Checking solution... "<<endl;
if (checkall(rows))
{
cout << "valid!" << endl;
}
else
{
cout << "not valid!" << endl;
}
return 0;
}#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "sudoku.h"
using namespace std;
// Read grid from file sudoku.dat
vector< vector<char> > load()
{
char line;
int o;
ifstream data("sudoku.dat");
cout << "Loading grid..." << endl;
vector <char> nums;
vector < vector<char> > rows;
for (int i=0; i!=9; i++)
{
for (int j=0; j!=9; j++)
{
data>>line;
nums.push_back(line);
}
rows.push_back(nums);
nums.resize(0);
}
return rows;
}
// Print grid with nice format
void print(vector< vector<char> > rows)
{
for (int i=0; i!=9; i++)
{
for (int j=0; j!=9; j++)
{
cout << rows[i][j];
if (j==2 || j==5)
{
cout << " ";
}
else if (j==8)
{
cout << endl;
if (i==2 || i==5)
{
cout << endl;
}
}
else
{
cout << " ";
}
}
}
}
// Returns a string containing the center of the block
// to which position i,j belongs.
string block(int i, int j)
{
char x,y;
string result = "00";
switch (i)
{
case 1:
case 2:
case 3:
x='2';
break;
case 4:
case 5:
case 6:
x='5';
break;
case 7:
case 8:
case 9:
x='8';
break;
}
switch (j)
{
case 1:
case 2:
case 3:
y='2';
break;
case 4:
case 5:
case 6:
y='5';
break;
case 7:
case 8:
case 9:
y='8';
break;
}
result[0] = x;
result[1] = y;
return result;
}
// Checks whether value num can be placed at position i,j.
bool check(int num, int i, int j, vector< vector<char> > rows)
{
char value = (char)(num+48);
string center;
int x,y;
if (value == '-')
{
return true;
}
// check row
for (int k=0; k!=9; k++)
{
if (rows[i][k]==value && k!=j)
{
return false;
}
}
// check column
for (int k=0; k!=9; k++)
{
if (rows[k][j]==value && k!=i)
{
return false;
}
}
// check region
center = block(i,j);
x = center[0]-48;
y = center[1]-48;
for (int m=-1; m<=1; m++)
{
for (int n=-1; n<=1; n++)
{
if (rows[x+m][y+n]==value && !((x+m)==i && (y+n)==j))
{
return false;
}
}
}
return true;
}
// Recursive Sudoku solver
void solve(vector< vector<char> > rows)
{
bool solved;
int i,j;
if (solved)
{
return;
}
// Find next empty value
for (i=0; i!=9; i++)
{
for (j=0; j!=9; j++)
{
if (rows[i][j] == '-')
{
break;
}
}
if (rows[i][j] == '-')
{
break;
}
}
// If no empty value is found, grid is solved
if (i==8 && j==8)
{
cout << "Grid solved!" << endl;
solved = true;
print(rows);
return;
}
// Try next possible number and solve the rest recursively
for (int a=1; a<=9; a++)
{
if (check(a,i,j,rows))
{
rows[i][j] = (char)(a+48);
solve(rows);
}
}
rows[i][j] = '-';
return;
}
//Check all values on grid for consistency (ignores empty values)
bool checkall(vector< vector<char> > rows)
{
int i,j;
char value;
for (i=0; i!=9; i++)
{
for (j=0; j!=9; j++)
{
if (rows[i][j] != '-')
{
value = rows[i][j];
if (!(check((int)value,i,j,rows)))
{
return false;
}
}
}
return true;
}
return true;
}#include <vector> using namespace std; vector< vector<char> > load(); void print(vector< vector<char> > rows); string block(int i, int j); bool check(int num, int i, int j, vector< vector<char> > rows); void solve(vector< vector<char> > rows); bool checkall(vector< vector<char> > rows);
const int squareSize = 9; for ( int i = 0; i < squareSize ; i++ )
bool solved; int i,j; if (solved) // ERROR: Un-initialized variable { return; }
// Find next empty value for (i=0; i!=9; i++) { for (j=0; j!=9; j++) { if (rows[i][j] == '-') break; } if (rows[i][j] == '-') // ERROR j == 9 here and is out of range { break; } }
#include <iostream>
#include <vector>
#include "sudoku.h"
using namespace std;
int main()
{
vector< vector<char> > rows;
// Load grid
rows=load();
// Check if grid is consistent.
if (checkall(&rows))
{
cout << "Valid grid." << endl;
}
else
{
cout << "Invalid grid; exiting";
return 0;
}
// Print initial grid
print(&rows);
// Solve grid.
solve(&rows);
// Check solution.
cout << "Checking solution... "<<endl;
if (checkall(&rows))
{
cout << "valid!" << endl;
}
else
{
cout << "not valid!" << endl;
}
return 0;
}#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "sudoku.h"
using namespace std;
// Read grid from file sudoku.dat
vector< vector<char> > load()
{
char line;
ifstream data("sudoku.dat");
cout << "Loading grid..." << endl;
vector <char> nums;
vector < vector<char> > rows;
for (int i=0; i<squaresize; i++)
{
for (int j=0; j<squaresize; j++)
{
data>>line;
nums.push_back(line);
}
rows.push_back(nums);
nums.resize(0);
}
return rows;
}
// Print grid with nice format
void print(vector< vector<char> > *rows)
{
for (int i=0; i<squaresize; i++)
{
for (int j=0; j<squaresize; j++)
{
cout << (*rows)[i][j];
if (j==2 || j==5)
{
cout << " ";
}
else if (j==8)
{
cout << endl;
if (i==2 || i==5)
{
cout << endl;
}
}
else
{
cout << " ";
}
}
}
}
// Returns a string containing the center of the block
// to which position i,j belongs.
string block(int i, int j)
{
char x,y;
string result = "00";
switch (i)
{
case 0:
case 1:
case 2:
x='2';
break;
case 3:
case 4:
case 5:
x='5';
break;
case 6:
case 7:
case 8:
x='8';
break;
}
switch (j)
{
case 0:
case 1:
case 2:
y='2';
break;
case 3:
case 4:
case 5:
y='5';
break;
case 6:
case 7:
case 8:
y='8';
break;
}
result[0] = x;
result[1] = y;
return result;
}
// Checks whether value num can be placed at position i,j.
bool check(int num, int i, int j, vector< vector<char> > *rows)
{
char value = (char)(num+48);
string center;
int x,y;
if (value == '-')
{
return true;
}
// check row
for (int k=0; k<squaresize; k++)
{
if ((*rows)[i][k]==value && k!=j)
{
return false;
}
}
// check column
for (int k=0; k<squaresize; k++)
{
if ((*rows)[k][j]==value && k!=i)
{
return false;
}
}
// check region
center = block(i,j);
x = center[0]-48;
y = center[1]-48;
for (int m=-1; m<=1; m++)
{
for (int n=-1; n<=1; n++)
{
if ((*rows)[x+m][y+n]==value && !((x+m)==i && (y+n)==j))
{
return false;
}
}
}
return true;
}
// Recursive Sudoku solver
void solve(vector< vector<char> > *rows)
{
bool solved;
int i,j;
if (solved)
{
return;
}
// Find next empty value
for (i=0; i<squaresize; i++)
{
for (j=0; j<squaresize; j++)
{
if ((*rows)[i][j] == '-')
{
break;
}
}
if ((*rows)[i][j] == '-')
{
break;
}
}
// If no empty value is found, grid is solved
if (i==9 && j==9)
{
cout << "Grid solved!" << endl;
solved = true;
print((rows));
return;
}
// Try next possible number and solve the rest recursively
for (int a=1; a<=9; a++)
{
if (check(a,i,j,rows))
{
(*rows)[i][j] = (char)(a+48);
solve(rows);
}
}
(*rows)[i][j] = '-';
return;
}
//Check all values on grid for consistency (ignores empty values)
bool checkall(vector< vector<char> > *rows)
{
int i,j;
char value;
for (i=0; i<squaresize; i++)
{
for (j=0; j<squaresize; j++)
{
if ((*rows)[i][j] != '-')
{
value = (*rows)[i][j];
if (!(check((int)value,i,j,rows)))
{
return false;
}
}
}
return true;
}
return true;
}#include <vector> using namespace std; const int squaresize=9; vector< vector<char> > load(); void print(vector< vector<char> > *); string block(int i, int j); bool check(int num, int i, int j, vector< vector<char> > *); void solve(vector< vector<char> > *); bool checkall(vector< vector<char> > *);
Why are you using a pointer, and not a reference?
You're still not initialising solved.
You're still running off the end of an array.
void solve(vector< vector<char> > &rows)
{
bool solved=false;
int i,j;
if (solved)
{
return;
}
// Find next empty value
for (i=0; i<squaresize; i++)
{
for (j=0; j<squaresize; j++)
{
if (rows[i][j] == '-')
{
break;
}
}
if (rows[i][j-1] == '-')
{
break;
}
}
// If no empty value is found, grid is solved
if (i==9 && j==9)
{
cout << "Grid solved!" << endl;
solved = true;
print(rows);
return;
}
// Try next possible number and solve the rest recursively
for (int a=1; a<=9; a++)
{
if (check(a,i,j,rows))
{
rows[i][j] = (char)(a+48);
solve(rows);
}
}
rows[i][j] = '-';
return;
}for (int a=0; a<9; a++)
{
if (i==9)
{
if (check(a,i-1,j,rows))
{
rows[i-1][j] = (char)(a+48);
solve(rows);
}
}
if (check(a,i,j,rows))
{
rows[i][j] = (char)(a+48);
solve(rows);
}
}
rows[i][j] = '-';
return;center = block(i,j);
x = center[0]-48;
y = center[1]-48;
for (int m=0; m<=2; m++)
{
for (int n=0; n<=2; n++)
{
if (rows[x+m][y+n]==value && !((x+m)==i && (y+n)==j))
{
return false;
}
}
}
return true;string block(int i, int j)
{
char x,y;
string result = "00";
switch (i)
{
case 0:
case 1:
case 2:
x='1';
break;
case 3:
case 4:
case 5:
x='4';
break;
case 6:
case 7:
case 8:
x='7';
break;
}
switch (j)
{
case 0:
case 1:
case 2:
y='1';
break;
case 3:
case 4:
case 5:
y='4';
break;
case 6:
case 7:
case 8:
y='7';
break;
}
result[0] = x;
result[1] = y;
return result;
}#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "sudoku.h"
using namespace std;
// Read grid from file sudoku.dat
vector< vector<char> > load()
{
char line;
ifstream data("sudoku.dat");
cout << "Loading grid..." << endl;
vector <char> nums;
vector < vector<char> > rows;
for (int i=0; i<squaresize; i++)
{
for (int j=0; j<squaresize; j++)
{
data>>line;
nums.push_back(line);
}
rows.push_back(nums);
nums.resize(0);
}
return rows;
}
// Print grid with nice format
void print(vector< vector<char> > &rows)
{
for (int i=0; i<squaresize; i++)
{
for (int j=0; j<squaresize; j++)
{
cout << rows[i][j];
if (j==2 || j==5)
{
cout << " ";
}
else if (j==8)
{
cout << endl;
if (i==2 || i==5)
{
cout << endl;
}
}
else
{
cout << " ";
}
}
}
}
// Returns a string containing the center of the block
// to which position i,j belongs.
string block(int i, int j)
{
char x,y;
string result = "00";
switch (i)
{
case 0:
case 1:
case 2:
x='1';
break;
case 3:
case 4:
case 5:
x='4';
break;
case 6:
case 7:
case 8:
x='7';
break;
}
switch (j)
{
case 0:
case 1:
case 2:
y='1';
break;
case 3:
case 4:
case 5:
y='4';
break;
case 6:
case 7:
case 8:
y='7';
break;
}
result[0] = x;
result[1] = y;
return result;
}
// Checks whether value num can be placed at position i,j.
bool check(int num, int i, int j, vector< vector<char> > &rows)
{
char value = (char)(num+48);
string center;
int x,y;
if (value == '-')
{
return true;
}
// check row
for (int k=0; k<squaresize; k++)
{
if (rows[i][k]==value && k!=j)
{
return false;
}
}
// check column
for (int k=0; k<squaresize; k++)
{
if (rows[k][j]==value && k!=i)
{
return false;
}
}
// check region
center = block(i,j);
x = center[0]-48;
y = center[1]-48;
for (int m=0; m<=2; m++)
{
for (int n=0; n<=2; n++)
{
if(x==7 && y==7)
{
for (int m=-1; m<=1; m++)
{
for (int n=-1; n<=1; n++)
{
if(x==-1 && y==-1)
{
if (rows[x+m+1][y+n+1]==value && !((x+m+1)==i && (y+n+1)==j))
{
return false;
}
}
else if(x==-1)
{
if (rows[x+m+1][y+n]==value && !((x+m+1)==i && (y+n)==j))
{
return false;
}
}
else if(y==-1)
{
if (rows[x+m][y+n+1]==value && !((x+m)==i && (y+n+1)==j))
{
return false;
}
}
else if(rows[x+m][y+n]==value && !((x+m)==i && (y+n)==j))
{
return false;
}
}
}
return true;
}
// Recursive Sudoku solver
void solve(vector< vector<char> > &rows)
{
bool solved=false;
int i,j;
if (solved)
{
return;
}
// Find next empty value
for (i=0; i<squaresize; i++)
{
for (j=0; j<squaresize; j++)
{
if (rows[i][j] == '-')
{
break;
}
}
if (rows[i][j-1] == '-')
{
break;
}
}
// If no empty value is found, grid is solved
if (i==9 && j==9)
{
cout << "Grid solved!" << endl;
solved = true;
print(rows);
return;
}
// Try next possible number and solve the rest recursively
for (int a=1; a<=9; a++)
{
if (i==9)
{
if (check(a,i-1,j,rows))
{
rows[i-1][j] = (char)(a+48);
solve(rows);
}
}
else
{
if (check(a,i,j,rows))
{
rows[i][j] = (char)(a+48);
solve(rows);
}
}
}
}
//Check all values on grid for consistency (ignores empty values)
bool checkall(vector< vector<char> > rows)
{
int i,j;
char value;
for (i=0; i<squaresize; i++)
{
for (j=0; j<squaresize; j++)
{
if (rows[i][j] != '-')
{
value = rows[i][j];
if (!(check((int)value,i,j,rows)))
{
return false;
}
}
}
return true;
}
return true;
}| DaniWeb Message | |
| Cancel Changes | |