Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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 在1';s和0';s_Java_Algorithm_Data Structures - Fatal编程技术网

Java 在1';s和0';s

Java 在1';s和0';s,java,algorithm,data-structures,Java,Algorithm,Data Structures,我有一个由1和0组成的二维数组,其中左上角和右下角用1填充,索引的其余部分用0和1填充 a[]]={{1,1,0,0,1}{0,1,1,0,0}{0,1,0,1,0}{0,1,1,1} 编写一个程序/算法,返回另一个大小相同的二维数组,包含从头到尾的路径 输出[][]={{1,1,0,0,0,0}{0,1,0,0,0}{0,1,0,0,0}{0,1,1,1} 请帮我解决上述问题。此外,该程序还应适用于其他输入序列。也许我可以为您提供更清晰版本的回溯算法。她说:“你去哪里 import java.

我有一个由1和0组成的二维数组,其中左上角和右下角用1填充,索引的其余部分用0和1填充

a[]]={{1,1,0,0,1}{0,1,1,0,0}{0,1,0,1,0}{0,1,1,1}

编写一个程序/算法,返回另一个大小相同的二维数组,包含从头到尾的路径

输出[][]={{1,1,0,0,0,0}{0,1,0,0,0}{0,1,0,0,0}{0,1,1,1}


请帮我解决上述问题。此外,该程序还应适用于其他输入序列。

也许我可以为您提供更清晰版本的回溯算法。她说:“你去哪里

import java.util.Arrays;

public class Backtrack {

    public static void main(String... args) {
        int[][] inputArray = {
                { 1, 1, 1, 0, 0 },
                { 0, 1, 1, 0, 0 },
                { 0, 1, 0, 1, 0 },
                { 0, 1, 1, 1, 1 } };
        int[][] findPath = findPath(inputArray);
        System.out.println(Arrays.deepToString(findPath));

    }

    public static int[][] findPath(int[][] maze) {
        int[][] solution = new int[maze.length][];
        for (int i = 0; i < maze.length; i++) {
            solution[i] = new int[maze[i].length];
        }
        if (!findPath(maze, solution, 0, 0)) {
            System.out.println("Didn't find a solution.");
        }
        return solution;
    }

    private static boolean findPath(int[][] maze, int[][] solution, int x, int y) {
        if (0 <= y && y < maze.length && 0 <= x && x < maze[y].length) {
            if (y == maze.length - 1 && x == maze[y].length - 1) {
                solution[y][x] = 1;
                return true;
            } else if (solution[y][x] != 1 && maze[y][x] == 1) {
                solution[y][x] = 1;
                if (findPath(maze, solution, x, y + 1)
                        || findPath(maze, solution, x + 1, y)
                        || findPath(maze, solution, x - 1, y)
                        || findPath(maze, solution, x, y - 1)) {
                    return true;
                }
                solution[y][x] = 0;
            }
        }
        return false;
    }
}
导入java.util.array;
公共类回溯{
公共静态void main(字符串…参数){
int[][]输入阵列={
{ 1, 1, 1, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1 } };
int[][]findPath=findPath(输入阵列);
System.out.println(Arrays.deepToString(findPath));
}
公共静态int[]findPath(int[]maze){
int[][]解决方案=新int[maze.length][];
对于(int i=0;i如果(0你尝试了什么?你被困在哪里了?一天的另一个作业:)@vish4071。另一方面,缺乏研究工作是……是的,没错,顺便说一句。@Santosh,使用bfs。你能发布你的解决方案吗?可能有点缺陷。