poj1088滑雪--dp之记忆化搜索

来源:互联网 发布:大数据基金有哪些 编辑:程序博客网 时间:2024/05/23 00:01

代码如下:

#include <cstdio>#include <algorithm>#include <cstring>#define maxn 105using namespace std;int dx[4] = { 1, -1, 0, 0}, dy[4] = { 0, 0, 1, -1}, r, c, dp[maxn][maxn] = {0}, a[maxn][maxn];int search( int x, int y){    if ( dp[x][y]){        return dp[x][y];    }    dp[x][y] = 1;    for ( int k = 0; k < 4; k++){        int tx = x + dx[k], ty = y + dy[k];        if ( tx > 0 && tx <= r && ty > 0 && ty <= c && a[x][y] > a[tx][ty]){            dp[x][y] = max( dp[x][y], search( tx, ty) + 1);        }    }    return dp[x][y];}int main(){    scanf( "%d%d", &r, &c);    for ( int i = 1; i <= r; i++){        for ( int j = 1; j <= c; j++){            scanf( "%d", &a[i][j]);        }    }    int m = 1;    for ( int i = 1; i <= r; i++){        for ( int j = 1; j <= c; j++){            m = max( m, search( i, j));        }    }    printf( "%d\n", m);    return 0;}


0 0
原创粉丝点击