QOJ.ac

QOJ

Límite de tiempo: 3.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#10496. 路径规划 2

Estadísticas

有一个 $n$ 行 $m$ 列的网格。每个单元格中都有一个整数,其中 $a_{i,j}$ 表示位于第 $i$ 行第 $j$ 列的单元格中的整数。

令 $(i, j)$ 为位于第 $i$ 行第 $j$ 列的单元格。你现在从 $(1, 1)$ 出发,需要到达 $(n, m)$。当你位于单元格 $(i, j)$ 时,如果 $j < m$,你可以移动到其右侧的单元格 $(i, j + 1)$;如果 $i < n$,你可以移动到其下方的单元格 $(i + 1, j)$。

令 $\mathbb{S}$ 为路径上所有单元格中整数组成的集合,包括 $a_{1,1}$ 和 $a_{n,m}$。令 $\text{mex}(\mathbb{S})$ 为不属于 $\mathbb{S}$ 的最小非负整数。请找到一条路径以最小化 $\text{mex}(\mathbb{S})$,并计算该最小值。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$ ($1 \le T \le 10^4$),表示测试数据的组数。对于每组测试数据:

第一行包含两个整数 $n$ 和 $m$ ($1 \le n, m \le 10^6, 1 \le n \times m \le 10^6$),表示网格的行数和列数。

接下来的 $n$ 行中,第 $i$ 行包含 $m$ 个整数 $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 10^9$),其中 $a_{i,j}$ 表示单元格 $(i, j)$ 中的整数。

保证所有测试数据的 $n \times m$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出一行,包含一个整数,表示 $\text{mex}(\mathbb{S})$ 的最小值。

样例

输入 1

2
2 3
2 0 1
0 3 4
1 5
100 0 2 0 1

输出 1

1
3

说明

对于第一个样例测试数据,有 3 条可能的路径。

  • 第一条路径是 $(1, 1) \to (1, 2) \to (1, 3) \to (2, 3)$。$\mathbb{S} = \{2, 0, 1, 4\}$,因此 $\text{mex}(\mathbb{S}) = 3$。
  • 第二条路径是 $(1, 1) \to (1, 2) \to (2, 2) \to (2, 3)$。$\mathbb{S} = \{2, 0, 3, 4\}$,因此 $\text{mex}(\mathbb{S}) = 1$。
  • 第三条路径是 $(1, 1) \to (2, 1) \to (2, 2) \to (2, 3)$。$\mathbb{S} = \{2, 0, 3, 4\}$,因此 $\text{mex}(\mathbb{S}) = 1$。

所以答案是 $\min(3, 1, 1) = 1$。

对于第二个样例测试数据,只有 1 条可能的路径,即 $(1, 1) \to (1, 2) \to (1, 3) \to (1, 4) \to (1, 5)$。$\mathbb{S} = \{100, 0, 2, 1\}$,因此 $\text{mex}(\mathbb{S}) = 3$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.