UVA 10285 - Longest Run on a Snowboard

本文介绍了一种通过先对所有点进行大小排序,再利用动态规划思想求解二维平面上从一个点到另一个点的最长路径的方法。适用于解决特定类型的算法问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

炫酷1A啊。


思路就是 把所有的点大小排序。 然后从最小的开始。 访问。 对于每一个点 他的步数 step 等于 周围比他小的点的 最大步数 加一。



#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
#define maxn 150+10
#define INF 1<<30
int d[5][3] = {{-1,0},{1,0},{0,-1},{0,1}};
struct po{
    int x,y;
    int v;
    friend bool operator < (po a, po b){
        return a.v < b.v;
    }
};
int main (){
    int counts;
    scanf("%d",&counts);
    while(counts--){
        string s;
        cin >> s;
        int m,n;
        int map1[maxn][maxn];
        int step[maxn][maxn] = {0};
        vector <po> p;
        p.clear();
        scanf("%d%d",&m,&n);
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                scanf("%d",&map1[i][j]);
                po x;
                x.v = map1[i][j];
                x.x = i;
                x.y = j;
                p.push_back(x);
            }
        }
        sort(p.begin(),p.end());
        int ma = -1;
        for(int i = 0; i < m * n; i++){
            int flag = 0;
            for(int j = 0; j < 4; j++){
                int xx = p[i].x + d[j][0];
                int yy = p[i].y + d[j][1];
                if(xx >=0 && xx < m && yy >= 0 && yy < n){
                    if(map1[p[i].x][p[i].y] > map1[xx][yy]){
                        flag = 1;
                        step[p[i].x][p[i].y] = max(step[p[i].x][p[i].y], step[xx][yy] + 1);
                    }
                }
            }
            if(!flag)
                step[p[i].x][p[i].y] += 1;
            ma = max(step[p[i].x][p[i].y], ma);
        }
        cout << s << ": ";
        printf("%d\n",ma);
    }
    return 0;
}


想到了这一点就好做了。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值