Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a C + + program that solves Sudoku puzzles. The input to Sudoku is a 9 x 9 board that is subdivided into 3
Write a Cprogram that solves Sudoku puzzles. The input to Sudoku is a x board that is subdivided into x squares.
The input to the program is a text file containing a collection of Sudoku boards, with one board per line. For example:
Z
For each board that is read, the output is a printout of the board correctly filled in
Complete your program that solves Sudoku puzzles. Your algorithm should be based on recursion. Your algorithm should read each board from the text file, solve it print the solution, and print the number of recursive iterations needed to solve that board. After all boards have
been solved, print the average number of recursive calls needed to solve all the boards. Your algorithm should minimize the average number of recursive calls needed to solve all the boards. Print the total number of your recursive calls
Board.cpp:
#include
#include
#include
#include dmatrix.h
#include dexcept.h
using namespace std;
typedef int ValueType;
const int Blank ;
const int SquareSize ;
const int BoardSize SquareSize SquareSize;
const int MinValue ;
const int MaxValue ;
class board
public:
boardint sqSize;
void clear;
void initializeifstream& fin;
void print;
bool isBlankint int;
bool isSolved;
ValueType getCellint int;
void setCellint int, ValueType;
void clearCellint int;
void printConflicts;
private:
matrix value;
matrix rowConflicts, colConflicts, squareConflicts;
int squareNumberint int;
void updateConflictsint int, ValueType, bool;
;
board::boardint sqSize : valueBoardSize BoardSize
rowConflictsBoardSize vectorMaxValue false
colConflictsBoardSize vectorMaxValue false
squareConflictsBoardSize vectorMaxValue false
clear;
void board::clear
for int i ; i BoardSize; i
for int j ; j BoardSize; j
valueij Blank;
void board::initializeifstream& fin
char ch;
for int i ; i BoardSize; i
for int j ; j BoardSize; j
fin ch;
if ch
setCelli j ch ; Convert char digit to int and set cell
else
valueij Blank;
void board::print
for int i ; i BoardSize; i
if i SquareSize
cout endl;
for int j ; j BoardSize; j
if j SquareSize
cout ;
if isBlanki j
cout getCelli j;
else
cout ;
cout endl;
cout endl;
bool board::isBlankint i int j
return getCelli j Blank;
ValueType board::getCellint i int j
if i i BoardSize j j BoardSize
throw rangeErrorbad value in getCell";
return valueij;
void board::setCellint i int j ValueType val
valueij val;
updateConflictsi j val, true;
void board::clearCellint i int j
updateConflictsi j getCelli j false;
valueij Blank;
void board::updateConflictsint i int j ValueType val, bool add
int sqNum squareNumberi j;
rowConflictsival add;
colConflictsjval add;
squareConflictssqNumval add;
int board::squareNumberint i int j
return i SquareSize SquareSize j SquareSize ;
bool board::isSolved
for int i ; i BoardSize; i
for int j ; j BoardSize; j
if isBlanki j
return false;
return true;
void board::printConflicts
cout "Conflicts for rows, columns, and squares:" endl;
int main
const vector fileNames "sudokutxt;
for const string& fileName : fileNames
ifstream finfileNamecstr;
if fin
cerr "Cannot open fileName endl;
continue;
cout "Processing file: fileName endl;
board bSquareSize;
while fin && fin.peekZ
binitializefin;
bprint;
cout "Solved: bisSolved "Yes" : No endl;
bprintConflicts;
fin.close;
cout endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started