I am writing a C++ program that solves a sodoku puzzle this is what i have so fa
ID: 641767 • Letter: I
Question
I am writing a C++ program that solves a sodoku puzzle this is what i have so far
// File: sudoku.cpp
// Tab stops: 4
// This program reads a Sudoku puzzle and prints a solution to that puzzle.
#include
#include
#include
#include "intset.cpp"
using namespace std;
typedef SetOfSmallInts Puzzle [9] [9];
typedef SetOfSmallInts* PuzzleSection [9];
enum SolutionStatus {solved, unsolvable, working};
//==============================================================
// copySetArray
//==============================================================
// Parameters p and q are arrays of 9 sets. Copy array p into
// array q.
//==============================================================
void copySetArray(SetOfSmallInts* q, SetOfSmallInts* p)
{
for(int i = 0; i < 9; i++)
{
q[i] = p[i];
}
}
//==============================================================
// copyPuzzle
//==============================================================
// Copy puzzle p into q. For example, if p is a puzzle, then
// Puzzle q = newPuzzle();
// copyPuzzle(q, p);
// stores a copy of puzzle p into q. (Only allocate q if it
// was not already allocated.)
//==============================================================
void copyPuzzle(Puzzle q, Puzzle p)
{
for(int i = 0; i < 9; i++)
{
copySetArray(q[i], p[i]);
}
}
//==============================================================
// getRow
//==============================================================
// Store the i-th row of puzzle p into puzzle section S.
// The rows are numbered from 0 to 8.
//
// After doing this, the k-th set in row i is *(S[k]).
// The cells in the row are numbered 0,1,...,8.
//==============================================================
void getRow(PuzzleSection R, Puzzle p, int i)
{
int j;
for(j = 0; j < 9; j++)
{
R[j] = &(p[i][j]);
}
}
//==============================================================
// getColumn
//==============================================================
// Store the j-th column of puzzle p into puzzle section S.
// The columns are numbered from 0 to 8.
//
// After doing this, the k-th set in column j is
// *(S[i]). The cells in the column are numbered 0,1,...,8.
//==============================================================
void getColumn(PuzzleSection R, Puzzle p, int j)
{
int i;
for(i = 0; i < 9; i++)
{
R[i] = &(p[i][j]);
}
}
//==============================================================
// getSquare
//==============================================================
// Store the k-th square of puzzle p into puzzle section S.
// The squares are numbered as follows.
// 0 1 2
// 3 4 5
// 6 7 8
// For example, square 4 is the middle square in the puzzle.
//
// After doing getSquare, the i-th set in the square is *(S[i]).
// The cells in the square are numbered 0,1,...,8, in the same
// pattern shown above for the squares themselves.
// For example *(R[3]) is the first position in the second row
// of the square.
//==============================================================
void getSquare(PuzzleSection R, Puzzle p, int k)
{
int i;
for(i = 0; i < 9; i++)
{
R[i] = &(p[k - k%3 + i/3][3*(k%3) + i%3]);
}
}
void readPuzzle(Puzzle p)
{
char c;
SetOfSmallInts set = rangeSet(1,9);
for(int i =0; i<9; i++)
{
for(int j=0; j < 9; j++)
{
cin >> c;
if(c == '-')
{
p[i][j]=set;
}
else
{
p[i][j] = singletonSet(c-'0');
}
}
}
}
void printPuzzle(Puzzle p)
{
int k;
for(int i=0; i < 9; i++)
{
for(int j=0; j < 9; j++)
{
k= onlyMember(p[i][j]);
if (j == 3 || j == 6)
{
cout << " ";
}
if (k == 0)
{
cout << "-";
}
else
{
cout << k;
}
}
if (i==2 || i == 5)
{
cout << " ";
cout <<" ";
}
}
}
bool tacticOneOnSection(PuzzleSection sec)
{
SetOfSmallInts tempSet[9];
Puzzle section;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(member(0,sec[i][j]))
{
sec[i][j] = rangeSet(1,9);
}
}
}
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(size(sec[i][j]) > 1)
{
getRow(sec, section, i);
for(int z = 0; z < 9; z++)
{
tempSet[z] = *section[z];
}
for(int k = 0; k<9; k++)
{
if(size(tempSet[k]) == 1)
sec[i][j] = setDifference(sec[i][j], tempSet[k]);
}
}
if(size(sec[i][j]) > 1)
{
getColumn(sec , section, j);
for(int z = 0; z < 9; z++)
{
tempSet[z] = *section[z];
}
for(int k = 0; k<9; k++)
{
if(size(tempSet[k]) == 1)
sec[i][j] = setDifference(sec[i][j], tempSet[k]);
}
}
if(size(sec[i][j]) > 1)
{
getSquare(sec, section, i);
for(int z = 0; z < 9; z++)
{
tempSet[z] = *section[z];
}
for(int k = 0; k<9; k++)
{
if(size(tempSet[k]) == 1)
sec[i][j] = setDifference(sec[i][j], tempSet[k]);
}
}
}
}
}
bool tacticOne(Puzzle p)
{
bool result = false;
PuzzleSection scanSec;
for(int i = 0; i < 9; i++)
{
getRow(scanSec,p,i);
if(tacticOneOnSection(scanSec)) result = true;
}
for(int i = 0; i < 9; i++)
{
getColumn(scanSec,p,i);
if(tacticOneOnSection(scanSec)) result = true;
}
for(int i = 0; i < 9; i++)
{
getSquare(scanSec,p,i);
if(tacticOneOnSection(scanSec)) result = true;
}
return result;
}
bool nonSingleton(Puzzle p)
{
bool final = false;
SetOfSmallInts sett;
for( int i = 0; i < 9; i++)
{
for( int j = 0; i < 9; j++)
{
sett=p[i][j];
if(isSingleton(sett)!=1) return true;
else return false;
}
return final;
}
}
void showPuzzle(Puzzle p)
{
SetOfSmallInts s;
int smallInt = 0;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
s=p[i][j];
for(int k=0; k < 9; k++)
{
smallInt=smallest(s);
if(smallInt==0 && k!=0)
{
cout << " ";
}
else cout << smallInt;
s = remove(smallInt, s);
if (k==9) cout << " ";
}
}
cout << ' ';
}
}
//==============================================================
// main
//==============================================================
int main(int argc, char** argv)
{
Puzzle p;
readPuzzle(p);
printPuzzle(p);
showPuzzle(p);
}
these are the two files you need to use
intset.h
Here is the link to the assignment
http://www.cs.ecu.edu/~karl/3300/spr15/assignments/Assignment3/assn3.html
i have gotten like most of it working just cant figure out the last part and this hard since im very new to C++ and programing in general
Explanation / Answer
#include #include #include #include using namespace std; #define UNASSIGNED 0 #define N 9 bool FindUnassignedLocation(int grid[N][N], int &row, int &col); bool isSafe(int grid[N][N], int row, int col, int num); /* assign values to all unassigned locations for Sudoku solution */ bool SolveSudoku(int grid[N][N]) { int row, col; if (!FindUnassignedLocation(grid, row, col)) return true; for (int num = 1; numRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.