1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#pragma once
#include <iostream>
#include <string>
#define NUM_ROWS 9
#define NUM_COLS 9
class Sudoku
{
public:
char ** GetInput();
void freeBoard(char **board);
void printBoard(char **board);
bool Solver(char ** board);
bool FindEmptySquares(char ** board, int &row, int &col);
bool NotPlayed(char ** board, int row, int col, int number);
bool UsedInRow(char ** board, int row, int number);
bool UsedInCol(char ** board, int col, int number);
bool UsedInBox(char **board, int startingRow, int startingCol, int number);
};
bool Sudoku::UsedInBox(char ** board, int startingRow, int startingCol, int number)
{
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 3; ++col)
{
if (board[row + startingRow][col + startingCol] == '0' + number)
{
return true;
}
}
}
return false;
}
bool Sudoku::UsedInCol(char ** board, int col, int number)
{
for (int row = 0; row < NUM_ROWS; ++row)
{
if (board[row][col] == '0' + number)
{
return true;
}
}
return false;
}
bool Sudoku::UsedInRow(char ** board, int row, int number)
{
for (int col = 0; col < NUM_COLS; ++col)
{
if (board[row][col] == '0' + number)
{
return true;
}
}
return false;
}
bool Sudoku::NotPlayed(char ** board, int row, int col, int number)
{
if ((UsedInRow(board, row, number) && UsedInCol(board, col, number) &&
UsedInBox(board, row - row % 3, col - col % 3, number)))
{
return false;
}
return true;
}
bool Sudoku::FindEmptySquares(char ** board, int &row, int &col)
{
for (int i = 0; row < NUM_ROWS; ++i)
{
for (int j = 0; j < NUM_COLS; ++j)
{
if (board[i][j] == '0')
{
row = i;
col = j;
return true;
}
}
}
return false;
}
bool Sudoku::Solver(char **board)
{
int row, col;
char empty = '0';
if (!FindEmptySquares(board, row, col))
{
return true;
}
// Check for numbers 1-9
for (int number = 1; number <= 9; ++number)
{
if (NotPlayed(board, row, col, number))
{
// Make an assignment tentatively
board[row][col] = '0' + number;
// If successful return true
if (Solver(board))
{
return true;
}
// Else failure, assign an "empty" square and try again
else
{
board[row][col] = empty;
}
}
}
return false; // trigger backtracking
}
char ** Sudoku::GetInput()
{
std::string input;
char **board = new char*[NUM_ROWS];
//std::cout << "Enter the board, 0 being an empty square" << std::endl;
//std::cin >> input;
input = "306508400520000000087000031003010080900863005050090600130000250000000074005206300";
//input = "007401680186300040900806710609040530070085902850039004090070806268904007005260091";
for (int row = 0; row < NUM_ROWS; ++row)
{
board[row] = new char[NUM_ROWS];
for (int col = 0; col < NUM_COLS; ++col)
{
board[row][col] = input[col + (row * NUM_COLS)];
}
}
return board;
}
void Sudoku::printBoard(char **board)
{
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
std::cout << board[row][col];
std::cout << "\n";
}
}
void Sudoku::freeBoard(char **board)
{
for (int i = 0; i < 81; ++i)
{
delete[] board[i];
}
delete[] board;
}
|