【问题标题】:How to use only one time bfs to solve this algorithm?如何只使用一次 bfs 来解决这个算法?
【发布时间】:2017-03-26 02:22:19
【问题描述】:

问题是uva1599,访问https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4474

问题大致描述如下:

迷宫由通过 m 条路径连接的 n 个房间组成。每条路径都被着色为某种颜色 ci。找到从 1 号房间到 1 号房间的理想路径 房间号 n.如果它的颜色序列是最短路径中字典顺序最小的路径,则该路径是理想路径。(2

我可以从最后一个房间使用 bfs 来获取每个房间的最短路径,如果有多个路径长度相同但颜色不同的路径,我可以再次使用 bfs 从头开始​​搜索。

如何只用一个 bfs 来解决这个问题?

我尝试从最后一个房间开始,使用 bfs 获取每个房间的最短路径和颜色序列。理想路径存储在房间 1 的颜色序列中。

对吗?

谢谢。

【问题讨论】:

  • 你尝试了什么?
  • 我的代码在第一个答案中

标签: algorithm breadth-first-search


【解决方案1】:

这是我的代码。错误是运行时错误

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class IdealPath {
    //store the path between two rooms, if there is a path between room i and j, then graph[i][j] = 1
    public static int [][]graph;
    //store the color between two rooms
    public static int [][]color;
    //store the shortest path to the end room for each room
    public static int []distance;
    //store the ideal color sequence for each room
    public static String []color_seq;
    //use for bfs
    public static Queue<Integer> queue = new LinkedBlockingQueue<Integer>();

    /**
     * Initialize parameters
     * @param n
     */
    public static void init(int n){
        graph = new int[n][n];
        color = new int[n][n];
        distance = new int[n];
        color_seq = new String[n];
        queue.clear();
    }

    /**
     * use bfs to find the ideal path
     * @param n
     */
    public static void bfs(int n){
        //init the end room
        distance[n-1] = 0;
        color_seq[n-1] = "";
        queue.add(n-1);
        //loop until the queue is empty
        while(!queue.isEmpty()){
            int node = queue.poll();
            for (int i = 0; i < n; i++) {
                //there is a path
                if (graph[node][i] == 1){
                    //the room is not visited or is visited by the last floor
                    if (distance[i] == 0 || distance[i] == distance[node] + 1) {
                        distance[i] = distance[node] + 1;  //add the distance
                        String str = color[i][node] + " " + color_seq[node];  //generate the color sequence
                        if (color_seq[i] == null)
                            color_seq[i] = str.trim();
                        //choose the ideal color sequence for the room
                        else
                            color_seq[i] = compare(color_seq[i], str) ? str.trim() : color_seq[i].trim();
                        queue.add(i);
                    }
                }
            }
        }
    }

    /**
     * return true if str2 < str1
     * @param str1
     * @param str2
     * @return
     */
    public static boolean compare(String str1, String str2){
        String []arr2 = str2.trim().split(" ");
        String []arr1 = str1.trim().split(" ");
        //assume the lengths of arr1 and arr2 are same
        for (int i = 0; i < arr1.length; i++) {
            if (arr2[i].compareTo(arr1[i]) < 0)
                return true;
        }
        return  false;
    }

    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            //input
            int n = input.nextInt();
            int m = input.nextInt();
            init(n);
            for (int i = 0; i < m; i++) {
                int node1 = input.nextInt() - 1;
                int node2 = input.nextInt() - 1;
                //ignore the path to itself
                if (node1 != node2) {
                    graph[node1][node2] = graph[node2][node1] = 1;
                    int c = input.nextInt();
                    //choose the smallest path if it has multi path
                    color[node1][node2] = color[node2][node1] = (color[node1][node2] == 0) ? c : Math.min(c, color[node1][node2]);
                }
            }
            //use bfs to find the ideal path
            bfs(n);
            //output
            //the ideal path is stored in the color_seq[0]
            String res = color_seq[0].trim();
            System.out.println((res.length() + 1) / 2);
            System.out.println(res);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2020-03-27
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多