Codevs 2152 滑雪 记忆化搜索DP

来源:互联网 发布:js div添加class 编辑:程序博客网 时间:2024/05/29 11:20

Codevs 2152 滑雪

dp[i][j] 为从 i, j 开始走能走多长距离。

代码

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;#define MAXN (100+10)const int dx[] = {0,1,0,-1,0};const int dy[] = {0,0,1,0,-1};int n, m;int maps[MAXN][MAXN];int dp[MAXN][MAXN];int dfs(int x, int y){    if(dp[x][y]) return dp[x][y];    int ans = 1;    for(int i = 1; i <= 4; i ++)    {        int nx = x + dx[i];        int ny = y + dy[i];        if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && maps[nx][ny] < maps[x][y])            ans = max(ans, dfs(nx, ny)+1);    }    dp[x][y] = ans;    return ans;}int main(){    cin >> n >> m;    for(int i = 1; i <= n; i ++)        for(int j = 1; j <= m; j ++)            scanf("%d", &maps[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