Algorithm 如何只使用一次bfs来求解该算法?

Algorithm 如何只使用一次bfs来求解该算法?,algorithm,breadth-first-search,Algorithm,Breadth First Search,问题是uva1599,请访问 问题大致描述如下: 迷宫由n个房间组成,由m条路径连接。每个路径都被着色为一些颜色ci。Fi 找到从1号房间到 房间号码。如果路径的颜色序列是最短路径中最小的,则该路径是理想路径。(2这是我的代码。错误是运行时错误 import java.util.*; import java.util.concurrent.LinkedBlockingQueue; public class IdealPath { //store the path between tw

问题是uva1599,请访问

问题大致描述如下:

迷宫由n个房间组成,由m条路径连接。每个路径都被着色为一些颜色ci。Fi 找到从1号房间到
房间号码。如果路径的颜色序列是最短路径中最小的,则该路径是理想路径。(2这是我的代码。错误是运行时错误

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);
        }
    }
}
import java.util.*;
导入java.util.concurrent.LinkedBlockingQueue;
公共类理想路径{
//存储两个房间之间的路径,如果房间i和j之间存在路径,则图形[i][j]=1
公共静态int[][]图;
//将颜色储存在两个房间之间
公共静态int[][]颜色;
//为每个房间存储到末端房间的最短路径
公共静态int[]距离;
//为每个房间储存理想的颜色序列
公共静态字符串[]颜色顺序;
//用于bfs
public static Queue Queue=new LinkedBlockingQueue();
/**
*初始化参数
*@param n
*/
公共静态void init(int n){
图=新整数[n][n];
颜色=新整数[n][n];
距离=新整数[n];
color_seq=新字符串[n];
queue.clear();
}
/**
*使用bfs找到理想路径
*@param n
*/
公共静态无效bfs(整数n){
//在休息室
距离[n-1]=0;
颜色顺序[n-1]=“”;
队列。添加(n-1);
//循环,直到队列为空
而(!queue.isEmpty()){
int node=queue.poll();
对于(int i=0;i
您尝试了什么?我的代码在第一个答案中