Game of Hyper Knights ( SG博弈,DFS写法 )

Game of Hyper Knights ( SG博弈,DFS写法 )

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个棋子,两个人每次每人移动一个棋子,移动方式如图所示。问谁先手能赢。

思路:SG博弈,二维的sg函数,先手赢的条件是所有棋子的sg异或和不为0. 对于每一步棋,sg等于其余六部棋的sem。

注:其中via需要定义在函数里面,因为每一次调用都需要不同的via。

 

代码:

#include<bits/stdc++.h>

using namespace std;

int n;
int sg[1005][1005];
int nex[6][2] = { {-2,1},{-3,-1},{-2,-1},{-1,-2},{-1,-3},{1,-2} };

int getsg( int x, int y )
{
    int via[1005] = {0};
    if ( sg[x][y]!=-1 ) { // 如果之前求过,直接return
        return sg[x][y];
    }
    for ( int i=0; i<6; i++ ) {
        int xx = x + nex[i][0];
        int yy = y + nex[i][1];
        if ( xx>=0 && yy>=0 ) {
            via[getsg(xx,yy)] = 1;
        }
    }
    for ( int i=0;i<100 ;i++ ) {
        if ( via[i]==0 ) {
            return sg[x][y] = i;
        }
    }
}

int main()
{
    int ji=1,listt,i,j,x,y;
    cin >> listt;
    memset(sg,-1,sizeof(sg)); // DFS写法必须初始化为-1
    while ( listt-- ) {
        cin >> n;
        int ans = 0;
        for ( i=0; i<n; i++ ) {
            cin >> x >> y;
            ans = ans^getsg(x, y);  //  求 n个棋子的sg异或和
        }

        cout << "Case " << ji++ << ": ";
        if ( ans==0 ) {
            cout << "Bob" << endl;
        }
        else {
            cout << "Alice" << endl;
        }
    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值