Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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>
#include <cstring>
using namespace std;
int next[][]={{-,},{,-},{-,-},{-,-},{-,-},{-,-}};
#define maxn 1005
int sg[maxn][maxn];
int dfs(int x,int y)
{
int vis[]={}; //注意该数组是一维的 表示该点后继的sg值的情况
if(sg[x][y]!=-)
return sg[x][y];
for(int i=;i<;i++)
{
int nx=x+next[i][];
int ny=y+next[i][];
if(nx>=&&ny>=) //注意不能不加符号就判断 等于0也是算在内的
vis[dfs(nx,ny)]=; //因为可能走到的点的sg值还没有求过 所以要用递归深搜
//之前写的非递归是因为之前的sg值都求过了 不用搜索也可以
}
for(int j=;j<;j++)
if(!vis[j]) return sg[x][y]=j;
}
int main()
{
memset(sg,-,sizeof(sg)); //这里定义成-1 比0 好 因为有的就是0 if的时候0还要再算一次浪费时间
int t,cas=;
cin>>t;
while(t--)
{
int n,x,y,ans=;
cin>>n;
for(int i=;i<n;i++)
{
cin>>x>>y;
ans^=dfs(x,y);
}
printf("Case %d: %s\n",cas++,ans?"Alice":"Bob");
}
return ;
}