标签:push first eid public record struct begin ack max
给你一个 R 行 C 列的整数矩阵 A。矩阵上的路径从 [0,0] 开始,在 [R-1,C-1] 结束。
路径沿四个基本方向(上、下、左、右)展开,从一个已访问单元格移动到任一相邻的未访问单元格。
路径的得分是该路径上的 最小 值。例如,路径 8 → 4 → 5 → 9 的值为 4 。
找出所有路径中得分 最高 的那条路径,返回其 得分。
class Solution {
private:
vector<int> root;
typedef struct {
int value;
pair<int, int> index;
} matrix;
static bool cmp(const matrix &m, const matrix &n) {
return m.value > n.value;
}
public:
int findRoot(int x)
{
if (root[x] == -1)
return x;
return findRoot(root[x]);
}
void unionRoot(int x, int y)
{
int a = findRoot(x);
int b = findRoot(y);
if (a != b) {
root[x] = y;
}
}
int maximumMinimumPath(vector<vector<int>>& A) {
int M = A.size();
int N = A[0].size();
if (M == 0 || N ==0) {
return 0;
}
root = vector<int>(M*N, -1);
matrix record;
vector<matrix> records;
for (int i = 0; i < M; i++){
for (int j = 0; j < N; j++) {
record.value = A[i][j];
record.index = make_pair(i, j);
records.push_back(record);
}
}
sort(records.begin(), records.end(), cmp);
int minValue = min(A[0][0], A[M-1][N-1]);
vector<vector<int>> tag(M, vector<int>(N, 0));
tag[0][0] = 1;
tag[M-1][N-1] = 1;
int bId = 0;
int eId = M * N - 1;
vector<vector<int>> dirs ={{0,1}, {0,-1}, {-1,0}, {1, 0}};
for (matrix record : records) {
int x = record.index.first;
int y = record.index.second;
int rootIndex = x * N + y;
tag[x][y] = 1;
minValue = min(minValue, record.value);
for (vector<int> id : dirs) {
int nx = x + id[0];
int ny = y + id[1];
if (nx >= 0 && nx < M && ny >= 0 && ny < N && tag[nx][ny] == 1) {
int rootNIndex = nx * N + ny;
unionRoot(rootIndex, rootNIndex);
}
}
if (findRoot(bId) == findRoot(eId)) {
break;
}
}
return minValue;
}
};
标签:push first eid public record struct begin ack max
原文地址:https://www.cnblogs.com/hunter-w/p/12790499.html