题目【[SHOI2002]滑雪】:https://www.luogu.com.cn/problem/P1434
解法可以用记忆化搜索,也可以DP。
这里我选了DP,使用了优先队列存储每个高度,从而实现从低到高的递推。
1 #include<iostream> 2 #include<algorithm> 3 #include<queue> 4 using namespace std; 5 6 struct node { 7 int i, j, num; 8 } a; 9 struct cmp1 { 10 bool operator()(node x, node y) { 11 return x.num > y.num; 12 } 13 }; 14 int maxn, r, c, f[110][110], g[110][110]; 15 int main() { 16 priority_queue<node,vector<node>,cmp1> q; 17 cin >> r >> c; 18 for (int i = 1; i <= r; i++) { 19 for (int j = 1; j <= c; j++) { 20 cin >> g[i][j]; 21 f[i][j] = 1; 22 a.i = i, a.j = j, a.num = g[i][j]; 23 q.push(a); 24 } 25 } 26 int i, j; 27 while (q.size()) { 28 a = q.top(); q.pop(); 29 i = a.i, j = a.j; 30 if (g[i - 1][j] < a.num)f[i][j] = max(f[i][j], f[i - 1][j] + 1); 31 if (g[i + 1][j] < a.num)f[i][j] = max(f[i][j], f[i + 1][j] + 1); 32 if (g[i][j+1] < a.num)f[i][j] = max(f[i][j], f[i][j+1] + 1); 33 if (g[i][j-1] < a.num)f[i][j] = max(f[i][j], f[i ][j-1] + 1); 34 maxn = max(maxn, f[i][j]); 35 } 36 cout << maxn; 37 return 0; 38 }
原文:https://www.cnblogs.com/zyyz1126/p/12669045.html