Alice and Bob are playing a game on a line of N squares. The line is initially populated with one of each of the numbers from 1 to N. Alice and Bob take turns removing a single number from the line, subject to the restriction that a number may only be removed if it is not bordered by a higher number on either side. When the number is removed, the square that contained it is now
empty. The winner is the player who removes the 1 from the line. Given an initial configuration, who will win, assuming Alice goes first and both of them play optimally?
Input
Input begins with a line with a single integer T, 1 ≤ T ≤ 100, denoting the number of test cases. Each test case begins with a line with a single integer N, 1 ≤ N ≤ 100, denoting the size of the line. Next is a line with the numbers from 1 to N, space separated, giving the numbers in line order from left to right.
Output
For each test case, print the name of the winning player on a single line.
Sample Input
4
4
2 1 3 4
4
1 3 2 4
3
1 3 2
6
2 5 1 6 4 3
Sample Output
Bob
Alice
Bob
Alice
题意:1~n的全排列,Alice和Bob每次可以从中拿走一个数,谁拿走数字1,谁就获胜,一个数只有比它左右相邻的两个数字大,才可以拿走,拿走后,这个数变为0。Alice先手,在两个人足够聪明的前提下,谁会赢
前提:若后继状态全都为必胜态,则当前为必败态;后继状态只要有一个必败态,当前为必胜态
dp[ l ][ r ][ left ] 表示当前在 l 到 r 包含数字1的独立的区间,这个区间左右相邻的两个数都为0,left表示这个区间外剩余数字的个数;值为1表示必胜态,值为0表示必败态(要保证当前的 [l ,r] 区间包含数字1)
dp[ l ][ r ][ left ] 的后继状态有(以下默认 i, l, r 可以拿走,即a[i]>a[i-1]&&a[i]>a[i+1]):
dp[ l +1][ r ][ left ]
dp[ l ][ r-1 ][ left ](可被拿的左右端点一定不是1)
dp[ l ][ i-1 ][ left+r-i ](数字1在 i 的左边)
dp[ l+1 ][ r ][ left+i-l ](数字1在 i 的右边)
dp[ l ][ r ][ left-1](left>0)
临界状态dp[ l ][ r ][ left ] = 1(l == r)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 109;
int dp[maxn][maxn][maxn];
int a[maxn];
int pos;
int dfs(int l,int r,int lef)
{
if(l==r)
return dp[l][r][lef] = 1;
if(dp[l][r][lef]>=0)
return dp[l][r][lef];
int ans = 0;
for(int i = l; i<=r; i++)
{
if(i==l&&a[i]>a[i+1])
{
ans = ans|(dfs(l+1,r,lef)^1);
}
if(i==r&&a[i]>a[i-1])
{
ans = ans|(dfs(l,r-1,lef)^1);
}
else
{
if(a[i]>a[i-1]&&a[i]>a[i+1])
{
if(i>pos)
ans = ans|(dfs(l,i-1,lef+r-i)^1);
else
ans =ans|(dfs(i+1,r,lef+i-l)^1);
}
}
}
if(lef>0)
ans =ans|(dfs(l,r,lef-1)^1);
return dp[l][r][lef] = ans;
}
int main()
{
int i,j,m,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(dp,-1,sizeof(dp));
for(i = 1; i <=n; i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
pos = i;
}
a[n+1] = a[0] = 0;
printf("%s\n",dfs(1,n,0)?"Alice":"Bob");
}
return 0;
}