2013-06-23 3 views
0

재귀 메서드를 사용하는 프로그램을 작성하려고하면 2 차원 배열의 시작 부분부터 끝까지 경로 번호를 찾습니다. 단계는 [0] [0]에서 끝까지 시작해야합니다. 배열은 0에서 99 사이의 int 숫자로 채워집니다.재귀 메서드 고정

[0] [0]이 21 일 경우 다음 단계는 [0 + 2] [0 + 1] 또는 [0 + 1] 일 수 있습니다. 0 + 2]이다. 메서드는 최종 경로에 도달하기 위해 여러 경로를 반환해야합니다. 이 방법은 코드를 해결하기 위해 도와주세요 배열을 [] []

 public class two 
     { 
      int[][] _my=new int[0][0]; 
      public two() 
     { 
     } 
     static int count=0; 
     public static int count; 

ountPath(int[][] mat) 
    { 
     int nextX=0; 
     int nextY=0; 
     int current=mat[0][0]; 
     if (current<=9) 
     { 
      nextX=current; 
      nextY=current; 
      if (checkBorders(0,0,nextX,nextY,mat)==0) 
      { 
       return 0; 
      } 
     } 
     else 
     { 
      nextX=(int)current/10; 
      nextY=current%10; 
      if (checkBorders(0,0,nextX,nextY,mat)==0) 
      { 
       return 0; 
      } 
     } 
     countPath(mat,nextX,nextY); 
     countPath(mat,nextY,nextX); 
     return count; 
    } 

    public static int countPath(int[][] mat,int x,int y) 
    { 
     int current=mat[x][y]; 
     int nextX=0; 
     int nextY=0; 
     int terminate=0; 
     if (current<=9) 
     { 
      nextX=current; 
      nextY=current; 
      if (checkBorders(x,y,nextX,nextY,mat)==0) 
      { 
       return 0; 
      } 
     } 
     else 
     { 
      nextX=(int)current/10; 
      nextY=current%10; 
      if (checkBorders(x,y,nextX,nextY,mat)==0) 
      { 
       return 0; 
      } 
     } 

     if (((mat.length-1)==nextY)&&((mat[0].length-1)==nextX)) 
     { 
      terminate=1; 
     } 

     if (terminate==1) 
      count++; 
     else 
     { 
     countPath(mat,nextX,nextY); 
     countPath(mat,nextY,nextX); 
     } 
     return count; 
    } 

    public static int checkBorders(int x,int y,int x1,int y1,int[][] mat) 
    { 
     int maxX=mat[0].length; 
     int maxY=mat.length; 
     if ((x+x1)>maxX) 
      return 0; 
     else if ((y+y1)>maxY) 
      return 0; 
     else return 1; 
    } 

} 

을 얻을 때 오버 플로우가

comilation a를 중 :

여기 내 코드입니다.

+1

어떻게 작동하지 않습니까? 'count'는 정적 변수입니까? Java에서 btw를 사용하면'boolean'을 사용할 수 있습니다 – UmNyobe

+0

코드 자체가 포함되어 있으면 문제가 무엇인지 말하는 것뿐만 아니라 전체 코드를 게시하는 것이 도움이 될 수 있습니다. – Sandro

+0

예, static count 변수를 사용합니다. – user2513643

답변

0

u가 이미 방문한 상태를 저장하는 곳에 bool 배열을 하나 더 추가해야합니다. 지금 당신은 다음과 같은 경우에 올 수

당신은 당신이 < 상태로 갈 수있는 상태 B에서 상태 B와 C 에 갈 수있는 상태 A에서 상태 A. 에 -이 무한 루프 될 것입니다.

또한 암기가 계산 속도를 크게 높입니다.

희망이 당신을 도왔습니다.