nefu 18 滑雪

来源:互联网 发布:编程用什么软件比较好 编辑:程序博客网 时间:2024/06/05 07:27

http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=18


题意: 找一条从高到低的最长的路的长度;

思路:记忆化搜索,但是不知道在那个地方开始或者结束,所以所有的点都走一遍:


#include<iostream>#include<cstdio>#include<cstring>#include<stdlib.h>#include<algorithm>using namespace std;const int maxn = 100 + 10;int dp[maxn][maxn];int a[maxn][maxn];int derict[][2] ={-1,0,1,0,0,1,0,-1};int n, m;int dfs(int x, int y){if (dp[x][y] != -1)return dp[x][y];int maxs = 0;for (int i = 0; i < 4; i++){int xx = x + derict[i][0], yy = y + derict[i][1];if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[x][y] > a[xx][yy]){maxs = max(maxs, dfs(xx, yy));}}return dp[x][y] = maxs + 1;}int main(){while (~scanf("%d%d", &n, &m)){memset(dp, -1, sizeof(dp));for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++)scanf("%d",&a[i][j]);}for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++)dfs(i, j);}int ans = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){ans = max(ans, dp[i][j]);}}cout << ans << endl;}return 0;}


0 0