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

Java 迷宫递归-少数输入出错(可能是逻辑错误)

Java 迷宫递归-少数输入出错(可能是逻辑错误),java,recursion,maze,Java,Recursion,Maze,我是java的初学者。 我一直在研究一个迷宫问题,试图通过递归来解决它。 我写的代码似乎只对少数输入有效,其他输入无效。 输入是一个由0和1组成的迷宫是起点,@是出口。0是墙,1是打开的。输出将是从#到@的跳数。 虽然我是通过递归来解决这个问题,但我一定是在逻辑上出错了。 请让我知道我错在哪里 课堂练习词汇 import java.util.Scanner; class practisenumwords { public static void main(String[] args){

我是java的初学者。 我一直在研究一个迷宫问题,试图通过递归来解决它。 我写的代码似乎只对少数输入有效,其他输入无效。 输入是一个由0和1组成的迷宫是起点,@是出口。0是墙,1是打开的。输出将是从#到@的跳数。 虽然我是通过递归来解决这个问题,但我一定是在逻辑上出错了。 请让我知道我错在哪里

课堂练习词汇

 import java.util.Scanner;

 class practisenumwords {
 public static void main(String[] args){
 Scanner in=new Scanner(System.in);
 int r=in.nextInt();
 int c=in.nextInt();
 maze maz=new maze(r,c);                    /*input in string copied to array*/
 char[] ch;                                        
 ch = "00000000111111101111011001101@#11100".toCharArray();  
 int l=0;
 for(int i=0;i<r;i++)
   {
    for(int j=0;j<c;j++)                /*initialising the maze elements*/
    {                               
     maz.m[i][j]=new cells();
     maz.m[i][j].c=ch[l];
     maz.m[i][j].row=i;
     maz.m[i][j].col=j;
     l++;
    }
  }
for(int i=0;i<r;i++)                             /*print the input maze */
  {
    for(int j=0;j<c;j++)
    {
    System.out.print(""+maz.m[i][j].c);
  }
  System.out.println();
}

maz.escape();
maz.find(maz.startx,maz.starty,maz.hops);
}
}
class cells {
char c;
int row;
int col;
boolean done=false;                 /*initially all cells are unvisited*/ 
}
课堂迷宫

class maze{                       
maze (int a,int b){                                
    rows=a;
    cols=b;
    m=new cells[rows][cols];
}
int rows;
int cols;
cells[][] m;
int startx,starty;
int hops=0;
void escape()
{
    for(int i=0;i<rows;i++)
    {
       for(int j=0;j<cols;j++)
       {
        if(m[i][j].c=='#') 
         {
               startx=i;
               starty=j;
               System.out.println(startx+" "+starty);
         }
       }
    }
}
void find(int x,int y,int h)
{    
    if   ((x+1<rows && m[x+1][y].c=='@'   &&  m[x+1][y].done!=true) 
        ||(x-1>=0   && m[x-1][y].c=='@'   &&  m[x-1][y].done!=true)
        ||(y+1<cols && m[x][y+1].c=='@'   &&  m[x][y+1].done!=true)   
        ||(y-1>=0   && m[x][y-1].c=='@'   &&  m[x][y-1].done!=true)){
        h++;
        System.out.println(h);   
         }
      else
       {  
        if(x-1>=0   &&  m[x-1][y].c=='1' && m[x-1][y].done!=true){   /*north cell*/
                    m[x][y].done=true;
                    h++;
                    find(x-1,y,h);
        }
        if(x+1<rows && m[x+1][y].c=='1'  && m[x+1][y].done!=true){   /*south cell*/
                     m[x][y].done=true;
                     h++;
                     find(x+1,y,h);
        }
        if(y+1<cols && m[x][y+1].c=='1'  && m[x][y+1].done!=true){   /*east cell*/
                    m[x][y].done=true;
                    h++;
                    find(x,y+1,h);
        }
        if(y-1>=0 && m[x][y-1].c=='1'    && m[x][y-1].done!=true){   /*west cell*/
                    m[x][y].done=true;
                    h++;
                    find(x,y-1,h);
        }
       }   
      }
    }
输出-12(获得正确的输出)

输出-7(获得正确的输出)

但不适用于其他输入,如

0 0 0 0 @ 0
0 1 0 1 1 0
1 1 1 1 0 1
0 1 0 1 0 0
0 0 # 1 1 1
0 1 1 0 0 1
正确输出-6获得输出-7


此外,输出随相邻单元格的检查顺序而变化。

在快速读取中,我注意到:

if(...) {
    ...
    h++;
    find(x-1,y,h);
}
对于每个if块

当满足第一个if条件时,第二个if块内h==h+2,第三个和第四个if块也是如此

也许你应该写:

if(...) {
    ...
    // h++;
    find(x-1,y,h+1);
}

老实说,我会以稍微不同的方式实现递归函数:

而且不需要检查bool值是否为
!=正确
!布尔值
很好

int find(int x,int y,int h)
{    
    int result = -1;
    if   ((x+1<rows && m[x+1][y].c=='@'   &&  !m[x+1][y].done) 
        ||(x-1>=0   && m[x-1][y].c=='@'   &&  !m[x-1][y].done)
        ||(y+1<cols && m[x][y+1].c=='@'   &&  !m[x][y+1].done)   
        ||(y-1>=0   && m[x][y-1].c=='@'   &&  !m[x][y-1].done)){   
        return h + 1;
         }
      else
       {

        if(x-1>=0   &&  m[x-1][y].c=='1' && !m[x-1][y].done){   /*north cell*/
                   m[x][y].done=true;

                   result = find(x-1,y,h + 1)
                   if (result > -1) {
                       return result; 
                   }
                   m[x][y].done=false;
        }

@user1365836-先生,我明白了,但我希望h的值在“if块”中递增,因为最后h将给出从#(开始)到@(退出)的总跳数。也就是说,我增加了h,并在find()函数中传递了相同的d。是的,但是只有当你这样做的时候,你才需要增加这个值。如果你采取这种方式,然后它是错误的,你不必增加。我写了find(x-1,y,h+1)yes,在迷宫类中包含布尔完成,并用您的函数替换了该函数。但无法获得正确的输出。@ngoa-同样的问题仍然存在。对于我提到的输入,它可以正常工作,并返回正确的值。但它不能像以前那样对相同的输入工作。唯一的区别是现在它返回0而不是错误的答案。我做了更改,将int result=-1和return(h+1)替换为return h。因为那将是最后一跳。现在它适用于所有输入!!!非常感谢您帮助检查死角。:)不跟踪死角导致错误。
if(...) {
    ...
    // h++;
    find(x-1,y,h+1);
}
int find(int x, int y, int h) {
    if ((x + 1 < rows && m[x + 1][y].c == '@' && m[x + 1][y].done != true)
            || (x - 1 >= 0 && m[x - 1][y].c == '@' && m[x - 1][y].done != true)
            || (y + 1 < cols && m[x][y + 1].c == '@' && m[x][y + 1].done != true)
            || (y - 1 >= 0 && m[x][y - 1].c == '@' && m[x][y - 1].done != true)) {
        h++;
        finish = true;
        return h;
    } else {
        if (x - 1 >= 0 && m[x - 1][y].c == '1' && m[x - 1][y].done != true
                && !finish) { /* north cell */
            m[x][y].done = true;
            int temp = find(x - 1, y, h);
            if (temp != 0)
                h = temp + 1;
            return h;

        }
        if (x + 1 < rows && m[x + 1][y].c == '1'
                && m[x + 1][y].done != true && !finish) { /* south cell */
            m[x][y].done = true;
            int temp = find(x + 1, y, h);
            if (temp != 0)
                h = temp + 1;
            return h;

        }
        if (y + 1 < cols && m[x][y + 1].c == '1'
                && m[x][y + 1].done != true && !finish) { /* east cell */
            m[x][y].done = true;

            int temp = find(x, y + 1, h);
            if (temp != 0)
                h = temp + 1;
            return h;

        }
        if (y - 1 >= 0 && m[x][y - 1].c == '1' && m[x][y - 1].done != true
                && !finish) { /* west cell */
            m[x][y].done = true;

            int temp = find(x, y - 1, h);
            if (temp != 0) {
                h = temp + 1;
            }
            return h;
        }
        return 0;
    }
}
    maz.hops = maz.find(maz.startx, maz.starty, maz.hops);
    System.out.println(maz.hops);
int find(int x,int y,int h)
{    
    int result = -1;
    if   ((x+1<rows && m[x+1][y].c=='@'   &&  !m[x+1][y].done) 
        ||(x-1>=0   && m[x-1][y].c=='@'   &&  !m[x-1][y].done)
        ||(y+1<cols && m[x][y+1].c=='@'   &&  !m[x][y+1].done)   
        ||(y-1>=0   && m[x][y-1].c=='@'   &&  !m[x][y-1].done)){   
        return h + 1;
         }
      else
       {

        if(x-1>=0   &&  m[x-1][y].c=='1' && !m[x-1][y].done){   /*north cell*/
                   m[x][y].done=true;

                   result = find(x-1,y,h + 1)
                   if (result > -1) {
                       return result; 
                   }
                   m[x][y].done=false;
        }
        return result;
       }