第七回日本最強プログラマー学生選手権-予選-(AtCoder Regular Contest 222)
C
2 个方向 vs 4 个方向
2 Directions vs 4 Directions
AtCoder
⌀ 1945
原题
← BD →

2 个方向 vs 4 个方向AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

There is an $N\times N$ grid, where $N$ is an integer at least $2$. For $1\leq i,j\leq N$, the cell at row $i$ from the top and column $j$ from the left is denoted by $(i,j)$.

有一个 $N\times N$ 的网格,其中 $N$ 是一个不小于 $2$ 的整数。对于 $1\leq i,j\leq N$,从上往下数第 $i$ 行、从左往右数第 $j$ 列的单元格记为 $(i,j)$。AI (CI翻译)

Initially, all cells are black. For each cell $(i,j)$, a cost $A_{i,j}$ to change the color of the cell from black to white is given.

初始时,所有单元格都是黑色的。对于每个单元格 $(i,j)$,给定一个将该单元格从黑色变为白色的代价 $A_{i,j}$。AI (CI翻译)

Alice and Bob play a game using this grid and one piece. The game consists of the following three steps.

Alice 和 Bob 使用这个网格以及一个棋子进行游戏。游戏由以下三个步骤组成。AI (CI翻译)

Step 1
步骤 1
AI (CI翻译)

The referee specifies the cell where the piece is initially placed.

裁判指定棋子最初所在的单元格。AI (CI翻译)

Step 2
步骤 2
AI (CI翻译)

Alice performs the following operation zero or more times:

Alice 执行以下操作零次或多次:AI (CI翻译)

  • Choose a black cell $(i,j)$, pay cost $A_{i,j}$, and change that cell to white.
  • 选择一个黑色单元格 $(i,j)$,支付代价 $A_{i,j}$,并将该单元格变为白色。
AI (CI翻译)
Step 3
步骤 3
AI (CI翻译)

Place the piece on the cell specified as the initial position in Step 1. Then, starting with Alice, Alice and Bob take turns making moves.

将棋子放在步骤 1 中指定的初始位置。然后从 Alice 开始,Alice 和 Bob 轮流移动棋子。AI (CI翻译)

  • On Alice's turn, Alice moves the piece to an adjacent cell to the left or right.
  • On Bob's turn, Bob moves the piece to an adjacent cell in any of the up, down, left, or right directions.
  • 在 Alice 的回合,Alice 将棋子移动到左侧或右侧的相邻单元格。
  • 在 Bob 的回合,Bob 将棋子移动到上、下、左、右任一方向的相邻单元格。
AI (CI翻译)

A move that would take the piece outside the grid is not allowed.

不允许进行会使棋子移出网格的移动。AI (CI翻译)

Step 3 ends immediately after Alice and Bob have each taken $10^{10}$ turns.

当 Alice 和 Bob 各自移动 $10^{10}$ 次后,步骤 3 立即结束。AI (CI翻译)

At the end of Step 3, if the piece is on a white cell, Alice wins; if it is on a black cell, Bob wins. In Step 3, Alice and Bob each act optimally to win.

步骤 3 结束时,如果棋子位于白色单元格,Alice 获胜;如果棋子位于黑色单元格,Bob 获胜。在步骤 3 中,Alice 和 Bob 都会采取最优策略以取得胜利。AI (CI翻译)

For each cell $(h,w)$, find the minimum total cost that Alice must pay in Step 2 to guarantee a win if $(h,w)$ is specified as the initial cell in Step 1.

对于每个单元格 $(h,w)$,求当步骤 1 指定 $(h,w)$ 为初始单元格时,Alice 为保证获胜而必须在步骤 2 支付的最小总代价。AI (CI翻译)

$T$ test cases are given; solve each of them.

给定 $T$ 个测试用例,请分别求解。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1\leq T\leq 10^4$
  • $2\leq N\leq 500$
  • $0\leq A_{i,j}\leq 10^9$
  • All input values are integers.
  • The sum of $N^2$ over all test cases is at most $500^2$.
  • $1\leq T\leq 10^4$
  • $2\leq N\leq 500$
  • $0\leq A_{i,j}\leq 10^9$
  • 所有输入值均为整数。
  • 所有测试用例的 $N^2$ 之和不超过 $500^2$。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入给出,格式如下:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case is given in the following format:

每个测试用例的格式如下:AI (CI翻译)

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $N$ lines per test case.

每个测试用例输出 $N$ 行。AI (CI翻译)

For each test case, the $h$-th line should contain the minimum total cost that Alice must pay in Step 2 to guarantee a win for each of $(h,1),(h,2),\ldots,(h,N)$ specified as the initial cell, space-separated.

对于每个测试用例,第 $h$ 行应包含当初始单元格指定为 $(h,1),(h,2),\ldots,(h,N)$ 时,Alice 为保证获胜而必须在步骤 2 支付的最小总代价,整数之间用空格分隔。AI (CI翻译)

样例

Input #1
3
2
1 4
3 2
3
1 2 3
8 9 4
7 6 5
4
0 0 1 0
1 0 1 1
1 1 1 0
0 1 1 1
Output #1
3 7
7 3
25 20 25
20 25 20
25 20 25
4 3 4 3
4 4 3 5
4 3 4 3
3 4 3 5
Let us explain the first test case. For $(h,w) = (1,1)$ or $(h,w) = (2,2)$, Alice changes cells $(1,1)$ and $(2,2)$ to white. In this case, at the end of each of Bob's turns, the piece is at $(1,1)$ or $(2,2)$. Therefore, immediately after Alice and Bob have each taken $10^{10}$ turns, the piece is at $(1,1)$ or $(2,2)$, so Alice wins regardless of how the piece is moved. The cost Alice pays in Step 2 is $A_{1,1}+A_{2,2}=1+2=3$. For $(h,w) = (1,2)$ or $(h,w) = (2,1)$, it can be verified that Alice can win by changing cells $(1,2)$ and $(2,1)$ to white. In this case, the cost Alice pays in Step 2 is $A_{1,2}+A_{2,1}=4+3=7$.
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。