簡體   English   中英

以遞歸方式向下,向右,向左和向上移動會導致堆棧溢出異常

[英]Moving down, right, left and up in matrix recursively causes stack overflow exception

該程序用於找出矩陣中兩點之間的最短路徑,其中我向下,向右,向左和向上遍歷,但由於遞歸,它進入一個來回的無限循環。

這個程序基本上遍歷矩陣所在

  • 'C'表示目的地
  • 'B'表示來源
  • '_'表示允許移動
  • 'D'表示不允許

問題是找到B和C之間最短的浴槽。

我怎么能讓這段代碼工作? 如同在一次之后停止控制向下。

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

/* Name of the class has to be "Main" only if the class is public. */
class Stockroom
{
    //static int m = 0;
    //static int n = 0;
    //static char a[][] = new char [m][n];

    public static boolean checkFeasibility(int x, int y, int row, int col, char a[][])
    {
        if(x>=0 && x<row && y>=0 && y<col && a[x][y] != 'D')
        return true;

        else
        return false;
    }

    public static boolean shortestPath(char a[][], int bx, int by, int x, int y, int len, int minLen)
    {
        if( checkFeasibility(bx,by,x,y,a)==false )
            return false;

           if(a[bx][by]=='C')
           {
               minLen = Math.min(len,minLen);
               System.out.println(minLen-1); 
               return true;
           }



               len++;


               if(shortestPath(a,bx+1,by,x,y,len++,minLen)== true)
               return true;

               if(shortestPath(a,bx,by+1,x,y,len++,minLen)==true)
               return true;

               if(shortestPath(a,bx,by-1,x,y,len++,minLen)== true)
                   return true;

               if(shortestPath(a,bx-1,by,x,y,len++,minLen)== true)
               return true;



               else {
                   len--;
                   return false;
               }

    }

    public static void main (String[] args) throws java.lang.Exception
    {
           char arr[][] = {
                            {'_','B','_','_'},

                            {'D','_','_','D'},

                            {'_','D','_','_'},

                            {'_','_','C','_'},

                          };

           int bx =0,by=1,px=3,py=2;
           int n =4,m=4;

           shortestPath(arr, bx, by, m, n, 0, 100);

    }
}

只需使用“D”標記您訪問過的每個字段,以避免返回。 因此,在shortestPath ,打完電話后checkFeasibility和檢查后,如果該值是“C”,這樣做:

a[bx][by] = 'D';

詳細闡述了Frank Puffer的想法:

class Stockroom {

    public static boolean checkFeasibility(int x, int y, int row, int col,
            char a[][]) {
        if (x >= 0 && x < row && y >= 0 && y < col && a[x][y] != 'D')
            return true;

        else
            return false;
    }

    public static boolean shortestPath(char a[][], int bx, int by, int x,
            int y, int len, int minLen) {
        if (checkFeasibility(bx, by, x, y, a) == false)
            return false;

        if (a[bx][by] == 'C') {
            minLen = Math.min(len, minLen);
            System.out.println(minLen - 1);
            return true;
        }

        len++;

        if (len >= minLen) { // this was not shortest
            return false;
        }

        // hack to make sure we don’t go through the same spot again
        a[bx][by] = 'D';

        if (shortestPath(a, bx + 1, by, x, y, len, minLen) == true) {
            // remove temporary block so this space can be used in other paths
            a[bx][by] = '_';
            return true;
        }

        if (shortestPath(a, bx, by + 1, x, y, len, minLen) == true) {
            a[bx][by] = '_';
            return true;
        }

        if (shortestPath(a, bx, by - 1, x, y, len, minLen) == true) {
            a[bx][by] = '_';
            return true;
        }

        if (shortestPath(a, bx - 1, by, x, y, len, minLen) == true) {
            a[bx][by] = '_';
            return true;
        }

        len--;
        return false;

    }

    public static void main(String[] args) {
        // find path from B to C; don’t go through D
        char arr[][] = { { '_', 'B', '_', '_' }, 
                         { 'D', '_', '_', 'D' },
                         { '_', 'D', '_', '_' },
                         { '_', '_', 'C', '_' },
                        };

        int bx = 0, by = 1, px = 3, py = 2;
        int n = 4, m = 4;

        shortestPath(arr, bx, by, m, n, 0, 100);
        System.out.println(Arrays.deepToString(arr));
    }
}

這會修復'_'字段的覆蓋,但仍會覆蓋'B'。 程序打印3,因為最短路徑長度為4,您減去1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM