LeetCode 329 - 矩阵中的最长递增路径

题目链接:LeetCode 329 - 矩阵中的最长递增路径

思路

类似 计蒜客 42397 - Digital Path(ICPC 2019 南京现场赛 C 题)

如果相邻的 \(x < y\),建一条从 \(x\) 的坐标到 \(y\) 的坐标的有向边,对图进行拓扑排序,然后按照拓扑顺序来 DP。

定义 \(f(x, y)\) 是以 \((x, y)\) 结尾的满足条件的最长路径。

转移方程:

\[f(x, y) = \max(f(x, y), f(x_p, y_p) + 1)\]

当且仅当 \((x_p, y_p)\)\((x, y)\) 有边的时候发生转移。

所有的点都可以初始化 \(f(x, y) = 1\)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
struct Coordinate {
int x, y;
};

class Solution {
public:
static constexpr int dx[] = {0, 0, -1, 1};
static constexpr int dy[] = {-1, 1, 0, 0};
static constexpr int MAX_N = 200 + 5;

vector<vector<int>> a, in_deg, f;
int n, m;
Coordinate s[MAX_N * MAX_N];

bool is_valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}

void get_topological_order() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int dir = 0; dir < 4; ++dir) {
int nx = i + dx[dir], ny = j + dy[dir];
if (is_valid(nx, ny) && a[i][j] < a[nx][ny]) {
++in_deg[nx][ny];
}
}
}
}

queue<Coordinate> q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (in_deg[i][j] == 0) {
q.push({i, j});
}
}
}

int s_ptr = 0;
while (!q.empty()) {
auto frt = q.front();
q.pop();
s[s_ptr++] = frt;

for (int dir = 0; dir < 4; ++dir) {
int nx = frt.x + dx[dir], ny = frt.y + dy[dir];
if (is_valid(nx, ny) && a[frt.x][frt.y] < a[nx][ny] && (--in_deg[nx][ny]) == 0) {
q.push({nx, ny});
}
}
}
}

int longestIncreasingPath(vector<vector<int>>& a_) {
a = a_;
n = a.size();
m = a[0].size();
in_deg = vector<vector<int>>(n, vector<int>(m, 0));
f = vector<vector<int>>(n, vector<int>(m, 0));

get_topological_order();

int tot = n * m, ans = 0;
for (int i = 0; i < tot; ++i) {
f[s[i].x][s[i].y] = 1;
for (int dir = 0; dir < 4; ++dir) {
int pre_x = s[i].x + dx[dir], pre_y = s[i].y + dy[dir];
if (is_valid(pre_x, pre_y) && a[pre_x][pre_y] < a[s[i].x][s[i].y]) {
f[s[i].x][s[i].y] = max(f[s[i].x][s[i].y], f[pre_x][pre_y] + 1);
}
}
ans = max(ans, f[s[i].x][s[i].y]);
}

return ans;
}
};