1. 程式人生 > 實用技巧 >小白進階之路-滑雪-暑假訓練

小白進階之路-滑雪-暑假訓練

搜尋依然爆炸的爛,繼續加油吧

題解:按照 h 排序的大根堆,每次尋找四周 h 小的接上dp串。

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

/*
 * time: 2020.7.23
 * thinking: 從 h 大的向 h 小的尋找,接上上一個的最長串
 * fighting and no pains no gains
 */
int a[200][200], dp[200][200];
int n, m;

struct node {
    int
x, y, h; bool operator<(const node &cmp) const { // 自定義排序函式,按 h 劃分的大根堆 return h < cmp.h; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; priority_queue<node> q; for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) { cin >> a[i][j]; dp[i][j] = 1; // 最小長度就是本身 q.push({i, j, a[i][j]}); } } int Max = -1; while (!q.empty()) { int xx = q.top().x, yy = q.top().y, hh = q.top().h; cout << hh << endl; q.pop();
int x = 0, b = 0, c = 0, d = 0; if (a[xx - 1][yy] > hh) x = dp[xx - 1][yy]; if (a[xx][yy - 1] > hh) b = dp[xx][yy - 1]; if (a[xx + 1][yy] > hh) c = dp[xx + 1][yy]; if (a[xx][yy + 1] > hh) d = dp[xx][yy + 1]; dp[xx][yy] = max(x, max(b, max(c, d))) + 1; Max = max(dp[xx][yy], Max); } cout << Max << endl; return 0; }