To solve the given problem, we will utilize the Sprague-Grundy theorem and memoization techniques. Here's the summary of our approach:
-
Model the Game using States: We model the game as a recursive function where the state is defined by the current position (column) and the availability of cells in the current and adjacent columns. Each column has three cells, and we represent the state of these cells using bits.
-
Define the Recursive Function: We define a recursive function grundy(pos, col_m1, col_0, col_p1) where:
pos is the current column index.
col_m1, col_0, col_p1 represent the availability of cells in the previous column, the current column, and the next column respectively.
-
Compute Grundy Numbers with Memoization: We utilize memoization to store the Grundy numbers for computed states to avoid redundant calculations. The base case is when we've processed all columns, and there are no available cells left.
-
Recursive Transitions: For each available cell in the current column, we simulate placing a piece and recursively compute the Grundy number for the new state. We need to update the availability of cells in adjacent columns and rows due to the adjacency constraints.
-
Determine the Winner: Using the initial state where all cells are available, we compute the Grundy number. If it's non-zero, Alice has a winning strategy; otherwise, Bob can force a win.
Below is the C++ code implementing this approach:
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
const int MAX_N = 100; // Maximum number of columns
int N;
int dp[MAX_N + 1][8][8][8]; // DP table for memoization
int initial_col[MAX_N + 2]; // Initial state of each column (0 to N+1)
// Function to get the mex (minimum excludant) of a set
int mex(set<int>& S) {
int m = 0;
while (S.count(m)) ++m;
return m;
}
// The recursive function to compute the Grundy number
int grundy(int pos, int col_m1, int col_0, int col_p1) {
if (pos > N) {
return 0;
}
if (dp[pos][col_m1][col_0][col_p1] != -1) {
return dp[pos][col_m1][col_0][col_p1];
}
// If there are no available cells in col_0, move to the next position
if (col_0 == 0) {
int res = grundy(pos + 1, col_0, col_p1, initial_col[pos + 2]);
dp[pos][col_m1][col_0][col_p1] = res;
return res;
}
set<int> S;
// Try to place a piece at each available cell in col_0
for (int row = 0; row < 3; ++row) {
if ((col_0 >> row) & 1) {
// Create copies for the next state
int col_m1_nxt = col_m1;
int col_0_nxt = col_0;
int col_p1_nxt = col_p1;
// Place the piece
col_0_nxt &= ~(1 << row); // Occupied
// Block adjacent cells
// Up
if (row > 0) col_0_nxt &= ~(1 << (row - 1));
// Down
if (row < 2) col_0_nxt &= ~(1 << (row + 1));
// Left
col_m1_nxt &= ~(1 << row);
// Right
col_p1_nxt &= ~(1 << row);
int res;
if (col_0_nxt == 0) {
// Move to next position
res = grundy(pos + 1, col_0, col_p1_nxt, initial_col[pos + 2]);
} else {
// Stay at the same position
res = grundy(pos, col_m1_nxt, col_0_nxt, col_p1_nxt);
}
S.insert(res);
}
}
int res = mex(S);
dp[pos][col_m1][col_0][col_p1] = res;
return res;
}
int main() {
cin >> N;
// Initialize the DP table
memset(dp, -1, sizeof(dp));
// Initialize initial_col array
for (int i = 0; i <= N + 1; ++i) {
initial_col[i] = 7; // All bits set (cells available)
}
// Initial call to the recursive function
int result = grundy(0, 0, initial_col[0], initial_col[1]);
if (result != 0) {
cout << "WIN" << endl;
} else {
cout << "LOSE" << endl;
}
return 0;
}
This code reads the number of columns N, initializes the DP table and the initial state of each column, and then computes the Grundy number starting from the leftmost column. Based on the computed Grundy number, it outputs "WIN" if Alice can force a win, or "LOSE" otherwise.