Bomb Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 806    Accepted Submission(s): 392

Problem Description

John and Jack, two mathematicians, created a game called “Bomb Game” at spared time. This game is played on an n*m chessboard. A pair of integers (p, q) represents the grid at row p, column q. Some bombs were placed on the chessboard at the beginning. Every round, a player can choose to explode a bomb located at (p, q), and the exploded bomb will disappear. Furthermore:

1.If p>1 and q>1, the bomb will split up into two bombs located at (u, q) and (p, v), u<p, v<q, u and v are chosen by the player.

2.If p=1 and q>1, one new bomb will be produced, located at (p, v), v<q, v can be chosen freely by the player.

3.If q=1 and p>1, one new bomb will be produced, located at (u, q), u<p, u can be chosen freely by the player.

If two bombs located at the same position or a bomb located at (1, 1), they will be exploded automatically without producing new bombs.

Two players play in turn, until one player cannot explode the bombs and loses the game.

John always plays first.

Now, we’ll give you an initial situation, and you should tell us who will win at last. Assume John and Jack are smart enough, and they always do the best choice.

Input

There are several test cases, each one begins with two integers n and m, 0<n, m<=50, represents the number of rows and columns. Following by an n*m grid, describing the initial situation, ‘#’ indicates bomb.

The input is terminated by n=m=0.

Output

For each test case, output one line, the name of the winner.

Sample Input

2 2
.#
..
2 2
.#
.#
0 0

Sample Output

John
Jack

题意:给定n*m的棋盘,棋盘中有炸弹,每进行一次操作炸弹炸一次,炸一次生成两个炸弹,分别位于左方和上方(左或上是边界则不生成),炸完之后原炸弹消失,两人轮流操作,最后不能引爆的输。

思路:

一维的情况,等价于多堆取石子的游戏,sg值即石子数,本题中也就是到1,1的距离。

二维时,引爆每个炸弹后会产生两个新的炸弹,而这个炸弹的sg即可看做新产生的两个炸弹的sg的异或(即"NIM和"),这样只要修改一下一维求sg的函数,变可以构造出二维的sg函数表,对应有炸弹的位置的sg值异或起来就可以判定胜负。

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int sg[55][55];
int get_sg(int x,int y)
{
bool mex[1111];
memset(mex,0,sizeof(mex));
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
mex[sg[x][j]^sg[i][y]]=1;
for(int i=0;;i++)
if(!mex[i])
return i;
}
void init()
{
for(int i=0;i<55;i++)
sg[i][0]=i,//i行上方石子总数
sg[0][i]=i;//i列左边石子总数
for(int i=1;i<55;i++)
for(int j=1;j<55;j++)
sg[i][j]=get_sg(i,j);
}
int main()
{
init();//打表
int x,y;
while(~scanf("%d%d",&x,&y)&&x&&y)
{
int sum=0;
char s[55][55];
for(int i=0;i<x;i++)
{
scanf("%s",s[i]);
for(int j=0;j<y;j++)
if(s[i][j]=='#')
sum^=sg[i][j];
}
if(sum)
printf("John\n");//先手赢
else
printf("Jack\n");//后手赢
}
return 0;
}

HDU2873 Bomb Game(二维SG函数)的更多相关文章

  1. Matlab绘制三维曲面(以二维高斯函数为例)

    原文地址为:Matlab绘制三维曲面(以二维高斯函数为例) 寒假学习了一下Python下的NumPy和pymatlab,感觉不是很容易上手.来学校之后,决定继续看完数字图像处理一书.还是想按照上学期的 ...

  2. 一个不错的PHP二维数组排序函数简单易用存用

    一个不错的PHP二维数组排序函数简单易用存用 传入数组,传入排序的键,传入排序顺序 public function array_sort($arr,$keys,$type='asc') { $keys ...

  3. Matlab 二维绘图函数(plot类)

    plot 功能 绘制二维图形的最基本函数. 语法 //x为向量时,以x的元素值为纵坐标,x的序号为横坐标绘制曲线. //x为矩阵时,以其序号为横坐标,按列绘制每列元素值相对于其序号的曲线. polt( ...

  4. PHP二维数组排序函数

    PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义. 以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义: functi ...

  5. PHP 二维数组排序函数的应用 array_multisort()

    <?php $arrayData = array( array("name"=>"泰山", "age"=>"23 ...

  6. [图形学] Chp8.4 OpenGL 二维观察函数——视口

    这节有几个显示窗口的控制函数,可以调整视口,创建子窗口,最小化为图标,设置图标名称,隐藏显示等. gluOrtho2D (xwmin, xwmax, ywmin, ywmax); // 定义二维裁剪窗 ...

  7. POJ 2311 Cutting Game(二维SG+Multi-Nim)

    Cutting Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4798   Accepted: 1756 Desc ...

  8. C语言学习笔记 (005) - 二维数组作为函数参数传递剖析

    前言 很多文章不外乎告诉你下面这几种标准的形式,你如果按照它们来用,准没错: //对于一个2行13列int元素的二维数组 //函数f的形参形式 f(int daytab[2][13]) {...} / ...

  9. C语言 memcpy二维数组的复制

    今天在实现二维数组的复制功能时,竟然出现了好多问题,还是太不小心了. 我们知道,平时进行矩阵复制,无非是二重循环进行赋值操作,所以今天想改用利用memcpy进行复制操作,当然一维数组的复制在上一篇文章 ...

随机推荐

  1. 函数和常用模块【day05】:迭代器(六)

    本节内容 1.简书 2.可迭代对象 3.迭代器 4.rang方法 5.总结 一.简述 我们经常使用for循环去遍历一些序列数据,但是我们有的时间发现for循环的效率很低,而且很占用了大量的硬件资源,但 ...

  2. idea常用的插件

    ignore 插件 可以自动生成.ignore文件   非常的实用 gitee 插件 搜所gitee安装即可  码云的插件 maven helper 插件 idea 中解决maven 包冲突的问题 a ...

  3. Kafka记录-常用命令选项说明

    1.kafka-topics  主题 如:kafka-topics --create --zookeeper 10.0.4.142:2181/kafka --replication-factor 1 ...

  4. vue自学入门-2(vue创建项目)

    本人也是刚学习VUE,边找资料,边学习,边给大家分享.1.创建项目 2.启动项目 3.注意上面和下面全部用cnpm

  5. TCP/IP详解 卷1 第十八章 TCP的建立与终止

    第十八章 TCP的建立与终止 tcpdump Tcpdump可以将网络中传送的数据报完截获下来进行分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句来帮助你去掉 ...

  6. Spring MVC 源码分析

      Spring MVC SpringMVC中扮演关键角色的DispatcherServlet类. 1 DispatcherServlet 1.1DispatcherServlet 类图 1.2 初始 ...

  7. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

  8. js代码解析原则

    js引擎在读取js代码时会进行两个步骤,第一个步骤是解释,第二个步骤是执行. 解释就是先通篇扫描所有的Js代码,然后把所有声明提升到顶端,第二步是执行,执行就是执行代码的操作. 例: 例子1: < ...

  9. BackBone结合ASP.NET MVC实现页面路由操作

    1. 问题的背景 什么是页面路由操作,就是通过浏览器地址栏的标记来实现页面内部的一些操作,这些操作具有异步性和持久性.应用场景主要有页面操作过程中的添加收藏夹的操作.后退操作等过程中能完全恢复界面. ...

  10. 使用密钥认证机制远程登录Linux

    密钥认证机制 创建存放key的文件 1)创建目录 /root/.ssh 并设置权限 [root@localhost ~]# mkdir /root/.ssh mkdir 命令用来创建目录,以后会详细介 ...