#P4363. [九省联考 2018] 一双木棋 chess

    ID: 3334 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp搜索2018各省省选状态压缩,状压

[九省联考 2018] 一双木棋 chess

Description

Feifei and Niuniu play on an nn by mm board. Feifei plays black and goes first, Niuniu plays white and goes second.

At the start, the board is empty. They alternately place pieces on cells until the board is completely filled.

The placement rule is: a cell can be chosen if and only if the cell is empty and all cells to its left in the same row and all cells above it in the same column already contain pieces.

Each cell of the board has two nonnegative integers written on it. For the cell in row ii (from top to bottom) and column jj (from left to right), the two integers are denoted by ai,ja_{i, j} and bi,jb_{i, j}.

After the game ends, Feifei’s score is the sum of ai,ja_{i, j} over all cells with a black piece, and Niuniu’s score is the sum of bi,jb_{i, j} over all cells with a white piece.

Both players want to maximize the result of their own score minus the opponent’s score. They would like to know, on the given board, if both sides play optimally and know the other will also play optimally, what is the final result.

Input Format

The first line contains two integers, the numbers of rows nn and columns mm of the board.
Lines 22 through (n+1)(n + 1) each contain mm integers; on line (i+1)(i + 1), the jj-th integer is ai,ja_{i, j}.
Lines (n+2)(n + 2) through (2n+1)(2n + 1) each contain mm integers; on line (n+i+1)(n + i + 1), the jj-th integer is bi,jb_{i, j}.

Output Format

Output a single integer, the value of Feifei’s score minus Niuniu’s score.

2 3
2 7 3
9 1 2
3 7 2
2 3 1

2

Hint

Sample 1 Explanation

The board is as shown. Under optimal play, the game proceeds as follows:

  • Feifei plays at row 11, column 11 (this is the only legal move on the first turn).
  • Niuniu plays at row 11, column 22.
  • Feifei plays at row 22, column 11.
  • Niuniu plays at row 11, column 33.
  • Feifei plays at row 22, column 22.
  • Niuniu plays at row 22, column 33 (this is the only legal move on this turn).
  • The board is filled, and the game ends.

The final position is:

Feifei’s score is 2+9+1=122 + 9 + 1 = 12, and Niuniu’s score is 7+2+1=107 + 2 + 1 = 10.

Constraints

The information for each test point is shown in the table below.

  • For test points with odd indices, it is guaranteed that bi,j=0b_{i, j} = 0.
  • For all test points, it is guaranteed that 1n,m101 \leq n, m \leq 10 and 0ai,j,bi,j1050 \leq a_{i, j}, b_{i, j} \leq 10^5.

Translated by ChatGPT 5