"UVA1378" A Funny Stone Game (game-Calculate SG value-output scheme)

Source: Internet
Author: User

Topic

Description

The funny stone game is coming. There is n piles of stones, numbered with 0, 1, 2, ..., n−1. Both
Persons pick stones in turn. In every turn, each person selects three piles of stones numbered I, J, K
(I < J, J≤k and at least one stone left in pile i). Then, the person gets one stone out of pile I, and
Put one stone into pile j and pile K respectively. (Note:if j = k, it'll be the same as putting
Stones into pile j). One would fail if he can ' t pick stones according to the rule.
David is the player of the first picks stones and he hopes to win the game. Can Write a program
To help him?
The number of piles, n, does not exceed 23. The number of stones in each pile does not exceed 1000.
Suppose the opponent player is very smart and he'll follow the optimized strategy to pick stones.


Input
Input contains several cases. Each case has a lines. The first line contains a positive integer n
(1≤n≤23) indicating the number of piles of stones. The second line contains n non-negative integers
separated by blanks, S0, ..., sn−1 (0≤si≤1000), indicating the Number of stones in pile 0 to pile
n−1 respectively.
The last case was followed by a line containing a zero.
output
For each case, Output a line in the format ' Game t:i J K '. T was the case number. I, J and K indicates
which three piles David shall select at the first step if he wants to win. If There is multiple groups of
I, J and K, output the group with the minimized lexicographic order. If there is no strategies to win
The game, I, J and K is equal to '-1 '.


Sample Input
4
1 0 1 100
3
1 0 5
2
2 1
0
Sample Output
Game 1:0 2 3
Game 2:0 1 1
Game 3:-1-1-1

"topic translation"

David plays a stone game. In the game, there are n heaps of stones, numbered 0. N-1. Two players take turns taking stones. Each game, each player chooses 3 piles of stone i,j,k (i<j,j<=k, and at least one stone in the first heap), takes a stone from I, and puts a pebble into the j,k (if j=k puts 2 stones into the K). The first person who cannot take a stone loses.

Please program to help David.

The number of stones will not exceed 23, and no more than 1000 stones per heap.

Analysis

First of all, assuming that the first heap has XI a stone, then you can first put xi%2.

Because the two stones in the same heap are identical. What the other person does to this stone, you can do the same thing to another stone, the equivalent of the two stones do not exist.

Because each stone can be turned into two and put in the back, that is, its transfer state and its number.

Each stone can be regarded as a heap of stones, if it is the first heap of stones, the number of stones it represents is n-1-p.

Because the gravel heap is not interfering with each other, the game can be seen as consisting of several games with only a dozen stones. (consider it separately)

It can reach the sub-state of NIM and update its own SG values. Like sweeping the staircase, even if the number of stones is an even number, he may still be useful, that is, you can split the state into a balanced state, to consider this also. (as if not very clear, the specific look at the Code bar ~ ~)

The code is as follows: (see the wrong range of data, too lazy to change, on the sauce bar ~)

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7 using namespacestd;8 #defineMAXN 10109 Ten intN; One inta[2*maxn],b[2*maxn],sg[2*MAXN]; A BOOLvis[2*MAXN]; -  - voidGET_SG (intx) the { -memset (Vis,0,sizeof(Vis)); -      for(intI=1; i<x;i++) -       for(intj=i;j<x;j++) +      { -vis[sg[i]^sg[j]]=1; +      } A      for(intI=0; i<= -; i++)  at         if(vis[i]==0) {sg[x]=i; Break;} - } -  - BOOLOutputintXintNow ) - { -      for(inti=x-1; i>=1; i--) in       for(intj=i;j>=1; j--) -       if((sg[i]^sg[j]) = =Now ) to       { +printf"%d%d%d\n", n-x,n-i,n-j); -           return 1; the       } *     return 0; $ }Panax Notoginseng  - intMain () the { +     intKase=0; A      for(intI=1; i<= +; i++) Get_sg (i); the      while(1) +     { -scanf"%d",&n); $         if(n==0) Break; $         intans=0; -          for(intI=1; i<=n;i++) scanf ("%d",&a[i]); -          for(intI=1; i<=n;i++) b[i]=a[n-i+1]; the          for(intI=1; i<=n;i++) -         {Wuyi             if(b[i]%2==1) ans^=Sg[i]; the         } -printf"Game%d:",++Kase); Wu         if(ans==0) {printf ("-1-1 -1\n");Continue;} -         intmx=0; About          for(intI=0;(1<<i) <=ans;i++) $             if((1<<i) &ans) mx= (1<<i); -          for(inti=n;i>=1; i--)  -             if(b[i]!=0) {if(Output (I,ans^sg[i])) Break;} -     } A     return 0; +}
[UVA1378]

2016-04-17 17:12:38

"UVA1378" A Funny Stone Game (game-Calculate SG value-output scheme)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.