Number Game (博弈) Gym - 101653Q

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章