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");
    }
}
时间: 2024-10-23 13:42:50

LightOJ 1315 - Game of Hyper Knights(博弈sg函数)的相关文章

Light OJ 1315 - Game of Hyper Knights

传送门 1315 - Game of Hyper Knights    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB 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

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO,最后一个放的人赢,问谁赢 思路:sg函数,每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O即可 代码: #include <stdio.h> #include <string.h> const int N = 105; int t, sg[3][3][N]; char str[N]; int ge

HDOJ 5724 博弈SG函数

链接: http://blog.csdn.net/tc_to_top/article/details/51958964 题意: n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜 题解: 组合博弈问题,直接sg函数,因为列只有20,可以状压搞,枚举每个状态,找到该状态下可行的操作然后标记 代码: 31 int sg[1 << 21]; 32 int vis[21]; 33

(转)博弈 SG函数

此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------------------------------------------------------------------- 1.定义P-position和N-positon P表示Previous,N表示Next. 即上一个移动的人有必胜策略的局面是P-position,"先手必败"或&qu

Marbles(博弈SG函数)

Marbles Gym - 101908B Using marbles as a currency didn't go so well in Cubic?nia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace. Of course, the game uses marb

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int

Light OJ 1296 - Again Stone Game (博弈sg函数递推)

F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the