找到目标阵列Java的最短飞行时间

时间:2016-03-02 18:17:32

标签: java arrays

所以我被分配了这个怪物程序

public class AirlineRoutes
{
private int[][] routes;

/**
 *  Creates a table with direct flight times for a group of cities.
 */
public AirlineRoutes(int[][] routes)
{
    this.routes = routes;
}

/**
 *  Returns the shortest flying time from start to destination
 *  going through at most one intermediate city.
 *  @precondition 1 <= start <= N (where N is size of matrix)
 *  @precondition 1 <= destination <= N (where N is size of matrix)
 *      Note that cities are labeled 1 to N , whereas the matrix
 *      is indexed from 0 to N-1
 *  @param start the city to start the flight from
 *  @param destination the city to end the flight at
 *  @return returns the least number of hours going from start
 *          city to destination.  The flight can be direct or
 *          have a stop in one other city - Destination travleing
 *          through at most one intermediate city.
 */
public int minTime(int start, int destination)
{

}

/**
 *  Returns a string containing a chart showing the times
 *  to each city in hours.
 *  @return a table showing the time in hours for direct
 *          flights from every city.
 */
public String toString()
{
    String s = "\t\t\tDestination City\n\t\t";
    s += "\n\t\t\t";
    for (int k = 1; k <= routes[0].length; k++)
        s += "\t" + k;
    s += "\n\t\t";
    for (int k = 0; k <= routes[0].length; k++)
        s += "----";
    s += "-\n";
    for (int r = 0; r < routes[0].length; r++)
    {
        if (r == 1)
            s += "Start";
        else if (r == 2)
            s += "City";
        else
            s += "\t";
        s += "\t" + (r+1) + "\t|\t";
        for (int c = 0; c < routes[r].length; c++)
            s += routes[r][c] + "\t";               
        s += "\n";          
    }
    return s;
}

}

public class AirlineRoutesDriver
{
public static void main(String[] args)
{
    int[][] routes = new int[4][4];
    for (int k = 0; k < routes.length; k++)
        routes[k][k] = 0;
    routes[0][1] = 3;
    routes[0][2] = 2;
    routes[0][3] = 6;
    routes[1][0] = 3;
    routes[1][2] = 1;
    routes[1][3] = 3;
    routes[2][0] = 2;
    routes[2][1] = 1;
    routes[2][3] = 5;
    routes[3][0] = 5;
    routes[3][1] = 4;
    routes[3][2] = 2;

    AirlineRoutes r = new AirlineRoutes(routes);

    System.out.println(r);
    System.out.println("From city 4 to city 1  takes " + r.minTime(4,1) + " hours");
    System.out.println("From city 1 to 3 to city  " + r.minTime(1,3) + " hours");
    System.out.println("From city 3 to 3 to city  " + r.minTime(3,3) + " hours");
}
}

public class AirlineRoutesDriver
{
public static void main(String[] args)
{
    int[][] routes = new int[4][4];
    for (int k = 0; k < routes.length; k++)
        routes[k][k] = 0;
    routes[0][1] = 3;
    routes[0][2] = 2;
    routes[0][3] = 6;
    routes[1][0] = 3;
    routes[1][2] = 1;
    routes[1][3] = 3;
    routes[2][0] = 2;
    routes[2][1] = 1;
    routes[2][3] = 5;
    routes[3][0] = 5;
    routes[3][1] = 4;
    routes[3][2] = 2;

    AirlineRoutes r = new AirlineRoutes(routes);

    System.out.println(r);
    System.out.println("From city 4 to city 1  takes " + r.minTime(4,1) + " hours");
    System.out.println("From city 1 to 3 to city  " + r.minTime(1,3) + " hours");
    System.out.println("From city 3 to 3 to city  " + r.minTime(3,3) + " hours");
}
}

我不知道如何处理minTime()方法。我知道我必须找到从A点到B点的最短时间,但我不知道如何将其写入代码。

非常感谢你!

更新:我找到了算法,但我不知道如何将其翻译成java

更新2:我已经开始编写代码了,我将在下面发布。它还没有完成,我需要在if语句之后发出ctr++;语句,但我不确定要放入什么...然后我还需要返回一个int这个方法

public int minTime(int start, int destination)
{
 int ctr = 1;
 int ran = routes.length;
 int shortest = 0; //keeps track of the shortest time
 int longest = 0; //keeps track of the longest time
 int[][] otherArray = new int[routes.length][routes[ran].length]; //creates a new array to store the shortest time in
 for (int i = 0; i < routes.length; i++)
 {
    for (int j = 0; j < routes[i].length; j++)
    {
        otherArray[ctr] = otherArray[i][j+ctr] + otherArray[j+ctr][j];
        ctr++;
    }
 }
 //return some integer here!
}

1 个答案:

答案 0 :(得分:0)

您必须使用动态编程。

算法: 从A开始,计算到B的所有可能路径。选择最短路径。