POJ - 1088 滑雪 dp

 http://bailian.openjudge.cn/practice/1088?lang=en_US

题解:

 设一个dp[N][N]数组代表从(i,j)坐标开始能滑到的最远距离。
更新的方法为 遍历每个dp[i][j], 对其四周(下一个状态)的点更新。
更新顺序为从低到高。所以用priority_queue存数据。

坑:
  一开始更新的时候没有写dp=max(dp,····)
  也忘记了答案是dp数组的最大值
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<cstring>
#include<set>
#include<algorithm>
#include<stack>
#include<string>
#include<cstdio>
#include<list>
#include<cstdlib>
#include<queue>

#define _for(i, a, b) for (int i = (a); i<(b); ++i)
using namespace std;
const int N = 100 + 5;
const int INF = 1e6;
int map[N][N];
int dp[N][N];//从i,j开始能滑到的最远距离。dp[i][j] 对其四周的点更新。更新顺序为从高到底。
struct node {
    int x, y, w;
    node(int x = 0, int y = 0, int w = 0) :x(x), y(y), w(w) {}
    bool operator<  (const node &b)const {
        return w > b.w;
    }
};
int dir[4][2] = { 1,0, 0,-1, -1,0, 0,1 };
priority_queue<node> Q;
int main() {
    int c, r;
    cin >> r >> c;
    _for(i, 0, r)
        _for(j, 0, c) {
        cin >> map[i][j];
        Q.push(node(i, j, map[i][j]));
        dp[i][j] = 1;
    }
    int ans = 0;

    while (!Q.empty()) {
        node now = Q.top();

        ans = max(ans, dp[now.x][now.y]);

        Q.pop();
        for (int i = 0; i < 4; i++) {
            int dx = now.x + dir[i][0];
            int dy = now.y + dir[i][1];
            if (dx<0 || dx >= r || dy<0 || dy >= c)continue;
            if (map[dx][dy] > now.w) dp[dx][dy] = max(dp[dx][dy], dp[now.x][now.y] + 1);

        }
    }
    cout << ans;
    //while (!Q.empty()) {cout << Q.top().w;Q.pop();}
    system("pause");
}

 

posted @ 2018-04-08 11:21  SuuTTT  阅读(180)  评论(0编辑  收藏  举报