路径搜索

给定一个二维正整数矩阵n * m,找到一条,从(0,0) 到(n-1, m-1)的路径,使得路径上的最小值最大,并输出该值。

华为笔试题,碰到个路径搜索题,一看就觉得可能要用dijkstra, 尴尬的是当时没有实现出来。

细节问题忽略,大致主要思路如下,采用从终点到起点到反向搜索方法,每次去最大的点作为下一步路径,并将该点加入群体,(该群体是遍历过的点,已经知道点到终点到最好结果),然后找这个群体最优的解,直到起点。

贴代码:(一些必要的判断忽略)

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 给定一个二维正整数矩阵n * m,找到一条,从(0,0) 到(n-1, m-1)的路径,使得路径上的最小值最大,并输出该值
 *
 * ex:
 *
 * 4,4,5,6
 * 2,4,7,3
 * 1,1,2,5
 * 3,4,5,6
 *
 * out: 3  ; 4->4->5->7->3->5->6
 */

public class SearchRoad {
    public static void main(String[] args) {
        int [][] matrix = new int[][]{{4,4,5,6},{2,4,7,3},{1,1,2,5},{3,4,5,6}};

        int max = findMaxRoad(matrix);
        System.out.println(max);
    }

    private static int findMaxRoad(int[][] matrix) {
        int max = 0;
        // 存已经到过到点
        Map<Integer, Set<Integer>> arrNode = new HashMap<Integer, Set<Integer>>();
        int r = matrix.length;
        int c = matrix[0].length;
        int tempMax = matrix[r -1][c - 1];
        arrNode.put(r-1, new HashSet<>());
        arrNode.get(r-1).add(c-1);
        boolean arrive = false;
        while (!arrive){

            int x=-1,y =-1;
            int thisNodeMax = -1;
            for (Map.Entry<Integer, Set<Integer>> node : arrNode.entrySet()) {
                for (int ceil : node.getValue()){
                    // 搜索当前节点到四个方向的节点,并记录最大值,以及最大值对应大节点,将该节点加入arrNode
                    // 上
                    if (node.getKey() - 1 >= 0) {
                        if (arrNode.containsKey(node.getKey() - 1) && arrNode.get(node.getKey() - 1).contains(ceil)) {
                            // 已经在arrnode
                        } else {
                            int m = matrix[node.getKey() - 1][ceil];
                            if (m > thisNodeMax) {
                                x = node.getKey() - 1;
                                y = ceil;
                                thisNodeMax = m;
                            }
                        }
                    }
                    // 下
                    if (node.getKey() + 1 < r) {
                        if (arrNode.containsKey(node.getKey() + 1) && arrNode.get(node.getKey() + 1).contains(ceil)) {
                            // 已经在arrnode
                        } else {
                            int m = matrix[node.getKey() + 1][ceil];
                            if (m > thisNodeMax) {
                                x = node.getKey() + 1;
                                y = ceil;
                                thisNodeMax = m;
                            }
                        }
                    }
                    // 左
                    if (ceil - 1 >= 0) {
                        if (arrNode.containsKey(node.getKey()) && arrNode.get(node.getKey()).contains(ceil-1)) {
                            // 已经在arrnode
                        } else {
                            int m = matrix[node.getKey()][ceil - 1];
                            if (m > thisNodeMax) {
                                x = node.getKey();
                                y = ceil - 1;
                                thisNodeMax = m;
                            }
                        }
                    }
                    // 右
                    if (ceil + 1 < c) {
                        if (arrNode.containsKey(node.getKey()) && arrNode.get(node.getKey()).contains(ceil +1)) {
                            // 已经在arrnode
                        } else {
                            int m = matrix[node.getKey()][ceil + 1];
                            if (m > thisNodeMax) {
                                x = node.getKey();
                                y = ceil + 1;
                                thisNodeMax = m;
                            }

                        }
                    }
                }


            }

            if (x != -1 && y != -1){
                if (thisNodeMax < tempMax){
                    tempMax = thisNodeMax;
                }

                if (!arrNode.containsKey(x)){
                    arrNode.put(x, new HashSet<>());
                }
                // 添加节点
                arrNode.get(x).add(y);
                System.out.println(x + "--"+y + ":" + thisNodeMax);

                if (x == 0 && y ==0){
                    arrive = true;
                }

            }else {
                max = 0;
                arrive = true;
            }

        }


        return tempMax;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值