Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在矩阵上寻找最短路径的问题_Java_Matrix - Fatal编程技术网

Java 在矩阵上寻找最短路径的问题

Java 在矩阵上寻找最短路径的问题,java,matrix,Java,Matrix,我写了程序的一部分,但找不到如何继续。这是我的家庭作业,我已经做了十天了,我的时间快到了。 我的课程要求: a) 通过关键字获取N作为输入。 b) 生成1到N*N之间的随机整数 c) 用这些整数填充矩阵 我已经这样做了,但我不能得到更多 越是=>贪婪的方法 例如,用户输入3作为输入。 和程序返回矩阵一样 1 2 6 4 8 5 397 最短路径为1,2,6,5,7。 另一个示例用户输入4作为输入,程序返回矩阵如下 141168 153161 10425 129713 最短路径可以是14,11,3

我写了程序的一部分,但找不到如何继续。这是我的家庭作业,我已经做了十天了,我的时间快到了。 我的课程要求: a) 通过关键字获取N作为输入。 b) 生成1到N*N之间的随机整数 c) 用这些整数填充矩阵 我已经这样做了,但我不能得到更多

越是=>贪婪的方法 例如,用户输入3作为输入。 和程序返回矩阵一样

1 2 6

4 8 5

397

最短路径为1,2,6,5,7。 另一个示例用户输入4作为输入,程序返回矩阵如下

141168

153161

10425

129713

最短路径可以是14,11,3,4,2,5,13

路径中不允许有交叉台阶

我的代码如下

import java.util.*;

public class Challenge1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a value for the matrix size.");
        int length = input.nextInt();
        int[][] x = randomMatrix(length);

        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                System.out.print(x[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] randomMatrix(int n) {
        Random r = new Random();
        int[][] matrix = new int[n][n];
        boolean[] trying = new boolean[n * n];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = r.nextInt(n * n) + 1;
                if (trying[matrix[i][j] - 1] == false)
                    trying[matrix[i][j] - 1] = true;

                else {
                    while (trying[matrix[i][j] - 1] == true) {
                        matrix[i][j] = r.nextInt(n * n) + 1;
                    }
                    trying[matrix[i][j] - 1] = true;
                }
            }
        }
        return matrix;
    }

}
import java.util.*;
公开课挑战者1{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入矩阵大小的值”);
int length=input.nextInt();
int[][]x=随机矩阵(长度);
对于(int i=0;i
下面是我在评论中提到的解决方案的一些python伪代码。让
shortestPath
最初为空列表,
shortestPathCost
为找到的最短路径的权重之和。最初它的值是
+无穷大
。两者都是全球可见的

 Procedure exhaustiveSearch (currentCost, currentPath, endNode):
      currentNode = currentPath.last
      if currentNode == endNode and currentCost < shortestPathCost:
           shortestPath = currentPath
           shortestPathCost = currentCost
           return

      for each neighbouringNode of currentNode not in currentPath:
           exhaustiveSearch (currentCost + neighbouringNode.cost, 
                             currentPath + neighbouringNode,
                             endNode)

最短路径
最短路径成本
将保留网格中的一条最短路径。显然,这个解决方案比
Java
本身要高一点,但它应该很容易实现。

这些数字对找到最短路径有什么意义吗?澄清一下,我不知道你所说的
越是=>邻居的数量将检查,并将继续与最小的。
这是不可理解的。该程序将检查邻居的数量,并将继续与最小的。我认为路径是指从左上角开始,在右下角结束;它的代价将是路径中的条目之和。@ZpCikTi我怀疑贪婪的aproach是否会产生全局最优值。哦,好吧,前面的例子(编辑之前)只是沿着一条路径的边缘走,当然没有最小的代价。我想你在最后添加了两次“NeightringNode”。编辑:没关系,你已经修复了。是的,我已经更改了一些参数,但没有更改调用。谢谢你的更正。非常感谢。我会尽力改正的。
 exhaustiveSearch(0, [firstNode], endNode);