ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CoinGame.h File Reference

The game of the coins in a line: \( n \) coins of values \( V_0 , \ldots , V_{n-1} \) lie in a row, and two players take in turn the first or the last coin left, each one to get the most; coinsInALine() is the most the first player can be sure of, against any play of the other. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "ADS/optimization/impl/CoinGame.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<class T>
T ADS::coinsInALine (const std::vector< T > &V)
 the most the first player can be sure of in the game of the coins in a line \( V \), both players taking in turn the first or the last coin

Detailed description

The game of the coins in a line: \( n \) coins of values \( V_0 , \ldots , V_{n-1} \) lie in a row, and two players take in turn the first or the last coin left, each one to get the most; coinsInALine() is the most the first player can be sure of, against any play of the other.

With \( D[ i ][ j ] \) the largest difference between the coins of the player to move and those of the other on \( V[ i .. j - 1 ] \),

\[ D[ i ][ j ] = \max( V_i - D[ i + 1 ][ j ] , V_{j-1} - D[ i ][ j - 1 ] ) , \]

\( D[ i ][ i ] = 0 \), and the first player gets \( ( S + D[ 0 ][ n ] ) / 2 \), \( S \) the sum of the coins; the table filled by increasing length keeps one row, \( \Theta( n^2 ) \) time and \( \Theta( n ) \) space. The scheme is the minimax on the intervals; the form with two moves ahead, \( M[ i ][ j ] = \max( V_i + \min( M[ i + 2 ][ j ] , M[ i + 1 ][ j - 1 ] ) , \ldots ) \), gives the same value.

For \( n \) even the first player never loses: the coins of even places and those of odd places can each be taken all by the first player, who chooses the larger set; this strategy, though, is not optimal in general.

The contracts [see Contract.h]: none on the values, which may be negative. The functions are defined in CoinGame.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13 and the Esercizi Programmazione dinamica, exercise B4 of 22/07/2013, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
P. Winkler, Mathematical Puzzles: A Connoisseur's Collection, A K Peters, 2004
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa