## Similar Problems

Similar Problems not available

# Game Play Analysis Ii - Leetcode Solution

## Companies:

LeetCode: Game Play Analysis Ii Leetcode Solution

Difficulty: Easy

Topics: database

Problem:

You are working on a game that is played on a grid of size m x n. The rules of the game are as follows:

Each player starts in the upper-left corner (1,1) and the bottom-right corner (m,n) of the grid respectively. They take turns to move, with the first player moving first. In each turn, the current player can move to an adjacent cell in one of the four directions (up, down, left, or right), as long as the cell is not occupied by the other player or outside the grid. The game ends when one player reaches the other player's start position. The player who moves last wins. Assume both players play optimally and have perfect knowledge of each other's position at all times. If the first player wins, return true; otherwise, return false.

Note: In the case where both players always move optimally, if the game still doesn't end by the nth move (where n is the total number of cells in the grid), the first player wins.

Example:

Input: grid = [ ["+", "+", ".", "+"], ["+", "+", "+", "."], ["+", "+", ".", "+"], ["+", ".", "+", "+"] ]

Output: true

Explanation: Player 1 moves down -> right -> right -> down -> down -> right -> down. After 7 moves, Player 1 reaches (4,3) which is the starting position of Player 2 and wins the game. Therefore, we return true. Note:

m == grid.length n == grid[i].length 1 <= m, n <= 100 grid[i][j] is either " ", "+", or ".".

Solution:

To solve this problem, we can use the minimax algorithm to find the optimal move for each player at each turn.

First, let's define the game state. We can represent the state as a 2D matrix where each cell can be either empty ("."), occupied by player 1 ("x"), or occupied by player 2 ("o"). We can also store the current player and the current move number.

Next, we need to define the evaluation function. For this game, we can simply return the distance between the two players. This gives us an estimate of how close each player is to winning. The closer a player is to winning, the higher the evaluation function.

Now, we can define the minimax algorithm. The algorithm works by recursively evaluating the game tree. At each level of the tree, we alternate between maximizing and minimizing the evaluation function. If the depth of the tree is even (i.e., it's player 2's turn), we minimize the value of the evaluation function. Otherwise, we maximize it.

To prevent our algorithm from taking too much time to search the entire game tree, we can use alpha-beta pruning. This is a technique where we stop evaluating a branch of the game tree if we know it won't affect the final result.

Here's the Python code to implement the solution:

def game_play_analysis(grid): m, n = len(grid), len(grid[0]) start1, start2 = (0, 0), (m - 1, n - 1)

```
# define game state
def state(grid, player, move):
return grid, player, move
# define evaluation function
def evaluate(grid):
p1, p2 = None, None
for i in range(m):
for j in range(n):
if grid[i][j] == 'x':
p1 = (i, j)
elif grid[i][j] == 'o':
p2 = (i, j)
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
# define minimax algorithm
def minimax(grid, player, move, alpha, beta):
score = evaluate(grid)
if move == m * n:
return score
if player == 'x':
# maximizing player
max_score = float('-inf')
for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
x, y = p1 = start1 if move % 2 == 0 else p1
nx, ny = x + i, y + j
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] != 'o':
grid[x][y], grid[nx][ny] = '.', 'x'
max_score = max(max_score, minimax(grid, 'o', move + 1, alpha, beta))
grid[x][y], grid[nx][ny] = 'x', '.'
alpha = max(alpha, max_score)
if beta <= alpha:
break
return max_score
else:
# minimizing player
min_score = float('inf')
for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
x, y = p2 = start2 if move % 2 == 1 else p2
nx, ny = x + i, y + j
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] != 'x':
grid[x][y], grid[nx][ny] = '.', 'o'
min_score = min(min_score, minimax(grid, 'x', move + 1, alpha, beta))
grid[x][y], grid[nx][ny] = 'o', '.'
beta = min(beta, min_score)
if beta <= alpha:
break
return min_score
# run minimax algorithm
result = minimax(grid, 'x', 0, float('-inf'), float('inf'))
return result > 0
```

The time complexity of this algorithm is O((mn/2)!/(mn/2-2)!), which is the number of possible moves for the first player. However, with alpha-beta pruning, the actual runtime is much lower than this worst-case scenario.

## Game Play Analysis Ii Solution Code

`1`