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).

Hyper Knight Moves

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
InputOutput

2 1 1 0 2 2 5 3 5

Case 1: Bob Case 2: Alice

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> 
#include<map>
#include<string.h>
#include<stdio.h>
#include<cstdio>
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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值