您的位置:首页 > 其它

UVa 10285 - Longest Run on a Snowboard

2013-05-03 17:54 399 查看
记忆化搜索,对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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: