QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#7057. 数字路径

统计

恶霸 Zhe 坏事做尽,经常欺负弱小。他的队友们长期受到虐待,终于决定不再忍受,逃往“数字村”(Digital Village),而恶霸则紧追不舍。由于地形复杂且存在大量交错的“数字路径”(Digital Paths),他们不容易被抓到。

为了让这些无辜者尽快熟悉地形以逃脱欺凌,他们现在需要统计数字村中数字路径的数量。

为了简化问题,数字村被抽象为一个 $n$ 行 $m$ 列的网格,每个格子中填有一个整数。数字路径是网格中的一条连续行走路线,满足以下条件:

  • 路径中相邻的格子共享一条公共边;
  • 路径是极大的,即无法继续延伸;
  • 路径至少包含四个格子;
  • 从一端走到另一端,任意两个相邻格子的数值增量恰好为 1。

以下是一些示例。

图 1:一条无效路径。

图 1 中的路径无效,因为其长度小于 4。

图 2:一条无效路径。

图 2 中的路径无效,因为它是非连续的。

图 3:一条无效路径。

图 3 中的路径无效,因为它还可以继续延伸。

图 4:一条无效路径。

图 4 中的路径也无效,因为路径中的数值并非严格递增 1。

图 5:所有有效路径。

数字路径可能会部分重叠。在图 5 中,有 4 条用不同颜色标记的数字路径。

输入格式

第一行包含两个正整数 $n$ 和 $m$ ($1 \le n, m \le 1000$),描述网格的大小。 接下来的 $n$ 行,每行包含 $m$ 个整数,其中第 $j$ 个整数记为 $a_{i,j}$ ($-10^7 \le a_{i,j} \le 10^7$),表示第 $i$ 行第 $j$ 列格子的数值。

输出格式

输出数字路径的数量,对 $10^9 + 7$ 取模。

样例

输入格式 1

3 5
1 2 3 8 7
-1 -1 4 5 6
1 2 3 8 7

输出格式 1

4

输入格式 2

4 4
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1

输出格式 2

16

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.