zoukankan      html  css  js  c++  java
  • hdu 2873 二维SG

    Bomb Game

    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.
    InputThere 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.OutputFor each test case, output one line, the name of the winner.Sample Input
    2 2
    .#
    ..
    2 2
    .#
    .#
    0 0
    Sample Output
    John
    Jack

    #是炸弹,引爆一个炸弹会在它的左边和上边产生新的炸弹,边界就不会了。(1,1)的炸弹不能引爆,John是先手,谁不能引爆炸弹就输了。

    很典型的SG函数模板题,不过是二维的。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N = 55;
     4 int sg[N][N], mex[N*100], n, m;
     5 char ch;
     6 int SG(int x, int y) {
     7     memset(mex, 0, sizeof(mex));
     8     for(int i = 0; i < x; i ++) {
     9         for(int j = 0; j < y; j ++) {
    10             mex[sg[x][j]^sg[i][y]] = 1;
    11         }
    12     }
    13     for(int i = 0; ; i ++) if(!mex[i]) return i;
    14 }
    15 int main() {
    16     for(int i = 0; i < N; i ++) sg[0][i] = sg[i][0] = i;
    17     for(int i = 1; i < N; i ++) {
    18         for(int j = 1; j < N; j ++) {
    19             sg[i][j] = SG(i,j);
    20         }
    21     }
    22     while(cin >> n >> m, n, m) {
    23         int ans = 0;
    24         for(int i = 0; i < n; i ++) {
    25             for(int j = 0; j < m; j ++) {
    26                 cin >> ch;
    27                 if(ch == '#') ans ^= sg[i][j];
    28             }
    29         }
    30         printf(ans?"John
    ":"Jack
    ");
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    解析json字符串
    python学习(十)文件操作方式
    python学习(九)集合
    python学习(八)内存地址及深/浅拷贝
    python学习(七)列表/字典合并、布尔型、交换变量值、列表转换字符串连接和分割
    python学习(六)字符串
    Python学习(五)字典
    python学习(四)数组,字符串,元组
    python学习(四)列表(数组)
    python学习(三)字符串格式化&strip的用法&小练习
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9113537.html
Copyright © 2011-2022 走看看