VOOZH about

URL: https://www.geeksforgeeks.org/dsa/dynamic-programming-in-game-theory-for-competitive-programming/

⇱ Dynamic Programming in Game Theory for Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic Programming in Game Theory for Competitive Programming

Last Updated : 23 Jul, 2025

In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive edge. Whether you're a seasoned coder or a newcomer, this article uncover the power of Dynamic Programming in Game Theory for Competitive Programming.

In these type of problems you are basically given 2 players(first and second), set of operations, and a win condition. Given that both the players play optimally and they take turn one after the other we have to determine the winner of the game.

If a player is standing at a particular state 'S' and it can send its opponent to one of the K different states T1, T2...TK then:

  • : If there exists atleast one state from T1 to TK which is a Loosing States.
  • : All the states from T1 to TK are Winning States.

Obviously, in order to win the player will try to send its opponent to any loosing state and if it is not possible to do so then that player itself looses the game. follow the below image for better understanding.

👁 Winning-and-Loosing-State-for-a-playerdrawio

Lets's take a look over some problems to understand this concept more thoroughly:

Two players (First and Second) are playing a game on a 2-D plane in which a Token has been place at coordinate (0,0). Players can perform two types of operations on the token:

  1. Increase the X coordinate by exactly K distance
  2. Increase the Y coordinate by exactly K distance

You are given an integer D, In order to perform above operations the player must ensure that the token stays within Euclidean distance D from (0,0) i.e. after each operation, X2 + Y2 <= D2 where X and Y are the coordinates of the token.

Given the value of K and D, determine the Winner of the Game assuming both the players play optimally and First player plays first.

Example:

: D=2, K=1
: Second
: First player moves token from (0,0) to (0,1)
Second player moves token from (0,1) to (0,2)
Now, whatever move First player makes, the distance will exceed the Euclidean Distance

: D=5, K=2
: Second

Let DP[i][j] denote whether the coordinate (i,j) are a winning state or a loosing state for any player that is:

  • DP[i][j] = 0 means a loosing state
  • DP[i][j] = 1 means a winning state

On the basis of given operations we can either increment x coordinate by K or y coordinate by K, hence DP[i][j] depends on two values:

  • DP[i+k][j]
  • DP[i][j+k]

:

  • DP[i+k][j] = 0 OR DP[i][j+k] = 0 (Why? read the definition of winning state)

:

  • DP[i+k][j] = 1 AND DP[i][j+k] = 1 (Why? read the definition of Loosing state)

Below is the implementation of the above approach:


Output
First player wins

Two players (First and Second) are playing a game on array arr[] of size N. The players are building a sequence together, initially the sequence is empty. In one turn the player can perform either of the following operations:

  • Remove the rightmost array element and append it to the sequence.
  • Remove the leftmost array element and append it to the sequence.

The rule is that the sequence must always be strictly increasing, the winner is the player that makes the last move. The task is to determine the winner.

Example:

: arr=[5, 4, 5]
: First Player Wins
: After the first player append 5 into the sequence the array would look like either [4,5] or [5,4] and second player won't be able to make any move.

: arr=[5, 8, 2, 1, 10, 9]
: Second Player Wins
: For any element the first player append to the sequence, the second player can always append a strictly greater elements.

Solution:

Let DP[L][R] denote whether the subarray from L to R is a Loosing state or a Winning state for any player that is:

  • DP[L][R] = 0 means L to R forms a Loosing subarray.
  • DP[L][R] = 1 means L to R forms a Winning subarray.

As the operations allow a player to only remove from the rightmost end or the leftmost end therefore DP[L][R] will depend on two ranges based on two conditions:

  • DP[L+1][R] if we can append Arr[L] into our strictly increasing sequence.
  • DP[L][R-1] if we can append Arr[R] into our strictly increasing sequence.

:

  • DP[L+1][R] =0 OR DP[L][R-1] = 0 (Why? read the definition of winning state)

:

  • DP[L+1][R] =1 AND DP[L][R-1] = 1 (Why? read the definition of Loosing state)

Below is the implementation of the above approach:


Output
Second player wins

Optimal Strategy for a Game

Optimal Strategy for a Game | Set 2

Optimal Strategy for a Game | Set 3

Optimal Strategy for the Divisor game using Dynamic Programming

Game of N stones where each player can remove 1, 3 or 4

Find the player who will win by choosing a number in range [1, K] with sum total N

Find the winner of the game with N piles of boxes

Coin game of two corners (Greedy Approach)

Comment
Article Tags: