Coins in a Line
There are n coins in a line. (Assume n is even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.
Solution:
typedef pair<int, int> i_pair;
int path[100];
//define pair here
int max(int m, int n, int p, int q)
{
vector<int> i_v;
i_v.push_back(m);
i_v.push_back(n);
i_v.push_back(p);
i_v.push_back(q);
sort(i_v.begin(), i_v.end());
int max_path = *(i_v.end()-1);
return max_path;
}
int MaxPath(int depth, int a[], int s_z, int i, int j, int max_path)
{
if(s_z%2 != 0) return 0;
else
{
if(depth == s_z/2)
{
return max_path;
}
else
{
int m = a[i] + MaxPath(depth+1, a, s_z, i+2, j, max_path);
int n = a[i] + MaxPath(depth+1, a, s_z, i+1, j-1, max_path);
int p = a[j] + MaxPath(depth+1, a, s_z, i, j-2, max_path);
int q = a[j] + MaxPath(depth+1, a, s_z, i+1, j-1, max_path);
max_path = max(m,n,p,q);
int pos = depth * 2;
if(max_path== m)
{
path[pos] = i;
path[pos+1] = j;
}
else if(max_path == n)
{
path[pos] = i;
path[pos+1] = i+1;
}
else if(max_path == p)
{
path[pos] = j;
path[pos+1] = j-1;
}
else
{
path[pos] = j;
path[pos+1] = i;
}
}
}
}
浙公网安备 33010602011771号