Game Theory preliminary

Foreword

"Game Theory" I've heard a lot of this algorithm, a year ago had been heard, but I have not, today is simple to learn game theory preliminary.

Game Theory In general, the form of the problem is that two people were some kind of game, the two men are very smart, you want to win, a draw game theory generally does not occur.

concept

  • Situation: the current status of the game

  • Losing situation: Operation to this situation people will fail, only by win situation transferred from.

  • Win situation: the operation to this situation who will win, it can only be transferred from a losing situation.

Preliminary Game

Nim Game

Nim Game is one of the most basic game theory problem.

Description of the problem: There are two players \ (A \) and \ (B \) together to play a game, a total of \ (N \) heap of stones, gravel each stack has \ (a [i] \) one, two people take turns stones, each can be removed any more than a pile of stones from arbitrary, but can not take, and when a person turn to take the stones, if he can no longer be operated (that is, the stones are taken over), he was lost, the game ends. To stack and count the number of each pile of stones set, two people want to take the optimal solution, ask whether there exist upper hand to win.

Analysis: The answer is, find every pile of stones and XOR, if \ (0 \) , losing the upper hand, if it is not \ (0 \) , the upper hand to win.


prove:

The first set \ (I \) stones piled there \ (a [i] \) a, then the situation is losing all the stones have been fetched, then \ (a [1] * a [2] * ... . * A [n-] = 0 \) .

I. If you are currently in a situation, this situation \ (A [1] * A [2] * ... * [the n-] A = the X-! = 1 \) (that is, win situation), then we use different or nature, while both sides of the equation or a different \ (X \) , can be obtained: \ (a [. 1] * a [2] * ... * [n-] a = X ^ X ^ X = 0 \ ) , that is to say,

We can \ (a [i] \) found in a number of \ (X \) XOR, the situation eventually turned \ (a [1] * a [2] * ... * [n] a = 0 \) , free to find this number, as long as \ (X ^ A [I] <= A [I] \) , then we can remove the stones from a heap \ (a [i] -x ^ a [ I] \) a stone, whereby

The next situation XOR and equal to \ (0 \) . XOR and we will not \ (0 \) is called win situation in the state, the first is because the current players may continue to operate, and the second is due to consider the optimal solution, it should turn a losing situation situation.

II. If you are in a situation, this situation \ (A [1] * A [2] * ... * A [the n-] = 0 \) (that is doomed to failure situation), then we take the current players, no matter how will turn XOR and not \ (0 \) situation that is win situation, for example, if an item is currently \ (a [i] \) is our

Becomes \ (A [J] \) , since \ (A [I]! = A [J] \) , satisfying \ (a [1] * ... * a [i] * ... a [n] = 0 \) in the case of no longer satisfies \ (a [. 1] * ... * a [J] * ... [n-] a = 0 \) , the case will be turned from the situation of losing win situation by re-entering I II case situation.

In summary, if the initial state is a win situation, so people in this situation to win, otherwise doomed to failure. <\ Font>

This is the basis of \ (Nim \) game.

Take a match game

#include<cstdio>
using namespace std;
int n,a[500005],maxn;
int main()
{
    int ans=0,add;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ans^=a[i];
    if(!ans){printf("lose\n");return 0;}
    for(int i=1;i<=n;i++)
    {
        if((ans^a[i])<=a[i])
        {
            printf("%d %d\n",a[i]-(ans^a[i]),i);
            for(int j=1;j<=n;j++)
            {
                if(j==i){printf("%d ",ans^a[i]);continue;}
                printf("%d ",a[j]);
            }
            break;
        }
    }
    return 0;
} 

QED.

Game Bash

Sleepy, tomorrow morning to write.

Guess you like

Origin www.cnblogs.com/valentino/p/11854431.html