Question: i need help with my sudoko solve function void Sudoku::solve() //backtrack.h #ifndef BACKTRACK_H #define BACKTRACK_H #include #include class BackTrack { public: typedef std::vector ::const_iterator const_iterator;

i need help with my sudoko solve function 
void Sudoku::solve() //backtrack.h #ifndef BACKTRACK_H #define BACKTRACK_H #include  #include  class BackTrack { public: typedef std::vector::const_iterator const_iterator; typedef std::vector::const_iterator iterator; BackTrack (unsigned nVariables, unsigned arity=2); template  BackTrack (Iterator arityBegin, Iterator arityEnd); unsigned operator[] (unsigned variableNumber) const; unsigned numberOfVariables() const; unsigned arity (unsigned variableNumber) const; bool more() const; void prune (unsigned level); BackTrack& operator++(); BackTrack operator++(int); // Iterator operations for easy access to the currently assigned values const_iterator begin() const {return values.begin();} iterator begin() {return values.begin();} const_iterator end() const {return values.end();} iterator end() {return values.end();} private: bool done; std::vector arities; std::vector values; }; inline unsigned BackTrack::operator[] (unsigned variableNumber) const { return values[variableNumber]; } inline unsigned BackTrack::numberOfVariables() const { return values.size(); } inline unsigned BackTrack::arity (unsigned variableNumber) const { return arities[variableNumber]; } inline bool BackTrack::more() const { return !done; } template  BackTrack::BackTrack (Iterator arityBegin, Iterator arityEnd): arities(arityBegin, arityEnd), done(false) { fill_n (back_inserter(values), arities.size(), 0); } //backtrack.cpp #endif
#include "backtrack.h" #include  #include  BackTrack::BackTrack (unsigned nVariables, unsigned arity) { } void BackTrack::prune (unsigned level) { level = (level > numberOfVariables()) ? numberOfVariables() : level; fill (values.begin()+level, values.end(), 0); int k = level-1; bool carry = true; while (k >= 0 && carry) { values[k] += 1; if (values[k] >= arities[k]) values[k] = 0; else carry = false; --k; } done = carry; } BackTrack& BackTrack::operator++() { prune(numberOfVariables()); return *this; } BackTrack BackTrack::operator++(int) { BackTrack oldValue = *this; prune(numberOfVariables()); return oldValue; }
//sudoku.h
#ifndef SUDOKU_H #define SUDOKU_H #include "backtrack.h" #include  #include  class Sudoku { public: Sudoku (std::vector initialProblem); // Attempt to solve the puzzle. void solve(); bool hasBeenSolved() const {return solved;} const std::vector& getSolution() const; void print (std::ostream&) const; private: std::vector initial; bool solved; BackTrack problem; int square(int k) const; int innerSquare(int k) const; int row(int k) const; int column(int k) const; int posBySquare(int ou, int in) const; int posByColRow(int col, int row) const; // returns the equivalent vector position in the range 0..80 }; inline std::ostream& operator<< (std::ostream& out, const Sudoku& puzzle) { puzzle.print(out); return out; } #endif
//sudoku.cpp include "sudoku.h" #include "backtrack.h" using namespace std; Sudoku::Sudoku (std::vector initialProblem) : initial(initialProblem), problem(81, 9), solved(false) {} const std::vector& Sudoku::getSolution() const { if (solved) { vector solution (problem.begin(), problem.end()); for (int i = 0; i < 81; ++i) ++solution[i]; } else return initial; } void Sudoku::solve() { solved = false; while ((!solved) && problem.more()) { } } void Sudoku::print (std::ostream& out) const { int k = 0; for (int line = 0; line < 9; ++line) { for (int col = 0; col < 9; ++col) { out << problem[k]+1 << ' '; if (col % 3 == 2) cout << ' '; k++; } cout << endl; if (line % 3 == 2) cout << endl; } } int Sudoku::square(int k) const { int r = row(k) / 3; int c = column(k) / 3; return c + 3 * r; } int Sudoku::innerSquare(int k) const { int r = row(k) % 3; int c = column(k) % 3; return c + 3 * r; } int Sudoku::row(int k) const { return k / 9; } int Sudoku::column(int k) const { return k % 9; } 
// Given a outer square # ou and an inner square # in: int Sudoku::posBySquare(int ou, int in) const // returns the equivalent vector position in the range 0..80 { int r = (ou / 3) * 3; int c = (ou % 3) * 3; r += in / 3; c += in % 3; return posByColRow(c, r); } // Given a column and row int Sudoku::posByColRow(int col, int row) const // returns the equivalent vector position in the range 0..80 { return 9 * row + col; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!