zoukankan      html  css  js  c++  java
  • 1102. 得分最高的路径

    描述

    给你一个 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] == x)
                return x;
            return findRoot(root[x]);
        }
    
        void unionRoot(int x, int y)
        {
            int a = findRoot(x);
            int b = findRoot(y);
            if (a != b) {
                root[a] = b;
            }
        }
    
        int maximumMinimumPath(vector<vector<int>>& A) {
            int M = A.size();
            if (M == 0)
                return 0;
            int N = A[0].size();
            if (N == 0)
                return 0;
    
            root = vector<int>(M*N, 0);
            for(int i=0; i<M*N; i++) {
                root[i] = i;
            }
    
            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;
        }
    };
    
  • 相关阅读:
    testng失败截图,注解方式调用。
    HttpURLConnection和HttpClient
    JDK中的URLConnection参数详解
    如何做好Web接口测试
    selenium webdriver定位不到元素的五种原因及解决办法
    Selenium(Webdriver)自动化测试常问到的问题解答(转自:潜龙0318)
    常用网址记录
    python set
    python 浅拷贝和深拷贝
    python 元组
  • 原文地址:https://www.cnblogs.com/hunter-w/p/12790499.html
Copyright © 2011-2022 走看看