解题思路:
搜索 + DP。
参考代码:
#include<bits/stdc++>h> using namespace std; const int MOD = 1000000007; int Map[52][52]; int status[52][52][14][14]; int Maph, Mapw, quan; int DFS(int posx, int posy, int total, int Maxvalue) { if (status[posx][posy][total][Maxvalue] != -1) return status[posx][posy][total][Maxvalue]; if (posx == Maph && posy == Mapw) { if ((total == quan) || (total == quan - 1 && Map[Maph][Mapw] > Maxvalue)) return status[posx][posy][total][Maxvalue] = 1; return status[posx][posy][total][Maxvalue] = 0; } int cnt = 0; if (posx < Maph) { if (Map[posx][posy] > Maxvalue) cnt = (cnt + DFS(posx + 1, posy, total + 1, Map[posx][posy])) % MOD; cnt = (cnt + DFS(posx + 1, posy, total, Maxvalue)) % MOD; } if (posy < Mapw) { if (Map[posx][posy] > Maxvalue) cnt = (cnt + DFS(posx, posy + 1, total + 1, Map[posx][posy])) % MOD; cnt = (cnt + DFS(posx, posy + 1, total, Maxvalue)) % MOD; } return status[posx][posy][total][Maxvalue] = cnt % MOD; } int main() { scanf("%d%d%d", &Maph, &Mapw, &quan); memset(status, -1, sizeof(status)); for (int i1 = 1; i1 <= Maph; i1++) for (int i2 = 1; i2 <= Mapw; i2++) { scanf("%d", &Map[i1][i2]); Map[i1][i2]++; } printf("%d\n", DFS(1, 1, 0, 0)); }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1055 |
A+B for Input-Output Practice (VII) (C语言代码)浏览:1415 |
你的开发任务 (C++代码)写到一半,等有心情回来补全浏览:923 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:674 |
C二级辅导-等差数列 (C语言代码)浏览:1315 |
永远的丰碑 (C语言代码)浏览:698 |
C语言程序设计教程(第三版)课后习题8.2 (Java代码)浏览:2287 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:287 |
Pascal三角 (C语言代码)浏览:1252 |
简单的a+b (C语言代码)浏览:564 |