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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include "Sudoku.h"
#include <string>
using namespace std;
const int Sudoku::SIZE = 16;
const char Sudoku::BLANK = '.';
Sudoku::Sudoku(): solved(false), solutions(0) {
board = new char* [SIZE];
for (int row = 0; row < SIZE; row++) {
board[row] = new char[SIZE];
}
}
const Sudoku& Sudoku::operator=(const Sudoku& rhs) {
if (this != &rhs) {
if (board) {
for (int row = 0; row < SIZE; row++) {
delete[] board[row];
}
delete[] board;
board = NULL;
}
solved = rhs.solved;
board = new char* [SIZE];
for (int row = 0; row < SIZE; row++) {
board[row] = new char[SIZE];
for (int col = 0; col < SIZE; col++) {
board[row][col] = rhs.board[row][col];
}
}
}
return *this;
}
Sudoku::Sudoku(const Sudoku& other) {
board = NULL;
*this = other;
}
Sudoku::~Sudoku() {
for (int row = 0; row < SIZE; row++) { delete[] board[row]; }
delete[] board;
}
void Sudoku::load_data(istream& in) {
string current_line;
for (int row = 0; row < SIZE; row++) {
getline(in, current_line);
for (int col = 0; col < SIZE; col++) {
board[row][col] = current_line[col];
}
}
}
int Sudoku::next_row_index(int row, int col)const {
if ((row == SIZE - 1) && (col == SIZE - 1)) { return -1; }
if (col == SIZE - 1) { return row + 1; }
else { return row; } //
}
int Sudoku::next_col_index(int row, int col)const {
if ((row == SIZE - 1) && (col == SIZE - 1)) { return -1; }
if (col == SIZE - 1) { return 0; }
else { return col + 1; }
}
bool Sudoku::in_same_row(int row, char digit) const {
for (int col = 0; col < SIZE; col++) {
if (board[row][col] == digit) {
return true;
}
}
return false;
}
bool Sudoku::in_same_col(int col, char digit)const {
for (int row = 0; row < SIZE; row++) {
if (board[row][col] == digit) {
return true;
}
}
return false;
}
bool Sudoku::in_same_grid(int row, int col, char digit)const {
int grid_start_row = row / 4*4, grid_start_col = col /4*4;
for (int i = grid_start_row; i < grid_start_row + 4; i++) {
for (int j = grid_start_col; j < grid_start_col + 4; j++) {
if ((board)[i][j] == digit) {
return true;
}
}
}
return false;
}
bool Sudoku::is_safe(int row, int col, char character){
if (!in_same_row(row, character) && !in_same_col(col, character) && !in_same_grid(row - row % 4, col - col % 4, character)){
return true;
}
}
void Sudoku::solve(int row, int col, ostream& out) {
if ((row == -1) || col == -1) {
out << "Solution " << ++solutions << std::endl << std::endl;
print_solution(out);
out << std::endl;
}
/*if (board[row][col] != BLANK) { //goes to the next cell
solve(next_row_index(row, col), next_col_index(row, col), out);
}*/
else {
for (char digit = '0'; digit <= '9'; digit++) {
// Check if it is safe to place
if (in_same_row(row, digit)) { continue; }
if (in_same_col(col, digit)) { continue; }
if (in_same_grid(row, col, digit)) { continue; }
if (is_safe(row, col, digit)) {
//Assigning the character in position of the grid
board[row][col] = digit;
// Checking for next possibility
solve(row, col + 1, out);
// Removing the assigned character to the next column
board[row][col] = 0;
}
}
for (char letter = 'a'; letter <= 'f'; letter++) {//I repeated it here but the letters to attempt the same idea
if (in_same_row(row, letter)) { continue; }
if (in_same_col(col, letter)) { continue; }
if (in_same_grid(row, col, letter)) { continue; }
if (is_safe(row, col, letter)) {
board[row][col] = letter;
solve(row, col + 1, out);
board[row][col] = 0;
}
}
}
}
void Sudoku::solve(ostream & out) {
solve(0,0,out);
}
void Sudoku::print_solution(ostream& out)const {
if (!solved) {out << "Attempt to output solution for unsolved puzzle." << endl;}
else {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {out << board[row][col];}
out << endl;
}
}
}
|