#include using namespace std; const int MAXSIZE =8; const char LEAF = 'L'; const int PARENCOST = 1; const int INVCOST = 2; const int IMPOSSIBLE = 1000000; int original[MAXSIZE]; struct node { int val; char op; node *left, *right; }; void printTree(node *tree) { if (tree->op == LEAF) cout << tree->val; else { cout << '('; printTree(tree->left); cout << ')' << tree->op << '('; printTree(tree->right); cout << ')'; } } int countParens(node *tree) { if (tree->op == LEAF) return 0; else { int count = countParens(tree->left) + countParens(tree->right); if (tree->op == '-' && tree->right->op == '-') count++; else if (tree->op == '*' || tree->op == '/') { if (tree->left->op == '+' || tree->left->op == '-') count++; if (tree->right->op == '+' || tree->right->op == '-') count++; if (tree->op == '/' && tree->right->op == '/') count++; } return count; } } int numParens(node tree) { int count = countParens(&tree); // printTree(&tree); // cout << ' ' << count << endl; return count; } int find24(node tlist[], int numTerms) { //cout << "here, numTerms = " << numTerms << endl; if (numTerms == 1) { if (tlist[0].val == 24) return numParens(tlist[0]); else return IMPOSSIBLE; } else { int cost = IMPOSSIBLE; for(int i=1; i= i; k--) buildList[k+1] = buildList[k]; buildList[i] = tmp; } return count; } int permute(int list[], int index, int n) { if (index == n) { int numInv = numInversions(list, n); //for(int i=0; i> n; n = 4; for(int i=0; i> list[i]; for(int i=0; i= IMPOSSIBLE) cout << "impossible" << endl; else cout << ans << endl; }