zoukankan      html  css  js  c++  java
  • LightOJ 1186 Icreable Chess(Nim博弈)

    You are given an n x n chess board. Only pawn is used in the 'Incredible Chess' and they can move forward or backward. In each column there are two pawns, one white and one black. White pawns are placed in the lower part of the board and the black pawns are placed in the upper part of the board.

    The game is played by two players. Initially a board configuration is given. One player uses white pieces while the other uses black. In each move, a player can move a pawn of his piece, which can go forward or backward any positive integer steps, but it cannot jump over any piece. White gives the first move.

    The game ends when there is no move for a player and he will lose the game. Now you are given the initial configuration of the board. You have to write a program to determine who will be the winner.

     

    Input

    Input starts with an integer T (≤ 200), denoting the number of test cases.

    Each case starts with an integer n (3 ≤ n ≤ 100) denoting the dimension of the board. The next line will contain nintegers, W0, W1, ..., Wn-1 giving the position of the white pieces. The next line will also contain n integers, B0, B1, ... Bn-1 giving the position of the black pieces. Wi means the row position of the white piece of ith column. And Bi means the row position of the black piece of ith column. You can assume that (0 ≤ Wi < Bi < n) for (0 ≤ i < n) and at least one move is remaining.

    Output

    For each case, print the case number and 'white wins' or 'black wins' depending on the result.

    Sample Input

    2

    6

    1 3 2 2 0 1

    5 5 5 3 1 2

    7

    1 3 2 2 0 4 0

    3 4 4 3 1 5 6

    Sample Output

    Case 1: black wins

    Case 2: white wins

    题解:对于黑白棋,一个后多少退则另一个跟进多少即可,因此只考虑往前的情况,这样就可以把黑白棋之间的距离看成石子的数量,就转化为n堆石子,依次去取的NIM博弈;

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=110;
     4 int a[maxn],b[maxn];
     5 int T,n,ans,cnt;
     6 
     7 int main()
     8 {
     9     scanf("%d",&T);
    10     for(int cas=1;cas<=T;cas++)
    11     {
    12         ans=cnt=0;
    13         scanf("%d",&n);
    14         for(int i=1;i<=n;++i) scanf("%d",&a[i]);
    15         for(int i=1;i<=n;++i) scanf("%d",&b[i]);
    16         for(int i=1;i<=n;++i)
    17         {
    18             a[i]=b[i]-a[i]-1; 
    19             ans^=a[i];
    20             //if(a[i]==1) cnt++;
    21             //else ans^=a[i];    
    22         } 
    23     
    24         if(!ans) printf("Case %d: black wins
    ",cas);
    25         else printf("Case %d: white wins
    ",cas);
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    Centos7下PyCharm2019安装
    企业的web项目类型、企业项目开发流程、立项申请阶段、需求分析、导航菜单、轮播图、退出登录、登录注册、课程列表、创建虚拟环境、依赖包安装、创建项目、调整目录、创建代码版本git、日志配置、异常处理、创建数据库、
    增量式爬取阳光热线网
    分布式爬取阳光热线网
    什么是接口测试,为什么做接口测试
    什么是接口、接口优势、类型(详解)
    187. Repeated DNA Sequences
    274. H-Index
    299. Bulls and Cows
    36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/csushl/p/10388759.html
Copyright © 2011-2022 走看看