2017-02-14 7 views
0

EDIT 誰かが効果的に私がエンドポイントに達したことを確認するためにすべての再帰をステップダウンする必要があることを指摘しています私がスタックを通って戻るときの値。私はこれがどういう意味か分かりません。ベクトルを作成してJavaの再帰メソッド内に情報を格納する

私の目標は、迷路を通してパスを構築し、そのパスをVectorに格納し、再帰の最後にそれを返す再帰的メソッドを構築することです。

私のメソッドは迷路を実行しますが、すべての再帰をインスタンス化する必要があるため、Vectorにリストを格納するのが困難です。ここで

は私のコード(EDITED)です:

protected Vector<GameCell> findPath(int nRow, int nCol) 
{ 

    aBoard[nRow][nCol].setVisited(true); 
    if(aBoard[nRow][nCol].getVal() == 'E') 
    { 
     Vector<GameCell> list = new Vector<GameCell>(); 
     list.add(aBoard[nRow][nCol]); 
     return list; 
    } 


    if(canGoLeft(nRow, nCol)) 
    { 
     if(!aBoard[nRow][nCol - 1].isVisited()) 
     { 

      return findPath(nRow, --nCol); 
     } 
    } 

    if(canGoRight(nRow, nCol)) 
    { 
     if(!aBoard[nRow][nCol+1].isVisited()) 
     { 
      return findPath(nRow, ++nCol); 
     } 

    } 

    if(canGoUp(nRow, nCol)) 
    { 
     if(!aBoard[nRow - 1][nCol].isVisited()) 
     { 
      return findPath(--nRow, nCol); 
     } 
    } 

    if(canGoDown(nRow, nCol)) 
    { 
     if(!aBoard[nRow + 1][nCol].isVisited()) 
     { 
      return findPath(++nRow, nCol); 
     } 
    } 

    System.out.println("You hit a dead end."); 
    return null; 

} 

は私が永久に私の値を格納することができますので、私は私のベクトルすべての再帰をインスタンス化することはできません方法はありますか?

ご協力いただければ幸いです。 Javaの2以降のベクトルが効果的に廃止されていることを

public List<GameCell> findPath(int nRow, int nCol) { 
    List<GameCell> result = new ArrayList<>(); 
    doFindPathRecursively(result, nRow, nCol); 
    return result; 
} 

private void doFindPathRecursively(List<GameCell> result, int nRow, int nCol) { 
    ... 
} 

注意、したがって使用:

+0

はい。引数としてメソッドに渡し、メソッドからそれを作成します。 –

+0

私が設定したパラメータは、自分の仕様に従って使用できる唯一のパラメータであることを明確にする必要があります。このメソッドでは、My Vectorを作成する必要があります。 –

+1

'java.util.Vector'は19年間時代遅れです。あなたはどのくらいJavaでプログラミングしていますか? –

答えて

0

私は解決策を見つけました。自分の道を見つけて、再帰のスタックをバックアップした後、要素を追加する必要がありました。ここに解決策があります。私はまだそれにバグがありますが、私が尋ねた問題は修正されています。

protected Vector<GameCell> findPath(int nRow, int nCol) 
{ 
    this.aBoard[nRow][nCol].setVisited(true);   
    if(this.aBoard[nRow][nCol].getVal() == 'E') 
    { 
     Vector<GameCell> list = new Vector<GameCell>(); 
     list.add(this.aBoard[nRow][nCol]); 
     return list; 
    } 

    if(canGoUp(nRow, nCol)) 
    { 
     if(!this.aBoard[nRow - 1][nCol].isVisited()) 
     { 
      Vector<GameCell> list = findPath(nRow - 1, nCol); 
      if(list != null) 
      { 
       list.add(this.aBoard[nRow][nCol]); 
       return list; 
      } 
     } 
    } 

    if(canGoRight(nRow, nCol)) 
    { 
     if(!this.aBoard[nRow][nCol+1].isVisited()) 
     { 
      Vector<GameCell> list = findPath(nRow, nCol + 1); 
      if(list != null) 
      { 
       list.add(this.aBoard[nRow][nCol]); 
       return list; 
      } 
     } 

    } 

    if(canGoLeft(nRow, nCol)) 
    { 
     if(!this.aBoard[nRow][nCol - 1].isVisited()) 
     { 
      Vector<GameCell> list = findPath(nRow, nCol - 1); 
      if(list != null) 
      { 
       list.add(this.aBoard[nRow][nCol]); 
       return list; 

      } 
     } 
    } 

    if(canGoDown(nRow, nCol)) 
    { 
     if(!this.aBoard[nRow + 1][nCol].isVisited()) 
     { 
      Vector<GameCell> list = findPath(nRow + 1, nCol); 
      if(list != null) 
      { 
       list.add(this.aBoard[nRow][nCol]); 
       return list; 
      } 
     } 
    } 

    return null; 

} 
+0

最後のバグを修正しました。入れ子にされたif文のプリインクリメントとデクリメントがある場所では、+または - 1が必要です。 --nRowではなくnRow - 1です。ヌルを返し、1ではなく2つの値にnullを返すと、再帰がそれらに戻りました。 –

1

それを行うための一般的な方法は、再帰的な一つであり、引数として埋めるためにリストを取り、別のメソッドに委譲することですリストとArrayListの代わりに。

+0

残念ながら、私はそれを行うために外部のメソッドを使用することはできません。さもなければ、これははるかに簡単な割り当てになります。私は私の質問を更新し、私が必要とするものをより明確にしました。 –

+1

これは、任意の、非現実的な要求のように見えます。しかし、あなたは、呼び出しごとにListを作成して返し、同じ結果を得るために返されたリストを現在のものに追加することもできます。 –

+0

'Vector'を使っている教師が、恣意的で非現実的な要求を持つ割り当てを渡すことは、一貫しているようです... – slim

関連する問題