Official

D - Game in Momotetsu World Editorial by en_translator


First, when the piece is at the \(i\)-th row and \(j\)-th column, the next player who take turns is Takahashi if \((i + j)\) is even, or Aoki if it is odd.
Consider the following two-dimensional integer array \(\mathrm{opt}\):

\(\mathrm{opt}[i][j]\) :

  • If \((i + j)\) is even: The maximum final \((\text{Takahashi's score}) - (\text{Aoki's score})\), where the game start with the piece placed at the \(i\)-th row and \(j\)-th column, and Takahashi takes turns first.
  • If \((i + j)\) is odd: The minimum final \((\text{Takahashi's score}) - (\text{Aoki's score})\), where the game start with the piece placed at the \(i\)-th row and \(j\)-th column, and Aoki takes turns first.

With \(f(i, j) = (1\) if \(A_{i, j}\) is + or \(-1\) if it is -\()\) defined, \(\mathrm{opt}\) satisfies the following equations:

  • If \((i + j)\) is even: \(\mathrm{opt}[i][j] = \max(\mathrm{opt}[i + 1][j] + f(i + 1, j), \mathrm{opt}[i][j + 1] + f(i, j + 1))\)
  • If \((i + j)\) is odd: \(\mathrm{opt}[i][j] = \min(\mathrm{opt}[i + 1][j] - f(i + 1, j), \mathrm{opt}[i][j + 1] - f(i, j + 1))\)

Therefore, we can calculate \((i, j)\) in the decreasing order of \((i, j)\).
Note that the equations above does not take into account the possibility of referencing outside the grid, so you have to add the process of ignoring the squares outside the grid.
Finally, the winner can be determined by the sign of \(\mathrm{opt}[1][1]\).

posted:
last update: