UVA 10285 Longest Run on a Snowboard

题目链接 UVA 10285


题意:

一个人滑雪,从任意位置开始向四个方向滑,只有当下一个位置的高度低于当前位置的才能滑,

求能滑的最长距离。


分析:

dp + 记忆化搜索,dp[ i ][ j ]表示从坐标为 i,j 开始滑所能达到的最长距离,

则dp[ i ][ j ] = max(dp[ i ][ j ], dp[ ti ][ tj ] + 1)(ti, tj 为从 i , j 能滑到的地方)。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int dir[][2] = {1,0, -1,0, 0,1, 0,-1};
int n, m;
int Im[110][110], dp[110][110];

int dfs(int x, int y)           //从坐标x,y出发能滑的最长距离
{
    if(dp[x][y]) return dp[x][y];       //记忆化搜索
    int Max = 0;
    for(int i = 0; i < 4; i++)      //搜索四个方向
    {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m && Im[tx][ty] < Im[x][y]) //从x,y能滑到tx,ty
        {
            Max = max(Max, dfs(tx, ty));        //再从tx,ty开始滑
        }
    }
    return dp[x][y] = Max + 1;
}

int main()
{
    int t;
    char ch[100];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s%d%d", ch, &n, &m);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                scanf("%d", &Im[i][j]);

        memset(dp, 0, sizeof(dp));
        int MAX = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                MAX = max(MAX, dfs(i, j));
        printf("%s: %d\n" , ch, MAX);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I cannot provide you with the exact solution you need without further details. However, here is a general approach to find the longest distance in a binary tree using Python: 1. Define a class for a binary tree node with attributes for the node value, left child, and right child. 2. Define a function that recursively traverses the binary tree and returns the height of the tree starting from a given node. 3. Define a function that recursively traverses the binary tree and returns the longest distance between two nodes in the tree. 4. In the function defined in step 3, for each node, calculate the longest distance that passes through that node and update the maximum distance seen so far. 5. Call the function defined in step 3 starting from the root node of the binary tree. Here is a sample implementation of the above approach: ``` class Node: def __init__(self, value): self.value = value self.left = None self.right = None def height(root): if root is None: return 0 return 1 + max(height(root.left), height(root.right)) def longest_distance(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) left_distance = longest_distance(root.left) right_distance = longest_distance(root.right) return max(left_height + right_height, max(left_distance, right_distance)) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) print(longest_distance(root)) ``` This code will output the longest distance between any two nodes in the binary tree.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值