POJ 1088 滑雪 - (DP)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rsy56640/article/details/82557433

题目链接:http://poj.org/problem?id=1088

#include <stdio.h>  
#include <vector>   
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;

//POJ 1088 滑雪

int maxPath[102][102];
int matrix[102][102];

struct MyGreater {
    bool operator()(const pair<int, pair<int, int> >& lhs, const pair<int, pair<int, int> >& rhs) {
        return lhs.first >= rhs.first;
    }
};

inline int max4(int v[4])
{
    int max = v[0];
    if (max < v[1])max = v[1];
    if (max < v[2])max = v[2];
    if (max < v[3])max = v[3];
    return max;
}

int main()
{
    int R, C;
    scanf("%d%d", &R, &C);
    vector<pair<int, pair<int, int> > > c(R*C);
    int count = 0;
    for (int i = 1; i <= R; i++)
        for (int j = 1; j <= C; j++)
        {
            scanf("%d", &matrix[i][j]);
            c[count++] = make_pair(matrix[i][j], make_pair(i, j));
            maxPath[i][j] = 1; // no way to go
        }
    priority_queue <pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, MyGreater>
        pq(MyGreater(), c);
    int max_path = 0;
    while (!pq.empty())
    {
        pair<int, pair<int, int> > p = pq.top(); pq.pop();
        int h = p.first, x = p.second.first, y = p.second.second;
        int v[4];
        v[0] = matrix[x - 1][y] >= h ? -1 : maxPath[x - 1][y];
        v[1] = matrix[x + 1][y] >= h ? -1 : maxPath[x + 1][y];
        v[2] = matrix[x][y - 1] >= h ? -1 : maxPath[x][y - 1];
        v[3] = matrix[x][y + 1] >= h ? -1 : maxPath[x][y + 1];
        maxPath[x][y] = max4(v);
        if (maxPath[x][y] < 0) maxPath[x][y] = 1; // no way to go
        else maxPath[x][y]++;
        max_path = max(max_path, maxPath[x][y]);
    }
    printf("%d", max_path);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rsy56640/article/details/82557433
今日推荐