Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
#ifndef BACKTRACK_H
2
#define BACKTRACK_H
3
4
class MoveState
5
{
6
public:
7
virtual bool reject()=0;
8
virtual bool accept()=0;
9
virtual MoveState* next()=0;
10
11
//move "memory"
12
virtual MoveState* before()=0;
13
virtual MoveState* after()=0;
14
};
15
16
17
//the simple backtracking algorithm
18
bool backtrack(MoveState *c)
19
{
20
if(c->reject()) return false;
21
if(c->accept()) return true;
22
23
MoveState c2;
24
while(c2=c->next()) {
25
if(backtrack(c2)) {
26
return true;
27
}
28
delete c2; //not that one!
29
}
30
31
return false;
32
}
33
34