UVa 10285 - Longest Run on a Snowboard

记忆化搜索,对d[i][j] 需要考虑 d[i - 1][j], d[i + 1][j], d[i][j - 1], d[i][j + 1]四种情况,但是需要注意边界的控制。

 

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int Case, R, C, A[105][105], opt[105][105], d[105][105];
string s;

int dp ( int r, int c ) {
    if ( opt[r][c] ) return d[r][c];
    opt[r][c] = 1;
    int ans1, ans2, ans3, ans4;
    ans1 = ans2 = ans3 = ans4 = 1;
    if ( 1 <= c - 1 && c - 1 <= C && A[r][c] > A[r][c - 1] ) ans1 = max ( ans1, dp ( r, c - 1 ) + 1 );
    if ( 1 <= c + 1 && c + 1 <= C && A[r][c] > A[r][c + 1] ) ans2 = max ( ans2, dp ( r, c + 1 ) + 1 );
    if ( 1 <= r - 1 && r - 1 <= R && A[r][c] > A[r - 1][c] ) ans3 = max ( ans3, dp ( r - 1, c ) + 1 );
    if ( 1 <= r + 1 && r + 1 <= R && A[r][c] > A[r + 1][c] ) ans4 = max ( ans4, dp ( r + 1, c ) + 1 );
    d[r][c] = max ( ans1, max ( ans2, max ( ans3, ans4 ) ) );
    return d[r][c];
}

int main ( ) {
    //freopen ( "input.txt", "r", stdin );
    //freopen ( "output.txt", "w", stdout );
    cin >> Case;
    while ( Case-- ) {
        memset ( A, 0, sizeof ( A ) );
        cin >> s >> R >> C;
        for ( int i = 1; i <= R; ++i )
            for ( int j = 1; j <= C; ++j )
                cin >> A[i][j];
        memset ( opt, 0, sizeof ( opt ) );
        memset ( d, 0, sizeof ( d ) );
        int Max = -1;
        for ( int i = 1; i <= R; ++i ) {
            for ( int j = 1; j <= C; ++j ) {
                int tmp = dp ( i, j );
                //cout << tmp << " ";
                if ( Max < tmp ) Max = tmp;
            }
            //cout << endl;
        }
        cout << s << ": " << Max << endl;
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值