noip review of mathematics (4) - a combination of game

We consider this game a kind of combination in the topic:

(1) the two players take turns operating

(2) state of the game set is limited, and no matter how the parties go before the state will no longer appear. This ensures that the game ends in a finite number of steps.

(3) who lost who can not operate, such a rule to avoid a tie.

And we only consider fair game, that is, if a player can put the state A to B, another player can be. Chess is not a fair game, because White can move albino, and Black can not move albino.

State diagram: For the convenience of description, we can state the game is Fig. Each node is a state representative of each edge transition from one state to another operating state.

Note: the upper hand to win state (win state for short) refers to the upper hand there is a winning strategy, not just get to go no matter how can win.

This is not difficult to draw two rules:

Rule number one: a state is doomed to failure state if and only if all its successors are winning state.

Rule 2: a winning state is state if and only if it has at least one subsequent state is doomed to failure.

Exception: no successor state of the state is doomed to failure state.

Because the state diagram is acyclic, we can be judged in accordance with the reverse order topology, each node is determined when it is determined that all successors have been passed.

1.Ferguson game

Rule: There are two boxes, one box with a start sweets m, and n have the other sweets box, such a state referred to as the (m, n). Wherein each move to a box is emptied to another box to get some sugar emptied box, so that the two boxes each have at least one sugar. Obviously only the final state of (1,1). The last move of the game wins. Then the state is (m, n) is the upper hand to win or lose?

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;

const int maxn=100;
int winning[maxn][maxn];
int main()
{
    winning[1][1]=false;//如果它的后继状态为(1,1),那么这个人现在的状态为必败状态
    for(int k=3;k<20;++k)//我们设k=m+n,这里只对k<20的情况做出了处理
    {
        for(int n=1;n<k;++n)
        {
            int m=k-n;
            winning[n][m]=false;
            for(int i=1;i<n;++i)
            {
                if(!winning[i][n-i]) winning[n][m]=true;//把原来有m颗糖果的盘子清空
            }
            for(int i=1;i<m;++i)
            {
                if(!winning[i][m-i]) winning[n][m]=true;//把原来有n颗糖果的盘子清空
            }
        }
    }
    return 0;
}

2.Chomp! Game

Rule: a m * n chessboard, it can be removed every time a square grid and remove all its right and above. To get the lower left corner of the grid (1,1) are lost.

And m is given n, Q upper hand losing or winning.

Analysis: This question is a bit unexpected conclusion, in addition to (1, 1) is losing the upper hand, the other cases are to win the upper hand. Certify as follows:

If you win the flip, flip to say there is a winning strategy, so that the first time to take the upper hand no matter what stones, FLAC able to get the final victory. So now assume the upper hand to take the upper right corner stones (n, m), so that the next flip through some sort of borrowing from yourself into a situation to win, however, because the upper hand for the first time to take a (n, m) the stones, and no impact on any other stones, so you can just get a start on this before the election to FLAC win situation, with the assumption that contradiction.

3. about the number of games

Rule: There are 1 ~ n digits, two people are selected from a number of turns, and all the constraints it and wipe it. Wipe away the last of a number of people to win, ask who will win.

Analysis: This question and the question would be similar, regardless n take a few, they are the upper hand to win

If the flip win, assume the upper hand for the first time to take the number 1, then flip into the state through some kind of strategy to win, but 1 still have no effect on any other number, so you can just get in the way of escape before selection this number, enter win situation, with the assumption that contradiction.

4.Nim game

Rule: There are three groups of matches, respectively, a, b, c root, that is, the state (a, b, c). Every time a player can take away from any pile in at least one match, you can take the whole pile, but not both at the same time from multiple stacks of matches take in. Match can not take the player to lose.

Practice before the establishment of the state diagram, although correct, but unfortunately a high degree of complexity, as great a, b, c a lot of time, a state diagram of nodes and edges will be.

L.Bouton in 1902 when given such a theorem state (x1, x2, x3) is doomed to failure state, if and only if x1 xor x2 xor x3 = 0, xor herein is the exclusive OR, also known as as Nim and (Nim sum).

In fact, this theorem is suitable for any situation heap. , I.e. x1 xor x2 xor ... xor xn = 0 when the upper hand will be lost.

Proof: First of all, if each pile are zero matches, losing the upper hand;

Otherwise, first we need to prove that two conclusions:

(1) For the win state, there must be a successor state is doomed to failure.

Proof: Suppose Nim and is X> 0, 1 in the k-th bit, then there is a bit of the stack 1 and X leftmost binary. Suppose the number of matches this pile is Y, it is only necessary to take into Z = YxorX a match, and at this time the new Nim (first remove the original number of matches as Nim and for X xor Y xor (Y xor X) Y matches the pile, it becomes Z = Y xor X together into a match) = 0.

(2) For the losing state, we are all successor states win

Proof: Since you can only change a bunch of matches, regardless of which one it modified, Nim and the corresponding one who certainly is not zero, therefore, can not be doomed to failure state.

So far, Bouton theorems proved successful.

* Expansion: 5.Bash Game

A total of n items, each in turn removed from one article 1 ~ m, taking the last player to win items.

Analysis: If n = m + 1, the upper hand to take the k (1 <= k <= m), flip always take complete.

Further we find that if n-= K (m +. 1) + r, the upper hand to take of r, the subsequent fetch t (1 <= t <= m) th, and the upper hand and then remove the m + 1-t a, at this time the situation would becomes n-'= (. 1-K) (m + 1), next to continue such a way, regardless of how many flip taken, always put it into the upper hand (m + 1) is an integer multiple, will finally start to n = m + 1 of the situation, and the upper hand of the game at this time is equivalent to flip, so that when such a situation, there are upper hand winning strategy.

To summarize that if n% (m + 1) == 0, losing the upper hand, otherwise the upper hand to win.

And a combination of the game

Suppose there are n combination play G1, G2, ..., Gn, you can define a new game in each round, the current player may choose one of the sub-game Gi conduct a lawful operation, and let's keep the situation in other games unchanged, the player can not operate lose. This new game is called G1, G2, ..., Gn and.

A combination of the game and are usually very complex, in order to solve these problems, we use SG and SG function theorem.

SG function: for any state x, is defined SG (x) = mex (S), wherein mex (S) represents the smallest non-negative integer not within S. S is the set of successor states SG function value of x. For example there are three successor states x, SG values ​​of 0,1,1,2,4,7 function, the SG (x) = 3.

SG value thus apparently final state 0 (since S is an empty set), other values ​​can be calculated by recurrence, difficult to find, SG is 0 if and only if this state is doomed to failure state.

Single stack Nim game function satisfies SG SG (x) = x.

SG theorem: the game and the SG function is equal to the sub-function and SG Nim game. In this way, we can put various sub-game of divide and rule.

Example 1: turn pawn game

The meaning of problems: each grid has a piece on a board, each operation can easily select a piece upwardly (x, y) (x rows and y columns), to select a form such as (x, b), or (a, y) (where b <y, a <x) pieces, and then it (x, y) with inversion, the output operation can not be human.

Analysis: beginning with n pieces right side up, will be one of the pieces (x, y) seen piles of size x and y match, a total of 2 * n heap of matches, and to set up their Nim K. Select (x, b), and (a, y) is equivalent, for simplicity, we consider only selected (a, y).

If (a, y) side up, it is equivalent to deleting the original Nim x and, y, a, y, and to the new Nim K xor x xor y xor a xor y, as a <x, therefore, K = K xor (x xor a) corresponds to the original size of the heap of x matches, the match becomes the size of a stack, the stack is equivalent to deleting a match (XA) size.

If (a, y) tails, it is equivalent to deleting the x, y, increases a, y, is equivalent to deleting (XA) matches the size of the heap, the same equation as above.

Example 2: Division game

The meaning of problems: there is a matrix of n * m (1 <= n, m <= 50), that each element is a positive integer from 2 to 10,000. Two players take turns operating. Each row can be selected in one or more of an integer greater than 1, the number of each of them have become true factor thereof, e.g. 12 may become 1,2,3,4,6, inoperable people lose.

Analysis: final state is the number of all the matrix is 1, which is a losing status. Consider each containing a number of prime factors number (for example 12 = 2 * 2 * 3 contains three prime factors), so that a number of the "factor into its truth" is equivalent to remove one or more of its prime factor. Thus, each row corresponds to a match stack, each prime factor of the number as a match, this is equivalent to Nim game.

Guess you like

Origin www.cnblogs.com/iwillenter-top1/p/11616724.html