CF2055C.The Trail
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are no mountains in Florida, and Florida Man cannot comprehend their existence. As such, he really needs your help with this one.
In the wilderness lies a region of mountainous terrain represented as a rectangular grid with n rows and m columns. Each cell in the grid is identified by its position (i,j), where i is the row index and j is the column index. The altitude of cell (i,j) is denoted by ai,j.
However, this region has been tampered with. A path consisting of n+m−1 cells, starting from the top-left corner (1,1) and ending at the bottom-right corner (n,m), has been cleared. For every cell (i,j) along this path, the altitude ai,j has been set to 0. The path moves strictly via downward (D) or rightward (R) steps.
To restore the terrain to its original state, it is known that the region possessed a magical property before it was tampered with: all rows and all columns shared the same sum of altitudes. More formally, there exists an integer x such that ∑j=1mai,j=x for all 1≤i≤n, and ∑i=1nai,j=x for all 1≤j≤m.
Your task is to assign new altitudes to the cells on the path such that the above magical property is restored. It can be proven that a solution always exists. If there are multiple solutions that satisfy the property, any one of them may be provided.
佛罗里达州没有山脉,而“佛罗里达男子”无法理解山脉的存在。因此,他非常需要你在此问题上的帮助。
野外有一片山地地形,以一个 n 行 m 列的矩形网格表示。网格中每个单元格由其位置 (i,j) 标识,其中 i 为行索引,j 为列索引。单元格 (i,j) 的海拔高度记为 ai,j。
然而,该区域已被人为篡改:一条由 n+m−1 个单元格组成的路径已被清理,该路径从左上角 (1,1) 出发,终止于右下角 (n,m)。对于该路径上的每一个单元格 (i,j),其海拔高度 ai,j 均被设为 0。该路径仅允许向下(D)或向右(R)移动。
为了将地形恢复至原始状态,已知该区域在被篡改前具有一种神奇性质:所有行与所有列的海拔高度之和均相等。更准确地说,存在某个整数 x,使得对所有 1≤i≤n,有 ∑j=1mai,j=x;且对所有 1≤j≤m,有 ∑i=1nai,j=x。
你的任务是为路径上的各单元格重新指定海拔高度,使得上述神奇性质得以恢复。可以证明,解一定存在。若存在多个满足条件的解,则输出任意一个即可。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (2≤n,m≤1000) — the number of rows and columns in the grid.
The second line of each test case contains a string s of length n+m−2 (si=D or si=R) — the steps the path makes from (1,1) to (n,m). The character D represents a downward step, and R represents a rightward step.
The i-th of the next n lines each contain m integers ai,1,ai,2,…,ai,m (−106≤ai,j≤106) — the altitude of each cell in the grid. It is guaranteed that if a cell (i,j) lies on the path, then ai,j=0.
It is guaranteed that the sum of n⋅m over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n,m≤1000)—— 分别表示网格的行数和列数。
每个测试用例的第二行包含一个长度为 n+m−2 的字符串 s(其中每个字符 si 为 D 或 R)—— 表示从 (1,1) 到 (n,m) 的路径所经过的步骤。字符 D 表示向下移动一步,R 表示向右移动一步。
接下来的 n 行中,第 i 行包含 m 个整数 ai,1,ai,2,…,ai,m(−106≤ai,j≤106)—— 表示网格中每个单元格的海拔高度。保证:若单元格 (i,j) 位于该路径上,则 ai,j=0。
保证所有测试用例中 n⋅m 的总和不超过 106。
输出格式
For each test case, output n lines of m integers representing the restored grid of altitudes bi,j. The altitudes must satisfy −1015≤bi,j≤1015, and additionally ai,j=bi,j if (i,j) is not on the path. If multiple solutions exist, output any of them.
对于每个测试用例,输出 n 行,每行 m 个整数,表示恢复后的海拔高度网格 bi,j。海拔高度必须满足 −1015≤bi,j≤1015,且当 (i,j) 不在路径上时,还需满足 ai,j=bi,j。若存在多个解,输出其中任意一个即可。
输入输出样例
输入#1
4 3 3 DRRD 0 2 3 0 0 0 3 1 0 4 5 DRRRRDD 0 1 0 2 3 0 0 0 0 0 -1 0 -3 -3 0 0 0 0 -1 0 2 3 RRD 0 0 0 0 1 0 5 5 DDDDRRRR 0 25 2 9 11 0 6 13 20 22 0 17 24 1 8 0 3 10 12 19 0 0 0 0 0
输出#1
1 2 3 2 3 1 3 1 2 -6 1 0 2 3 7 -1 3 2 -11 -1 0 -3 -3 7 0 0 0 -1 1 0 -1 1 0 1 -1 18 25 2 9 11 4 6 13 20 22 15 17 24 1 8 21 3 10 12 19 7 14 16 23 5
说明/提示
In the first test case, the grid has been filled such that every row and column contains the numbers 1,2,3 in some order, resulting in a common sum of 6.
In the second test case, the grid has been filled such that all rows and columns sum to 0.
在第一个测试用例中,网格已填满,使得每行和每列都以某种顺序包含数字 1,2,3,从而得到共同的和 6。
在第二个测试用例中,网格已填满,使得所有行和列的和均为 0。
输入解题思路,AI测评打分。不知道怎么写?