1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <stack>
using namespace std;
//------------------------------------------------
struct item
{
bool isOperator = false;
int op = 0;
int val = 0;
};
char opSymbol[] = { '+', '-', '*', '/' };
const int Nops = sizeof( opSymbol ) / sizeof( opSymbol[0] );
//------------------------------------------------
vector< vector<int> > allPermutations( int N ) // return all permutations of 0, 1, ..., N-1
{
vector< vector<int> > result;
vector<int> one( N );
iota( one.begin(), one.end(), 0 );
do
{
result.push_back( one );
} while( next_permutation( one.begin(), one.end() ) );
return result;
}
//------------------------------------------------
vector< vector<int> > allCollections( int N, int p ) // return all length-N collections of 0, ..., p-1
{ // (needs improvement!)
vector< vector<int> > result;
result.push_back( vector<int>{} );
for ( int pos = 0; pos < N; pos++ )
{
vector< vector<int> > temp = result;
result.clear();
for ( auto &v : temp )
{
v.push_back( 0 );
for ( int i = 0; i < p; i++ )
{
v[pos] = i;
result.push_back( v );
}
}
}
return result;
}
//------------------------------------------------
vector< vector<bool> > allChoices( int N, int r ) // return all choices of r from N
{ // (needs improvement!)
vector< vector<bool> > result;
result.push_back( vector<bool>( N, false ) );
for ( int choice = 1; choice <= r; choice++ )
{
vector< vector<bool> > temp = result;
result.clear();
for ( auto &v : temp )
{
int lastTrue = N - 1;
while( lastTrue >= 0 && !v[lastTrue] ) lastTrue--;
for ( int i = lastTrue + 1; i < N; i++ )
{
auto vv = v;
vv[i] = true;
result.push_back( vv );
}
}
}
return result;
}
//------------------------------------------------
bool evaluate( const vector<item> &rpn, int &result ) // work out an rpn expression
{
stack<int> S;
result = 0;
for ( auto &e : rpn )
{
if ( e.isOperator )
{
if ( S.empty() ) return false;
int op2 = S.top(); S.pop();
if ( S.empty() ) return false;
int op1 = S.top(); S.pop();
switch( e.op )
{
case 0: S.push( op1 + op2 ); break;
case 1: S.push( op1 - op2 ); break;
case 2: S.push( op1 * op2 ); break;
case 3:
{
if ( op2 == 0 ) return false;
S.push( op1 / op2 );
break;
}
}
}
else
{
S.push( e.val );
}
}
if ( S.size() != 1 ) return false;
result = S.top();
return true;
}
//------------------------------------------------
void output( const vector<item> &rpn ) // output an rpn expression
{
for ( auto &e : rpn )
{
if ( e.isOperator ) cout << opSymbol[e.op] << " ";
else cout << e.val << " ";
}
cout << '\n';
}
//------------------------------------------------
bool test( const vector<int> &V, int target, vector<item> &rpn ) // test numbers in V against target
{
int N = V.size();
int rpnLength = 2 * N - 1;
vector< vector<bool> > operatorPositions = allChoices( rpnLength - 3, N - 2 );
for ( auto &V : operatorPositions )
{
V.insert( V.begin(), 2, false ); // operator can't be in first two positions
V.push_back( true ); // operator must be in last position
}
vector< vector<int> > perms = allPermutations( N );
vector< vector<int> > ops = allCollections ( N - 1, Nops );
for ( auto &pos : operatorPositions ) // operator positions loop
{
for ( auto &op : ops ) // operator collection loop
{
rpn = vector<item>( rpnLength );
for ( int i = 0, n = 0; i < rpnLength; i++ )
{
if ( pos[i] )
{
rpn[i].isOperator = true;
rpn[i].op = op[n];
n++;
}
}
for ( auto &perm : perms ) // number permutation loop
{
for ( int i = 0, n = 0; i < rpnLength; i++ )
{
if ( !pos[i] )
{
rpn[i].val = V[perm[n]];
n++;
}
}
int ans;
if ( evaluate( rpn, ans ) && ans == target ) return true;
}
}
}
return false;
}
//------------------------------------------------
int main()
{
vector<int> V = { 1, 2, 3, 5, 6 };
int target = 45;
vector<item> rpn;
if ( test( V, target, rpn ) ) output( rpn );
else cout << "Impossible\n";
}
|