uva_10285 Longest Run on a Snowboard

本文介绍了一种结合动态规划与记忆化搜索的方法,用于解决特定类型的路径寻找问题。通过递归的方式预先计算并存储最优路径长度,避免了重复计算,提高了算法效率。代码示例展示了如何在二维网格中寻找从起点到终点的最大递增路径。
dp+记忆化搜索
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define DIR     4
#define MAXN    101

int dir[][2] = {
        {0, 1}, {0, -1}, {-1, 0}, {1, 0}
};
int h[MAXN][MAXN], dp[MAXN][MAXN], row, col;

int dfs(const int &x, const int &y)
{
        if( -1 != dp[x][y] ) {
                return dp[x][y];
        }
        int tx, ty, rst(0);
        for(int i = 0; i < DIR; i ++) {
                tx = x+dir[i][0]; ty = y+dir[i][1];
                if( tx < 0 || ty < 0 || tx >= row || ty >= col || h[x][y] <= h[tx][ty] ) {
                        continue;
                }
                dp[tx][ty] = (-1 == dp[tx][ty])? dfs(tx, ty) : dp[tx][ty];
                rst = max(rst, dp[tx][ty]);
        }
        return rst+1;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int cas, ans;
        char name[MAXN]; scanf("%d", &cas);
        while( cas -- ) {
                scanf("%s %d %d", name, &row, &col);
                for(int i = 0; i < row; i ++) {
                        for(int j = 0; j < col; j ++) {
                                scanf("%d", &h[i][j]);
                        }
                }
                memset(dp, -1, sizeof(dp)); ans = 0;
                for(int i = 0; i < row; i ++) {
                        for(int j = 0; j < col; j ++) {
                                dp[i][j] = (-1 == dp[i][j])? dfs(i, j) : dp[i][j];
                                ans = max(ans, dp[i][j]);
                        }
                }
                printf("%s: %d\n", name, ans);
        }
        return 0;
}

### Python 中 `zip_longest` 未定义的解决方案 在 Python 的不同版本中,`itertools.zip_longest()` 函数的行为有所不同。如果遇到 `zip_longest not defined` 错误,则可能是由于使用的 Python 版本不支持该方法或者导入方式有误。 #### 解决方案一:确认 Python 版本 在 Python 3.x 中,`zip_longest` 是作为 `itertools` 模块的一部分引入的。而在 Python 2.x 中,并不存在名为 `zip_longest` 的函数,取而代之的是 `izip_longest`[^1]。因此,在使用前需确保运行环境为 Python 3.x 并正确导入模块: ```python from itertools import zip_longest ``` #### 解决方案二:检查是否正确导入 即使是在 Python 3.x 下,也需要显式地从 `itertools` 导入 `zip_longest` 才能正常使用它。如果没有正确导入,就会引发 `NameError: name 'zip_longest' is not defined` 错误。以下是正确的导入语句以及用法示例: ```python from itertools import zip_longest lists = ['abc', range(5)] for item in zip_longest(*lists, fillvalue='-'): print(item) ``` 上述代码会输出如下结果: ``` ('a', 0) ('b', 1) ('c', 2) ('-', 3) ('-', 4) ``` 这里的关键在于通过参数 `fillvalue` 来指定当较短序列结束后的填充值,默认情况下这个值为空白字符(None)。这使得即便两个可迭代对象长度不一样也能正常配对处理。 #### 解决方案三:兼容性考虑 (针对旧版 Python 或未知版本场景) 为了使程序能够同时适用于 Python 2 和 Python 3,可以采用条件判断的方式来动态选择合适的工具函数: ```python try: from itertools import izip_longest as zip_longest # For Python 2 compatibility. except ImportError: from itertools import zip_longest # For Python 3. data = [[1, 2], ('X', 'Y', 'Z')] result = list(zip_longest(*data)) print(result) # Output will be [(1,'X'), (2,'Y'), (None,'Z')] under both Pythons versions with default None filling value. ``` 以上脚本首先尝试从 `itertools` 加载 `izip_longest` (这是 Python 2 中的功能),如果失败则加载 `zip_longest` 。这样就可以保证无论在哪种环境下都能找到对应的方法来实现功能需求.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值