solution.md 6.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
# 地宫取宝
X 国王有一个地宫宝库,是 n×m 个格子的矩阵,每个格子放一件宝贝,每个宝贝贴着价值标签。
地宫的入口在左上角,出口在右下角。
小明被带到地宫的入口,国王要求他只能向右或向下行走。
走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿)。
当小明走到出口时,如果他手中的宝贝恰好是 k 件,则这些宝贝就可以送给小明。
请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这 k 件宝贝。

#### 输入格式
第一行 3 个整数,n,m,k,含义见题目描述。
接下来 n 行,每行有 m 个整数 Ci 用来描述宝库矩阵每个格子的宝贝价值。

#### 输出格式
输出一个整数,表示正好取 k 个宝贝的行动方案数。
该数字可能很大,输出它对 1000000007 取模的结果。

#### 数据范围
```
1≤n,m≤50,
1≤k≤12,
0≤Ci≤12
```
#### 输入样例1:
```
2 2 2
1 2
2 1
```
#### 输出样例1:
```
2
```
#### 输入样例2:
```
2 3 2
1 2 3
2 1 5
```
#### 输出样例2:
```
14
```

## aop
### before
```cpp
#include <iostream>
using namespace std;
const int N = 55;
const int MOD = 1e9 + 7;
int dp[N][N][13][14];
int g[N][N];
int n, m, k;
```
### after
```cpp

```

## 答案
```cpp
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
            g[i][j]++;
        }
    }
    dp[1][1][1][g[1][1]] = 1;
    dp[1][1][0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            for (int u = 0; u <= k; u++)
            {
                for (int v = 0; v <= 13; v++)
                {
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
                    if (u > 0 && v == g[i][j])
                    {
                        for (int c = 0; c < v; c++)
                        {
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u - 1][c]) % MOD;
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
                        }
                    }
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= 13; i++)
        res = (res + dp[n][m][k][i]) % MOD;
    cout << res << endl;
    return 0;
}

```
## 选项

### A
```cpp
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
            g[i][j]++;
        }
    }
    dp[1][1][1][g[1][1]] = 1;
    dp[1][1][0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            for (int u = 0; u <= k; u++)
            {
                for (int v = 0; v <= 13; v++)
                {
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
                    if (u > 0 && v == g[i][j])
                    {
                        for (int c = 0; c < v; c++)
                        {
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j][u - 1][c]) % MOD;
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
                        }
                    }
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= 13; i++)
        res = (res + dp[n][m][k][i]) % MOD;
    cout << res << endl;
    return 0;
}
```

### B
```cpp
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
            g[i][j]++;
        }
    }
    dp[1][1][1][g[1][1]] = 1;
    dp[1][1][0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            for (int u = 0; u <= k; u++)
            {
                for (int v = 0; v <= 13; v++)
                {
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
                    if (u > 0 && v == g[i][j])
                    {
                        for (int c = 0; c < v; c++)
                        {
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u - 1][c]) % MOD;
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j][u - 1][c]) % MOD;
                        }
                    }
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= 13; i++)
        res = (res + dp[n][m][k][i]) % MOD;
    cout << res << endl;
    return 0;
}
```

### C
```cpp
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
            g[i][j]++;
        }
    }
    dp[1][1][1][g[1][1]] = 1;
    dp[1][1][0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            for (int u = 0; u <= k; u++)
            {
                for (int v = 0; v <= 13; v++)
                {
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
                    dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
                    if (u > 0 && v == g[i][j])
                    {
                        for (int c = 0; c < v; c++)
                        {
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][c]) % MOD;
                            dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
                        }
                    }
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= 13; i++)
        res = (res + dp[n][m][k][i]) % MOD;
    cout << res << endl;
    return 0;
}
```