Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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_Recursion_Overwrite_Maze - Fatal编程技术网

Java 修改发送到布尔函数中的原始字符[]中的数据副本时,该数据将被覆盖

Java 修改发送到布尔函数中的原始字符[]中的数据副本时,该数据将被覆盖,java,recursion,overwrite,maze,Java,Recursion,Overwrite,Maze,显然,我需要一些帮助,但我不知道代码中发生了什么。看起来很直截了当,但就是说不通 我的理解是java没有指针C++的方式,所以我的问题就开始了。 我有一个私有静态字符[],它是一个类变量,称为“maze”。简单。没有什么地方会被修改,除非给它填充数据 之后,它被发送到一个递归算法中,该算法将迷宫作为“mz”进行求解,同时将“maze”作为参考/映射,在“mz”中发送的一个用作步骤映射,每次在某个磁贴/单元上执行步骤时,都会使用标记进行修改 现在的问题是,当我再次尝试绘制迷宫时,它显示的是修改了值

显然,我需要一些帮助,但我不知道代码中发生了什么。看起来很直截了当,但就是说不通

<>我的理解是java没有指针C++的方式,所以我的问题就开始了。 我有一个私有静态字符[],它是一个类变量,称为“maze”。简单。没有什么地方会被修改,除非给它填充数据

之后,它被发送到一个递归算法中,该算法将迷宫作为“mz”进行求解,同时将“maze”作为参考/映射,在“mz”中发送的一个用作步骤映射,每次在某个磁贴/单元上执行步骤时,都会使用标记进行修改

现在的问题是,当我再次尝试绘制迷宫时,它显示的是修改了值的步骤图“mz”,而不是自递归开始以来从未接触过的“迷宫”

提前谢谢

代码如下:

import java.io.*;
import java.util.*;

public class Maze {

    public static int   sizeX = 10, // Width of the maze
                        sizeY = 10, // Height of the maze
                        startX = 5, // Starting point on the X-axis of the maze
                        startY = 5, // Starting point on the Y-axis of the maze
                        winStep = 98; // Number of steps needed for a feasible maze and must be even or the path will be open at the end 
    private static int[][] path = new int[winStep][2]; // Placeholder for list of co-ordinates on the path
        // Path[  N  ][  0  ] = X
        // Path[  N  ][  1  ] = Y
    private static char[][] maze; // Placeholder for the maze map, and the step map
        // Maze[ Row ][ Col ]
        // Maze[  Y  ][  X  ] , necessary to know for traversing the maze

    public static int totalSteps = 0; // Troubleshoot code, used to see how far algorithm goes in the maze in case it fails


    public static void drawMaze() {

        System.out.printf("  "); // Spacing before drawing X-axis
        for( int i = 0 ; i < sizeX ; i++)
            System.out.printf(i + "  "); // Draw X-axis
        System.out.println();

    // Draws the maze from left to right (i = 0 to sizeX-1), top to bottom (j = 0 to sizeY-1)
        for( int i = 0 ; i < sizeX ; i++ ) {
            System.out.printf(i + " "); // Draw Y-axis
            for( int j = 0 ; j < sizeY ; j++ ) {
                System.out.print(maze[i][j] + "  ");                
            }
            System.out.println(); // Output formatting
        }
        System.out.println(); // Output formatting

        return;
        // End of drawMaze()
    }

    public static boolean pathfinder2(char[][] mz, int x, int y, int step, char oDir, char nDir) { 

    // Check if you've completed the maze yet
        if ( step == winStep - 15 ) { // Troubleshoot code
//      if ( x == startX && y == startY && step == winStep ) {
            path[step-1][0] = x;
            path[step-1][1] = y;
            return true;
        }

    // Preliminary check to make the path is still in the maze  
        if( x < 0 || x >= sizeX || y < 0 || y >= sizeY ) {
            return false;
        }

    // Check where you've ended up
        if( step > 0 ) { // Check these scenarios after the first step

        // Checking previous steps
            if(     ( nDir == 'u' && ( (oDir == 'u' && maze[y+1][x] == '/') || (oDir == 'u' && maze[y+1][x] == '\\') ) )    // If you just moved UP, but you're last step was also UP and the last tile was a '/' or '\'
                ||  ( nDir == 'r' && ( (oDir == 'r' && maze[y][x-1] == '/') || (oDir == 'r' && maze[y][x-1] == '\\') ) )    // If you just moved RIGHT, but you're last step was also RIGHT and the last tile was a '/' or '\'
                ||  ( nDir == 'd' && ( (oDir == 'd' && maze[y-1][x] == '/') || (oDir == 'd' && maze[y-1][x] == '\\') ) )    // If you just moved DOWN, but you're last step was also DOWN and the last tile was a '/' or '\'
                ||  ( nDir == 'l' && ( (oDir == 'l' && maze[y][x+1] == '/') || (oDir == 'l' && maze[y][x+1] == '\\') ) ) )  // If you just moved LEFT, but you're last step was also LEFT and the last tile was a '/' or '\'
                        { return false; }
        // Checking current steps
            else
                if( maze[y][x] == 'X' ) { // If you've walked into a Block-Tile 
                    return false;
                }else
                    if( x == startX && y == startY && step != winStep ) { // If you've ended up at the starting position but have not taken enough steps
                        return false;
                    }else
                        if( nDir == 'u' // If you've just moved UP from a lower tile
                        && ( maze[y][x] == 'U' || maze[y][x] == 'R' || maze[y+1][x] == 'D' || maze[y+1][x] == 'L' || mz[y][x] == 'S' ) ) { // If you've just walked through a ramp wall or a previously stepped on tile 
                                return false;
                        }else
                            if( nDir == 'r' // If you've just moved RIGHT from a lower to the left
                            && ( maze[y][x-1] == 'U' || maze[y][x] == 'R' || maze[y][x] == 'D' || maze[y][x-1] == 'L' || mz[y][x] == 'S' ) ) { // If you've just walked through a ramp wall or a previously stepped on tile 
                                    return false;
                            }else
                                if( nDir == 'd' // If you've just moved DOWN from an upper tile
                                && ( maze[y-1][x] == 'U' || maze[y-1][x] == 'R' || maze[y][x] == 'D' || maze[y][x] == 'L' || mz[y][x] == 'S' ) ) { // If you've just walked through a ramp wall or a previously stepped on tile 
                                        return false;
                                }else
                                    if( nDir == 'l' // If you've just moved LEFT from a tile to the right
                                    && ( maze[y][x] == 'U' || maze[y][x+1] == 'R' || maze[y][x+1] == 'D' || maze[y][x] == 'L' || mz[y][x] == 'S' ) ) { // If you've just walked through a ramp wall or a previously stepped on tile 
                                            return false;
                                    }else { // No obstacles were in the way. It is okay to continue

                                        if( step > totalSteps ) {
                                            totalSteps = step; } // Troubleshoot code
                                        System.out.println( "Step " + step + ": " + nDir + " " + x + " , " + y + " [" + maze[y][x] + "] x"); // Troubleshoot code

                                        if( mz[y][x] != 's' && ( maze[y][x] == '/' || maze[y][x] == '\\' ) ) { // If the '/' or '\' tile is not stepped on yet
                                            mz[y][x] = 's'; // Mark the tile as half stepped on and continue
                                        }else {
                                            mz[y][x] = 'S'; // Mark the tile as fully stepped on and continue
                                        }
                                    }
        }

    // MOVE
        if( pathfinder2( mz , x , y-1 , step+1 , nDir, 'u' ) ) { // Go North / Up
            path[step] = new int[] { x , y };
            return true;
        }else
            if( pathfinder2( mz , x+1 , y , step+1 , nDir, 'r' ) ) { // Go East / Right
                path[step] = new int[] { x , y };
                return true;
            }else
                if( pathfinder2( mz , x , y+1 , step+1 , nDir, 'd' ) ) { // Go South / Down
                    path[step] = new int[] { x , y };
                    return true;
                }else
                    if( pathfinder2( mz , x-1 , y , step+1 , nDir, 'l' ) ) { // Go West / Left
                        path[step] = new int[] { x , y };
                        return true;
                    }else { // Reaching this point means you've reached a dead-end

                        if( mz[y][x] == 'S' && ( maze[y][x] == '/' || maze[y][x] == '\\' ) ) { // If the '/' or '\' was fully stepped on previously
                            mz[y][x] = 's'; // Unmark the full stepped, and mark it as half stepped on
                        }else {
                            mz[y][x] = '-'; // Unmark the tile as stepped on and continue
                        }
                        return false;
                    }

        // End of pathfinder2()
    }

    public static void main(String args[]) throws IOException {

        maze = new char[][] { 
                // 0    1    2     3     4     5     6     7     8     9
                { '-', '-', 'X' , '-' , '-' , '-' , '-' , '-' , '-' , '-' },    // 0
                { '-', '-', '-' , '-' , '-' , '-' , '-' , '-' , 'U' , '-' },    // 1
                { '-', '-', '-' , '-' , '-' , 'X' , '-' , '-' , '-' , '-' },    // 2
                { 'X', '-', '-' , 'R' , '-' , '-' , '-' , '-' , '-' , 'X' },    // 3
                { '-', '-', '-' , '-' , '-' , '\\' , '-' , '-' , '-' , '-' },   // 4
                { '-', '-', '-' , '-' , '-' , '-' , '-' , '-' , '/' , '-' },    // 5
                { '-', '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' },    // 6
                { 'D', '-', 'R' , '-' , '-' , '-' , '-' , '-' , '-' , '-' },    // 7
                { '-', '-', '-' , '-' , '-' , '-' , '-' , '-' , '\\' , '-' },   // 8
                { '-', '-', '-' , '-' , 'X' , '-' , '-' , '-' , '-' , '-' }     // 9
            };
        drawMaze();

        if ( winStep % 2 == 0 ) { // The maze will end with dead-ends if the total possible steps are odd numbered.

            if( pathfinder2 ( maze , startX , startY , 0 , 'x', 'x' ) ) {

                System.out.printf("\n\nThe complete route started on x=" + startX + ", y=" + startY + ".\n");
                System.out.printf("The following steps make up the path: \n");

                for( int i = 0 ; i < winStep ; i++ ) {

                    System.out.println("Step " + (i+1) + ": " + path[i][0] + " , " + path[i][1] + " [" + maze[path[i][1]][path[i][0]] + "]" );
                }
                System.out.println();
                drawMaze();

            }else {

                System.out.println("This program is silly. Total steps taken this time: " + totalSteps);
            }
        }else {
            System.out.printf("\n\nIt is not possible to complete this maze.");
        }

        return;
        // End of main()
    }
}
import java.io.*;
导入java.util.*;
公共类迷宫{
公共静态int-sizeX=10,//迷宫的宽度
sizeY=10,//迷宫的高度
startX=5,//迷宫X轴上的起点
startY=5,//迷宫Y轴上的起点
winStep=98;//可行迷宫所需的步数,且必须为偶数,否则路径将在末端打开
私有静态int[]path=new int[winStep][2];//路径上坐标列表的占位符
//路径[N][0]=X
//路径[N][1]=Y
私有静态字符[][]maze;//maze映射和步骤映射的占位符
//梅兹[世界其他地区][哥伦比亚]
//迷宫[Y][X],穿越迷宫时需要了解
public static int totalSteps=0;//疑难解答代码,用于在算法失败时查看算法在迷宫中走多远
公共静态void drawMaze(){
System.out.printf(“”;//绘制X轴前的间距
对于(int i=0;i=sizeX | | y<0 | | y>=sizeY){
返回false;
}
//看看你的结局
如果(步骤>0){//在第一步之后检查这些场景
//检查前面的步骤
if((nDir='u'&&((oDir='u'&&maze[y+1][x]='/'))| |(oDir='u'&&maze[y+1][x]='\'))//如果你刚刚向上移动,但你的最后一步也是向上的,最后一块是'/'或'\'
||(nDir='r'&&((oDir='r'&&maze[y][x-1]='/'))(oDir='r'&&maze[y][x-1]='\\')//如果你刚刚向右移动,但你的最后一步也是对的,最后一块瓷砖是'/'或'\')
||(nDir='d'&&((oDir='d'&&maze[y-1][x]='/'))(oDir='d'&&maze[y-1][x]='\\')//如果你刚下楼,但你的最后一步也下楼了,最后一块瓷砖是'/'或'\')
||(nDir='l'&&((oDir='l'&&maze[y][x+1]='/'))|(oDir='l'&&maze[y][x+1]='\\'))//如果你刚刚向左移动,但你的最后一步也走了,最后一块瓷砖是'/'或'\')
{返回false;}
//检查当前步骤
其他的
如果(迷宫[y][x]='x'){//如果你走进了一块砖
返回false;
}否则
如果(x==startX&&y==startY&&step!=winStep){//如果您已经到达起始位置,但没有采取足够的步骤
返回false;
}否则
if(nDir=='u'//如果您刚从较低的磁贴向上移动
&&(迷宫[y][x]='U'| |迷宫[y][x]='R'| |迷宫[y+1][x]='D'| | |迷宫[y+1][x]='L'| | mz[y][x]='S'){//如果你刚刚穿过斜坡墙或之前踩过的瓷砖
返回false;
}否则
if(nDir=='r'//如果您刚刚从下向左移动
&&(迷宫[y][x-1]='U'| |迷宫[y][x]='R'| |迷宫[y][x]='D'| | |迷宫[y][x-1]==''L'| | mz[y][x]='S'){//如果你刚刚穿过斜坡墙或之前踩过的瓷砖
返回false;
}否则
if(nDir=='d'//如果您刚从上分幅向下移动
&&(迷宫[y-1][x]='U'| | |迷宫[y-1][x]='R'| |迷宫[y][x]='D'| | |迷宫[y][x]='L'| | mz[y][x]='S'){//如果你刚刚穿过斜坡墙或之前踩在瓷砖上的话
返回false;
}否则
if(nDir=='l'//如果您刚刚移动了l
void somemethod(char[][] array) {
    array = new char[][] {
        {'a', 'b', 'c'},
        {'d', 'e', 'f'},
    };
}
       if (pathfinder2(maze , startX , startY , 0 , 'x', 'x')) {
       char[][] mazevar = new char[10][];
       for (int i = 0; i < 10; ++i) {
           mazevar[i] = Arrays.copyOf(maze[i], 10);
       }
       if (pathfinder2(maze , startX , startY , 0 , 'x', 'x')) {