Game of Hyper Knights(递归求sg)

Game of Hyper Knights

 1 seconds

 64 MB

Medium Hard

LOJ-1315

English

A Hyper Knight is like a chess knight except it has some special moves that a regular knight cannot do. Alice and Bob are playing this game (you may wonder why they always play these games!). As always, they both alternate turns, play optimally and Alice starts first. For this game, there are 6 valid moves for a hyper knight, and they are shown in the following figure (circle shows the knight).

Game of Hyper Knights(递归求sg)_第1张图片

They are playing the game in an infinite chessboard where the upper left cell is (0, 0), the cell right to (0, 0) is (0, 1) and the cell below (0, 0) is (1, 0). There are some hyper knights in the board initially and in each turn a player selects a knight and gives a valid knight move as shown in the picture. The player who cannot make a valid move loses. Multiple knights can go to the same cell, but exactly one knight should be moved in each turn.

You are given the initial knight positions in the board, you have to find the winner of the game.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.Each case starts with a line containing an integer n (1 ≤ n ≤ 1000) where n denotes the number of hyper knights. Each of the next n lines contains two integers x y (0 ≤ x, y < 500) denoting the position of a knight.

Output

For each case, print the case number and the name of the winning player.

Sample

Input Output

2 1 1 0 2 2 5 3 5

Case 1: Bob Case 2: Alice

 

 

#include
#include
#include
#include 
#include
#include
#include
#include
using namespace std;

int mex[8], sg[505][505];
int id[6][2] = { 1,-2, -1,-3,-1,-2,-2,-2,-3,-1,-2,1 };

int getsg(int x, int y)
{
    if (sg[x][y] != -1)
        return sg[x][y];
    memset(mex, 0, sizeof mex);
    for (int i = 0; i < 6; i++)
    {
        int tx = x + id[i][0];
        int ty = y + id[i][1];
        if(tx>=0&&ty>=0)
        mex[getsg(tx, ty)] = 1;
    }
    int ct = 0;
    while (mex[ct])
        ct++;
    sg[x][y] = ct;
    return sg[x][y];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for(int i=1;i<=t;i++)
    {
        memset(sg, -1, sizeof sg);
        int n;
        cin >> n;
        int ans = 0,x,y;
        while (n--)
        {
            cin >> x >> y;
            ans = ans xor getsg(x, y);
        }
        cout << "Case " << i << ": ";
        if (!ans)cout << "Bob" << endl;
        else cout << "Alice" << endl;
    }

}

你可能感兴趣的:(刷题,acm2023夏训,算法)