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 (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 integersx 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 |
Output for Sample Input |
2 1 1 0 2 2 5 3 5 |
Case 1: Bob Case 2: Alice |
题目大意:
有n个骑士(1<=n<=1000)在无限的棋盘中,给定n个骑士的坐标(xi,yi),(0<=xi,yi<500)。骑士每一步有六种走法(详见图片),最后不能移动骑士的算输,问先手胜还是后手胜。
解题思路:
这是一个SG函数的问题,因为 n 个骑士是相互独立的, 注意到骑士总是往左边走,所以在计算每一格的SG函数时可以按照row+col为定值依次计算。注意到骑士只有六种走法,所以对于每一格的SG函数值SG(x,y)不会超过6。
My Code:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1e3+5; int mat[6][2] = {{-2,1}, {1,-2},{-2,-1},{-1,-2},{-3,-1},{-1,-3}}; bool hash[MAXN][MAXN]; int sg[MAXN][MAXN]; int get_SG(int x, int y) { bool check[105]; if(hash[x][y]) return sg[x][y]; hash[x][y] = 1; memset(check, false, sizeof(check)); for(int i=0; i<6; i++) { int xx = x + mat[i][0]; int yy = y + mat[i][1]; if(xx>=0 && yy>=0) check[get_SG(xx,yy)] = 1; } for(int i=0; i<100; i++) if(!check[i]) return sg[x][y] = i; } int main() { int T; cin>>T; for(int cas=1; cas<=T; cas++) { int m, x, y; int ans = 0; scanf("%d",&m); while(m--) { scanf("%d%d",&x,&y); ans ^= get_SG(x,y); } if(!ans) printf("Case %d: Bob\n",cas); else printf("Case %d: Alice\n",cas); } return 0; }