LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

G - Game of Hyper Knights
Time Limit:1000MS      Memory Limit:32768KB      64bit IO Format:%lld & %llu
Submit  Status

Description

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

 

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

Now 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

2

1

1 0

2

2 5

3 5

Sample Output

Case 1: Bob

Case 2: Alice

题意:有n个骑士(1<=n<=1000)在无限的棋盘中,给定n个骑士的坐标(xi,yi),(0<=xi,yi<500)。
骑士每一步有六种走法,最后不能移动骑士的算输,问先手胜还是后手胜。
思路:n个骑士相互独立,所以可以应用SG定理。
其一,注意到骑士总是往左边走,并且,是在当前对角线的左边,所以在计算每一格的SG函数时可以按照row+col为定值依次计算。
其二,注意到骑士只有六种走法,所以对于每一格的SG函数值SG(x,y)不会超过6。

 
    
#include<iostream>
#include<cstdio>
using namespace std;
int nxt[6][2] = { {-2,1}, {1,-2},{-2,-1},{-1,-2},{-3,-1},{-1,-3}};
#define mx 2000
bool on[mx][mx];
int sg[mx][mx];
int calcsg(int xx, int yy){
    int tmp[101];
    int a,b,c,d,e,f,g,h,x,y,z;
    if(on[xx][yy] != 0)
        return sg[xx][yy];
    on[xx][yy] = 1;
    for(x = 0; x < 100; x++)
        tmp[x] = 0;
    for(x = 0; x < 6;x++){
        a = xx + nxt[x][0];
        b = yy + nxt[x][1];
        if(a >= 0 && b >= 0)
            tmp[calcsg(a,b)] = 1;
    }
    for(x = 0; x < 100; x++)
        if(tmp[x] == 0)
            return sg[xx][yy] = x;
}
int main(){
    int a,b,c,d,e,f,g,h, x, y,z ;
    scanf("%d", &a);
    for(z = 1; z <= a; z++){
        cin >> b;
        c = 0;
        for(x = 0; x < b; x++){
            cin >> d >> e;
            c ^= calcsg(d,e);
        }
        printf("Case %d: %s\n",z,c?"Alice":"Bob");
    }
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值