LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
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 ;
}
LightOJ 1315 - Game of Hyper Knights(博弈sg函数)的更多相关文章
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- hdu 3032(博弈sg函数)
题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...
- HDU-4678 Mine 博弈SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...
- (转)博弈 SG函数
此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...
- 尼姆博弈+SG函数
博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...
- 【转】博弈—SG函数
转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...
- HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)
Fibonacci again and again Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeansException
Spring4.X配置一个简单的读取sping的xml的配置文件结果
- 【bzoj1502】 NOI2005—月下柠檬树
http://www.lydsy.com/JudgeOnline/problem.php?id=1502 (题目链接) 今天考试题,从来没写过圆的面积之类的东西..GG 题意 一颗树由n个圆台组成,现 ...
- C#获取局域网中的所有正在使用的IP地址
方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 求DAG上两点的最短距离
Problem 给出一个不带边权(即边权为1)的有向无环图(unweighted DAG)以及DAG上两点s, t,求s到t的最短距离,如果无法从s走到t,则输出-1. Solution DFS,BF ...
- 安装make命令
步骤1:通过root用户将两个iso源上传到被测试服务器的/opt/huawei/software/iso目录下. # mkdir -p /opt/huawei/software/iso 挂载iso源 ...
- Socket 入门- 客户端回射程序
结果输出:------------------------------------------------------客户端:xx@xxxxxx:~/Public/C$ ./postBackCli.o ...
- apache AddDefaultCharset
AddDefaultCharset 指令 说明 当应答内容是text/plain或text/html时,在HTTP应答头中加入的默认字符集 语法 AddDefaultCharset On|Off|ch ...
- JNI环境变量——JNIEnv*的使用 &&配置jd环境变量
如果没有配置环境变量,先配置环境变量,如下: 1.右键我的电脑——高级——环境变量——下面的系统变量 2.选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HO ...
- 使用 GDB 调试多进程程序
使用 GDB 调试多进程程序 GDB 是 linux 系统上常用的调试工具,本文介绍了使用 GDB 调试多进程程序的几种方法,并对各种方法进行比较. 3 评论 田 强 (tianq@cn.ibm.co ...
- 你无法修改 Git 的历史记录
转自:http://www.oschina.net/news/26241/you-can-not-change-git-history 有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 ...