Prerequisite knowledge: Introduction to Game Theory
This article discusses the two-player zero-sum game.
In a zero-sum game, the sum of the two players' payoffs is always zero, so one player's gain necessarily means a loss for the other. A zero-sum game can be viewed as a special case of a constant-sum game. Conversely, any constant-sum game can be converted equivalently into a zero-sum game by adding or subtracting a constant from one player's payoff, so it suffices to discuss zero-sum games.
In algorithm competitions, common zero-sum games can roughly be divided into sequential zero-sum games and simultaneous zero-sum games.
Sequential Zero-Sum Games
In a sequential zero-sum game, the two players take turns acting until the game ends.
The payoff function of a sequential zero-sum game has a recursive structure. A game state can be divided into three types: terminal states , states where player acts, and states where player acts. Suppose that at a terminal state , player receives and player receives . Therefore, when it is player 's turn, maximizing that player's payoff is equivalent to minimizing player 's payoff. If both players use optimal strategies, the maximum payoff player can obtain at state , denoted by , satisfies the following recurrence:
Here, means that is a successor state of . This is the minimax principle.
When applying this algorithm to an actual problem, the following approaches are common:
-
If the game has only a small number of states, implement the algorithm by brute force.
-
If the game has many states and no special structure, consider Alpha–Beta pruning together with other search-pruning methods.
-
If a single game state is often the successor of multiple states, use memoized search or another dynamic-programming algorithm to avoid repeated searches.
-
If a player's final payoff is the sum of the payoffs of all actions before the game ends, the model can be optimized. Specifically, suppose that at terminal state , player takes the action sequence , action has payoff , and player 's payoff function is
Let be the maximum score the player to move can obtain in the game after state . For the initial state , , so finding is enough to solve the original problem. For , the recurrence is:
Here, is an action that moves the state from to . If several such actions exist, choose the one with the largest payoff .
-
Every impartial combinatorial game is a sequential zero-sum game; assign payoffs and to the winning and losing sides. The recurrence for is then exactly the lemma for deciding winning and losing states.
A common variant asks for the minimum number of turns needed by the winner and the maximum number of turns for which the loser can survive. When BFS is started from terminal states and the lemma is used to classify states, record the BFS round in which each winning or losing state is classified; these are the required turn counts. A winning state needs only one losing successor, so it is reached from the losing successor with the smallest round number. A losing state requires all successors to be winning, so it is reached from the winning successor with the largest round number.
The same method extends to general directed-graph games.
Example
Codeforces 794 E. Choosing Carrot
Given a sequence of length , two players and alternately remove a number from one of its ends until only one number remains. Player wants to maximize the remaining number, while player wants to minimize it. Before the game officially starts, player may make moves first. Assuming both players use optimal strategies throughout, find the final remaining number for every . Here, .
Solution
Regardless of how the players remove numbers, the remaining sequence is always a contiguous interval. Thus a game state is fully described by this interval and the player whose turn it is, so dynamic programming applies. Let be the final remaining number for the state . As discussed above, when it satisfies:
The terminal condition is . Therefore all possible states can be evaluated in time. For each , the answer is
This algorithm is too slow for the constraints of the original problem, so we need to optimize the transitions. There are several ways to do this; we present only one.
View the transitions as operations on the whole sequence. The two recurrences replace each pair of adjacent numbers by its maximum or minimum; call them the maximization operation and the minimization operation, respectively. Each operation decreases the sequence length by one. There are intervals of length , corresponding to the sequence obtained after operations. To obtain , the last operation must be a maximization operation, so every relevant operation sequence ends with maximization.
Consider the effect of two consecutive operations. Suppose the minimization operation is performed first, followed by maximization. The three numbers become
Examining all possible relative orders of shows that this expression equals except when and , that is, when is a strict local maximum. In other words, if a sequence has no strict local maximum, two consecutive operations only remove one element from each end. This greatly simplifies the transitions. The remaining question is how to ensure that the sequence has no strict local maximum. A single maximization operation is enough to guarantee this. Therefore, this results after an even number of operations can be obtained by applying two operations to the initial sequence and then repeatedly removing one element from each end; results after an odd number of operations are obtained by applying one operation and then repeatedly removing one element from each end.
The complete sequence needs at most operations, and the subsequent answer collection takes only passes, so the total time complexity is .
this concept
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int& x : a) std::cin >> x;
std::vector<int> ans(n), tmp;
tmp = a;
for (int i = 0; i < n - 1; ++i) {
tmp[i] = std::max(tmp[i], tmp[i + 1]);
}
for (int l = n / 2 - 1, r = (n - 1) / 2, ma = 0; l >= 0; --l, ++r) {
ma = std::max({ma, tmp[l], tmp[r]});
ans[r - l] = ma;
}
tmp = a;
for (int i = 0; i < n - 1; ++i) {
tmp[i] = std::min(tmp[i], tmp[i + 1]);
}
for (int i = 0; i < n - 2; ++i) {
tmp[i] = std::max(tmp[i], tmp[i + 1]);
}
for (int l = (n - 3) / 2, r = n / 2 - 1, ma = 0; l >= 0; --l, ++r) {
ma = std::max({ma, tmp[l], tmp[r]});
ans[r - l] = ma;
}
ans[n - 1] = *std::max_element(a.begin(), a.end());
for (auto x : ans) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
Exercises
- Luogu P2734 [USACO3.3] Game A Game
- Luogu P4576 [CQOI2013] Board Game
- Luogu P7097 [yLOI2020] Silk Play
- Codeforces 388 C. Fox and Card Game
- Codeforces 794 E. Choosing Carrot
- Codeforces 1628 D2. Game on Sum (Hard Version)
- Luogu P3210 [HNOI2010] Stone-Taking Game
Simultaneous Zero-Sum Games
In a simultaneous zero-sum game, the two players act at the same time.
A simultaneous zero-sum game is usually represented by a payoff matrix. Let player have action set . If players choose , their payoffs are and , respectively.
Example
Consider rock–paper–scissors. Suppose a win is worth , a loss is worth , and a draw is worth . The two players' payoffs can then be written as
A general two-player simultaneous game can be represented in the same form and is therefore also called a bimatrix game. In a zero-sum game, player 's payoff matrix is the negative of player 's, so it is enough to consider player 's payoff matrix:
The problem is: given the payoff matrix , how can we find the optimal strategies and maximum payoffs of the two players?
Mixed Strategies
In contrast to sequential zero-sum games, the roles of the two players in a simultaneous game are symmetric. Since we have already solved the sequential version, suppose first that player acts before player . Then, by the preceding discussion, player 's payoff at the end of the game is
This is the worst payoff player can obtain because player can observe player 's action. Conversely, if player acts first, player 's payoff is
This is the best payoff player can obtain because player 's action is visible to player . Thus player should expect a payoff in the actual simultaneous game. Although always holds (see the proof of the weak duality theorem), equality need not hold, so the sequential-game analysis generally cannot determine a unique outcome.
Continuing the example
In rock–paper–scissors, if the two moves are made in sequence, the first player always loses and the second player always wins. In mathematical terms, this is
Here, , so the two bounds are not equal.
The preceding analysis missed a key feature of simultaneous games: a player cannot accurately predict the opponent's action. Formally, this means that both players may use randomized strategies. This idea does not help in a sequential game, because the second player can always observe the first player's action and respond accordingly. In a simultaneous game, however, the strategic uncertainty introduced by randomization prevents the opponent from targeting a specific action effectively.
Continuing the example
In rock–paper–scissors, suppose player chooses rock, paper, or scissors uniformly at random. Depending on player 's action, player 's possible expected payoffs are
Thus player 's expected payoff is always , regardless of player 's action. This is clearly better than committing to a single deterministic action.
This motivates the definition of a mixed strategy.
Mixed Strategy
In a simultaneous game, a mixed strategy for player is a function satisfying . In other words, is a probability distribution on the action set . The set of all mixed strategies for player is denoted by ; here, denotes the set of all probability distributions on . For player , if is a degenerate distribution, meaning that some satisfies , then is called a pure strategy.
The payoff of mixed strategies is the expected payoff of a single pair of actions:
If each individual action is viewed as the corresponding pure strategy, the action set can be embedded in the (mixed) strategy set . The expression above can then be viewed as extending from to .
von Neumann's Theorem
After mixed strategies are introduced, the maximin and minimax values agree, so the outcome of a simultaneous zero-sum game is uniquely determined.
Theorem (von Neumann)
In a simultaneous zero-sum game with mixed strategies, if both players use optimal strategies, player 's maximum payoff is
and player 's maximum payoff is .
Proof
Let . For the inner minimization, , so ; an optimal solution is the corresponding pure strategy. Therefore . Introducing an auxiliary variable , rewrite this as
The constraint is equivalent to for every . Substituting the definition of the mixed strategy and the payoff function , the problem is equivalent to the linear program
This problem is feasible and has an optimum. By the duality principle, its optimum equals that of the dual problem:
Repeating the preceding steps, this problem is equivalent to . The theorem follows.
This result is precisely the Nash equilibrium of the game. If both players choose optimal strategies in the equilibrium, neither player can gain strictly by deviating from the equilibrium strategy.
Reduction to Linear Programming
The proof of von Neumann's theorem also gives a way to solve simultaneous zero-sum games. Let and be the numbers of actions available to players and , respectively. Given player 's payoff matrix , solve the following linear program:
This is a linear program of size and can be solved efficiently with the simplex method. The optimal solution returned by the algorithm is player 's optimal mixed strategy. To obtain player 's optimal strategy, read the dual variables (the shadow prices) of the optimal solution from the simplex tableau.