A typical game theory problem - Recursion + Memorized Search
http://justprogrammng.blogspot.com/2012/06/interviewstreet-challenges-permutation.html#.VlEMrvmrTIU
#include<iostream>
#include<set>
using namespace std; // Bit Ops Utils
int set(int a, int i)
{
return a | << i;
}
int unset(int a, int i)
{
return a & (~( << i));
}
int get(int a, int i)
{
return ((a&( << i)) >> i);
} //
int array[]; // for results record
int a[]; //
int play(int a[], int n, int r)
{
if (array[r] != -) return array[r]; // Check increase
int last = , i;
for (i = ; i < n; i++)
{
if (get(r, i))
{
if (a[i]>last) last = a[i];
else break;
}
}
if (i == n)
{
array[r] = ;
return ;
} // Recursively check each possibility
int rec[] = { , };
for (int i = ; i<n; i++)
{
if (get(r, i))
{
int ret = play(a, n, unset(r, i));
rec[ret] = ;
}
}
array[r] = rec[]; return array[r]; } int main()
{
int T;
cin >> T; while (T--)
{
for (int i = ; i < ; i++)
array[i] = -; int n; cin >> n;
for (int i = ; i < n; i++)
cin >> a[i]; int res = play(a, n, ( << n) - );
if (!res) cout << "Bob\n";
else cout << "Alice\n";
}
}