记忆化搜索
p1434 滑雪 https://www.luogu.org/problem/P1434
总结:
1.将每个点都搜一遍,用dp数组记录每个点的最大解
#include<bits/stdc++.h> using namespace std; int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; int mapp[105][105], dp[105][105]; int maxx, r, c; int dfs(int x, int y) { if(dp[x][y] != 0) return dp[x][y]; int t, maxt = 1; for(int i = 0; i < 4; i++) { int nx = x+dx[i]; int ny = y+dy[i]; if(nx > 0 && ny > 0 && nx <= r && ny <= c && mapp[nx][ny] > mapp[x][y]) { t = dfs(nx, ny) + 1; maxt = max(t, maxt); } } dp[x][y] = maxt; return maxt; } int main() { memset(dp, 0, sizeof(dp)); cin >> r >> c; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) cin >> mapp[i][j]; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) { dp[i][j] = dfs(i, j); maxx = max(maxx, dp[i][j]); } cout << maxx; return 0; }