Problem 2 Finished.
This commit is contained in:
parent
d7dae370f9
commit
9f01313edf
5 changed files with 71 additions and 9 deletions
|
@ -1 +1 @@
|
||||||
((3,12,8),(2,4,6),(14,5,2))
|
((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5)))
|
||||||
|
|
|
@ -16,6 +16,7 @@ using namespace std;
|
||||||
// Perform min/max on this tree.
|
// Perform min/max on this tree.
|
||||||
void minmax(node *n)
|
void minmax(node *n)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(n->getLevel() % 2 == 0)
|
if(n->getLevel() % 2 == 0)
|
||||||
{
|
{
|
||||||
int max = n->getValue();
|
int max = n->getValue();
|
||||||
|
@ -23,16 +24,32 @@ void minmax(node *n)
|
||||||
{
|
{
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
for(int c = 0; c < n->getChildren().size(); c++)
|
||||||
{
|
{
|
||||||
minmax(n->getChildAt(c));
|
n->getChildAt(c)->setBeta(n->getBeta());
|
||||||
|
n->getChildAt(c)->setAlpha(n->getAlpha());
|
||||||
|
|
||||||
|
cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
||||||
|
|
||||||
|
minmax(n->getChildAt(c));
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() > max)
|
if(n->getChildAt(c)->getValue() > max)
|
||||||
{
|
{
|
||||||
max = n->getChildAt(c)->getValue();
|
max = n->getChildAt(c)->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(n->getChildAt(c)->getValue() > n->getAlpha())
|
||||||
|
{
|
||||||
|
n->setAlpha(n->getChildAt(c)->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(n->getAlpha() >= n->getBeta())
|
||||||
|
{
|
||||||
|
cout << "Beta pruning" << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
n->setValue(max);
|
n->setValue(max);
|
||||||
cout << n->getValue() << endl;
|
//cout << n->getValue() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
@ -42,16 +59,32 @@ void minmax(node *n)
|
||||||
{
|
{
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
for(int c = 0; c < n->getChildren().size(); c++)
|
||||||
{
|
{
|
||||||
minmax(n->getChildAt(c));
|
n->getChildAt(c)->setBeta(n->getBeta());
|
||||||
|
n->getChildAt(c)->setAlpha(n->getAlpha());
|
||||||
|
|
||||||
|
cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
||||||
|
|
||||||
|
minmax(n->getChildAt(c));
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() < min)
|
if(n->getChildAt(c)->getValue() < min)
|
||||||
{
|
{
|
||||||
min = n->getChildAt(c)->getValue();
|
min = n->getChildAt(c)->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(n->getChildAt(c)->getValue() < n->getBeta())
|
||||||
|
{
|
||||||
|
n->setBeta(n->getChildAt(c)->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(n->getBeta() <= n->getAlpha())
|
||||||
|
{
|
||||||
|
cout << "Alpha pruning." << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
n->setValue(min);
|
n->setValue(min);
|
||||||
cout << n->getValue() << endl;
|
//cout << n->getValue() << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +106,7 @@ void printTree(node n)
|
||||||
|
|
||||||
tuple<node, string> makeTree(string input, node n, int level)
|
tuple<node, string> makeTree(string input, node n, int level)
|
||||||
{
|
{
|
||||||
|
bool negate = false;
|
||||||
char tempc;
|
char tempc;
|
||||||
string tempn = "";
|
string tempn = "";
|
||||||
|
|
||||||
|
@ -88,12 +122,16 @@ tuple<node, string> makeTree(string input, node n, int level)
|
||||||
input = "";
|
input = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "testing" << endl;
|
|
||||||
// If the char is a number.
|
// If the char is a number.
|
||||||
if(isdigit(tempc))
|
if(isdigit(tempc))
|
||||||
{
|
{
|
||||||
tempn += tempc;
|
tempn += tempc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if(tempc == '-')
|
||||||
|
{
|
||||||
|
negate = true;
|
||||||
|
}
|
||||||
|
|
||||||
// If the char is a comma.
|
// If the char is a comma.
|
||||||
else if(tempc == ',')
|
else if(tempc == ',')
|
||||||
|
@ -102,10 +140,16 @@ tuple<node, string> makeTree(string input, node n, int level)
|
||||||
{
|
{
|
||||||
node tempnode;
|
node tempnode;
|
||||||
tempnode.setLevel(level);
|
tempnode.setLevel(level);
|
||||||
tempnode.setValue(stoi(tempn));
|
int negatemp = stoi(tempn);
|
||||||
|
if(negate)
|
||||||
|
{
|
||||||
|
negatemp = -negatemp;
|
||||||
|
}
|
||||||
|
tempnode.setValue(negatemp);
|
||||||
tempnode.setParent(&n);
|
tempnode.setParent(&n);
|
||||||
n.addChild(tempnode);
|
n.addChild(tempnode);
|
||||||
tempn = "";
|
tempn = "";
|
||||||
|
negate = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,10 +160,16 @@ tuple<node, string> makeTree(string input, node n, int level)
|
||||||
{
|
{
|
||||||
node tempnode;
|
node tempnode;
|
||||||
tempnode.setLevel(level);
|
tempnode.setLevel(level);
|
||||||
tempnode.setValue(stoi(tempn));
|
int negatemp = stoi(tempn);
|
||||||
|
if(negate)
|
||||||
|
{
|
||||||
|
negatemp = -negatemp;
|
||||||
|
}
|
||||||
|
tempnode.setValue(negatemp);
|
||||||
tempnode.setParent(&n);
|
tempnode.setParent(&n);
|
||||||
n.addChild(tempnode);
|
n.addChild(tempnode);
|
||||||
tempn = "";
|
tempn = "";
|
||||||
|
negate = false;
|
||||||
}
|
}
|
||||||
tuple<node, string> tempt(n, input);
|
tuple<node, string> tempt(n, input);
|
||||||
return tempt;
|
return tempt;
|
||||||
|
@ -192,6 +242,7 @@ int main(int argc, char **argv)
|
||||||
tree = makeTree(input.substr(1), root, 1);
|
tree = makeTree(input.substr(1), root, 1);
|
||||||
root = get<0>(tree);
|
root = get<0>(tree);
|
||||||
|
|
||||||
|
printTree(root);
|
||||||
minmax(&root);
|
minmax(&root);
|
||||||
|
|
||||||
printTree(root);
|
printTree(root);
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5)))
|
(((-2,-11,10),(2,15,13),(-1,-16,-16)),((-20,13,20),(-13,7,-19),(7,3,4)),((-20,-9,-14),(-4,-6,-12),(11,18,-3)))
|
||||||
|
|
|
@ -7,6 +7,8 @@ node::node()
|
||||||
parent = NULL;
|
parent = NULL;
|
||||||
level = 0;
|
level = 0;
|
||||||
value = 0;
|
value = 0;
|
||||||
|
alpha = INT_MIN;
|
||||||
|
beta = INT_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
node::node(node* n)
|
node::node(node* n)
|
||||||
|
@ -15,5 +17,7 @@ node::node(node* n)
|
||||||
level = n->getLevel();
|
level = n->getLevel();
|
||||||
value = n->getValue();
|
value = n->getValue();
|
||||||
children = n->getChildren();
|
children = n->getChildren();
|
||||||
|
alpha = n->getAlpha();
|
||||||
|
beta = n->getBeta();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define NODE_H
|
#define NODE_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -12,6 +13,8 @@ class node
|
||||||
int level;
|
int level;
|
||||||
int value;
|
int value;
|
||||||
vector<node> children;
|
vector<node> children;
|
||||||
|
int alpha;
|
||||||
|
int beta;
|
||||||
public:
|
public:
|
||||||
node();
|
node();
|
||||||
node(node *n);
|
node(node *n);
|
||||||
|
@ -25,6 +28,10 @@ class node
|
||||||
void addChild(node n) { children.push_back(n); };
|
void addChild(node n) { children.push_back(n); };
|
||||||
vector<node> getChildren() { return children; };
|
vector<node> getChildren() { return children; };
|
||||||
node* getChildAt(int i) { return &children.at(i); };
|
node* getChildAt(int i) { return &children.at(i); };
|
||||||
|
void setAlpha(int a) { alpha = a; };
|
||||||
|
int getAlpha() { return alpha; };
|
||||||
|
void setBeta(int b) { beta = b; };
|
||||||
|
int getBeta() { return beta; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Reference in a new issue