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