Skip to content

Commit

Permalink
Merge pull request #1 from jwakely/master
Browse files Browse the repository at this point in the history
Fix memory leak
  • Loading branch information
pauek committed Mar 28, 2013
2 parents 6bd3929 + 84a1055 commit 9792d25
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions sudoku.en.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
using namespace std;

class Possible {
Expand Down Expand Up @@ -164,39 +165,34 @@ Sudoku::Sudoku(string s)
}
}

Sudoku* solve(Sudoku *S) {
if (S == NULL || S->is_solved()) {
unique_ptr<Sudoku> solve(unique_ptr<Sudoku> S) {
if (S == nullptr || S->is_solved()) {
return S;
}
int k = S->least_count();
Possible p = S->possible(k);
for (int i = 1; i <= 9; i++) {
if (p.is_on(i)) {
Sudoku *S1 = new Sudoku(*S);
unique_ptr<Sudoku> S1(new Sudoku(*S));
if (S1->assign(k, i)) {
Sudoku *S2 = solve(S1);
if (S2 != NULL) {
if (S2 != S1) delete S1;
if (auto S2 = solve(std::move(S1))) {
return S2;
}
}
delete S1;
}
}
return NULL;
return {};
}

int main() {
Sudoku::init();
string line;
while (getline(cin, line)) {
Sudoku* S = solve(new Sudoku(line));
if (S != NULL) {
if (auto S = solve(unique_ptr<Sudoku>(new Sudoku(line)))) {
S->write(cout);
} else {
cout << "No solution";
}
delete S;
cout << endl;
}
}

0 comments on commit 9792d25

Please sign in to comment.