CF1353F.Decreasing Heights

提高+/省选-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are playing one famous sandbox game with the three-dimensional world. The map of the world can be represented as a matrix of size n×mn \times m, where the height of the cell (i,j)(i, j) is ai,ja_{i, j}.

You are in the cell (1,1)(1, 1) right now and want to get in the cell (n,m)(n, m). You can move only down (from the cell (i,j)(i, j) to the cell (i+1,j)(i + 1, j)) or right (from the cell (i,j)(i, j) to the cell (i,j+1)(i, j + 1)). There is an additional restriction: if the height of the current cell is xx then you can move only to the cell with height x+1x+1.

Before the first move you can perform several operations. During one operation, you can decrease the height of any cell by one. I.e. you choose some cell (i,j)(i, j) and assign (set) ai,j:=ai,j−1a_{i, j} := a_{i, j} - 1. Note that you can make heights less than or equal to zero. Also note that you can decrease the height of the cell (1,1)(1, 1).

Your task is to find the minimum number of operations you have to perform to obtain at least one suitable path from the cell (1,1)(1, 1) to the cell (n,m)(n, m). It is guaranteed that the answer exists.

You have to answer tt independent test cases.

你正在玩一款著名的沙盒游戏,其世界是三维的。游戏地图可以表示为一个 n×mn \times m 的矩阵,其中单元格 (i,j)(i, j) 的高度为 ai,ja_{i, j}。

你当前位于单元格 (1,1)(1, 1),目标是到达单元格 (n,m)(n, m)。你每次只能向下移动(从单元格 (i,j)(i, j) 移动到 (i+1,j)(i + 1, j))或向右移动(从单元格 (i,j)(i, j) 移动到 (i,j+1)(i, j + 1))。此外还有一个限制条件:若当前单元格的高度为 xx,则你只能移动到高度恰好为 x+1x+1 的单元格。

在第一次移动之前,你可以执行若干次操作。每次操作中,你可以将任意一个单元格的高度减小 11。即:你选择某个单元格 (i,j)(i, j),并令 ai,j:=ai,j−1a_{i, j} := a_{i, j} - 1。注意,单元格高度可被减至 00 或负数;同时,你也可以降低起点单元格 (1,1)(1, 1) 的高度。

你的任务是求出为使得至少存在一条从 (1,1)(1, 1) 到 (n,m)(n, m) 的合法路径所需执行的最少操作次数。题目保证答案一定存在。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and mm (1≤n,m≤1001 \le n, m \le 100) — the number of rows and the number of columns in the map of the world. The next nn lines contain mm integers each, where the jj-th integer in the ii-th line is ai,ja_{i, j} (1≤ai,j≤10151 \le a_{i, j} \le 10^{15}) — the height of the cell (i,j)(i, j).

It is guaranteed that the sum of nn (as well as the sum of mm) over all test cases does not exceed 100100 (∑n≤100;∑m≤100\sum n \le 100; \sum m \le 100).

输入的第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤1001 \le n, m \le 100),分别表示世界地图的行数和列数。接下来的 nn 行每行包含 mm 个整数,其中第 ii 行的第 jj 个整数为 ai,ja_{i, j}(1≤ai,j≤10151 \le a_{i, j} \le 10^{15}),表示单元格 (i,j)(i, j) 的高度。

保证所有测试用例中 nn 的总和(以及 mm 的总和)均不超过 100100(即 ∑n≤100\sum n \le 100;∑m≤100\sum m \le 100)。

输出格式

For each test case, print the answer — the minimum number of operations you have to perform to obtain at least one suitable path from the cell (1,1)(1, 1) to the cell (n,m)(n, m). It is guaranteed that the answer exists.

对于每个测试用例,输出答案——即从单元格 (1,1)(1, 1) 到单元格 (n,m)(n, m) 获得至少一条合适路径所需执行的最少操作次数。保证答案存在。

输入输出样例

  • 输入#1

    5
    3 4
    1 2 3 4
    5 6 7 8
    9 10 11 12
    5 5
    2 5 4 8 3
    9 10 11 5 1
    12 8 4 2 5
    2 2 5 4 1
    6 8 2 4 2
    2 2
    100 10
    10 1
    1 2
    123456789876543 987654321234567
    1 1
    42

    输出#1

    9
    49
    111
    864197531358023
    0

输入解题思路,AI测评打分。不知道怎么写?

首页