#abc293c. C - Make Takahashi Happy

C - Make Takahashi Happy

Score : 300300 points

问题描述

存在一个具有 HH 行(水平方向)和 WW 列(垂直方向)的网格。对于满足条件 1iH1 \leq i \leq H1jW1 \leq j \leq W 的两个整数 iijj,从上到下的第 ii 行、从左到右的第 jj 列(我们用坐标 (i,j)(i, j) 表示)的方格上写有一个整数 Ai,jA_{i, j}

高桥当前位于点 (1,1)(1,1)。从现在开始,他重复向当前所在方格右侧或下方相邻的方格移动,直到到达点 (H,W)(H, W)。在移动过程中,他不允许离开网格范围。

如果高桥经过的所有方格(包括初始位置 (1,1)(1, 1) 和最终位置 (H,W)(H, W))上的整数各不相同,那么他将会很开心。请找出使他开心的可能路径数量。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a grid with HH horizontal rows and WW vertical columns. For two integers ii and jj such that 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, the square at the ii-th row from the top and jj-th column from the left (which we denote by (i,j)(i, j)) has an integer Ai,jA_{i, j} written on it.

Takahashi is currently at (1,1)(1,1). From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches (H,W)(H, W). When he makes a move, he is not allowed to go outside the grid.

Takahashi will be happy if the integers written on the squares he visits (including initial (1,1)(1, 1) and final (H,W)(H, W)) are distinct. Find the number of his possible paths that make him happy.

Constraints

  • 2H,W102 \leq H, W \leq 10
  • 1Ai,j1091 \leq A_{i, j} \leq 10^9
  • All values in the input are integers.

Input

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

HH WW

A1,1A_{1, 1} A1,2A_{1, 2} \ldots A1,WA_{1, W}

A2,1A_{2, 1} A2,2A_{2, 2} \ldots A2,WA_{2, W}

\vdots

AH,1A_{H, 1} AH,2A_{H, 2} \ldots AH,WA_{H, W}

Output

Print the answer.

Sample Input 1

3 3
3 2 2
2 1 3
1 5 4

Sample Output 1

3

There are six possible paths:

  • $(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,2,3,43, 2, 2, 3, 4, so he will not be happy.
  • $(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,1,3,43, 2, 1, 3, 4, so he will not be happy.
  • $(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,1,5,43, 2, 1, 5, 4, so he will be happy.
  • $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,1,3,43, 2, 1, 3, 4, so he will not be happy.
  • $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,1,5,43, 2, 1, 5, 4, so he will be happy.
  • $(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3)$: the integers written on the squares he visits are 3,2,1,5,43, 2, 1, 5, 4, so he will be happy.

Thus, the third, fifth, and sixth paths described above make him happy.

Sample Input 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Sample Output 2

48620

In this example, every possible path makes him happy.

update @ 2024/3/10 12:12:22