java—从扫描程序转换为插入函数中的节点参数

bpsygsoo  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(216)

我得到了下面的方法和我的main,在这里我调用这个方法写两个节点,它们也在我的main中声明,我希望每次运行main时都能直接写下我的节点的名称(我一直在尝试使用扫描仪,但我无法从扫描仪转换到节点),而不必手动在我的main中写“n1”或“n31”。
有没有比创建一个辅助方法更简单的解决方案,该方法搜索我可以从扫描器获得的字符串,直到它等于我正在搜索的节点?如果没有一个更简单的解决方案,我该如何编写搜索方法?
重要的是,我的节点看起来是这样的:node(string val,double hval)和我还有边edge(node targetnode,double costval),它们通过node.adjacencies与我的节点相关(作为节点的邻接也在main中声明)。
这是代码的相关部分:

public static void main(String[] args){

         Node n1 = new Node("Piraeus", 19);
         Node n2 = new Node("Faliro", 15); 

        n1.adjacencies = new Edge[]{
                new Edge(n2,4)
        };

        n2.adjacencies = new Edge[]{
                new Edge(n1,4),
                new Edge(n3,3)
        };

       AstarSearch(n1 , n31);

}

public static void AstarSearch(Node source, Node goal){

            Set<Node> explored = new HashSet<Node>();

            PriorityQueue<Node> queue = new PriorityQueue<Node>(20, 
                    new Comparator<Node>(){
                             //override compare method
             public int compare(Node i, Node j){
                if(i.f_scores > j.f_scores){
                    return 1;
                }

                else if (i.f_scores < j.f_scores){
                    return -1;
                }

                else{
                    return 0;
                }
             }

                    }
                    );

            //cost from start
            source.g_scores = 0;

            queue.add(source);

            boolean found = false;

            while((!queue.isEmpty())&&(!found)){

                    //the node in having the lowest f_score value
                    Node current = queue.poll();

                    explored.add(current);

                    //goal found
                    if(current.value.equals(goal.value)){
                            found = true;
                    }

                    //check every child of current node
                    for(Edge e : current.adjacencies){
                            Node child = e.target;
                            double cost = e.cost;
                            double temp_g_scores = current.g_scores + cost;
                            double temp_f_scores = temp_g_scores + child.h_scores;

                            /*if child node has been evaluated and 
                            the newer f_score is higher, skip*/

                            if((explored.contains(child)) && 
                                    (temp_f_scores >= child.f_scores)){
                                    continue;
                            }

                            /*else if child node is not in queue or 
                            newer f_score is lower*/

                            else if((!queue.contains(child)) || 
                                    (temp_f_scores < child.f_scores)){

                                    child.parent = current;
                                    child.g_scores = temp_g_scores;
                                    child.f_scores = temp_f_scores;

                                    if(queue.contains(child)){
                                            queue.remove(child);
                                    }

                                    queue.add(child);

                            }
                    }
            }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题