1

ナイトのツアーを実装しようとしています。

私は(計画のように)それに取り組んでおり、今ではほぼ2〜3時間です。そして、私はまだ進歩していません。スタート地点が見えないような..

以下は、騎士のツアー バージョンに変更する必要がある基本的な dfs メソッドです。

class StackK
{
private final int MAX_VERTS = 25;
private int[] st;
private int top;
// ------------------------------------------------------------
public boolean isFull()
{return (top == MAX_VERTS-1);}
// ------------------------------------------------------------
public StackK()           // constructor
  {
  st = new int[MAX_VERTS];    // make array
  top = -1;
  }
// ------------------------------------------------------------
public void push(int j)   // put item on stack
  { st[++top] = j; }
// ------------------------------------------------------------
public int pop()          // take item off stack
  { return st[top--]; }
// ------------------------------------------------------------
public int peek()         // peek at top of stack
  { return st[top]; }
// ------------------------------------------------------------
public boolean isEmpty()  // true if nothing on stack
  { return (top == -1); }
// ------------------------------------------------------------
}  // end class StackK

class VertexK
{
public char label;        // label (e.g. 'A')
public boolean wasVisited;
// ------------------------------------------------------------
public VertexK(char lab)   // constructor
  {
  label = lab;
  wasVisited = false;
  }
// ------------------------------------------------------------
}   // end class VertexK

class GraphK
{
private final int MAX_VERTS = 5;
private VertexK VertexKList[]; // list of vertices
private int adjMat[][];      // adjacency matrix
private int nVerts;          // current number of vertices
private StackK theStack;
// ------------------------------------------------------------
 public GraphK()               // constructor
  {
  VertexKList = new VertexK[MAX_VERTS];
                                      // adjacency matrix
  adjMat = new int[MAX_VERTS][MAX_VERTS];
  nVerts = 0;
  for(int y=0; y<MAX_VERTS; y++)      // set adjacency
     for(int x=0; x<MAX_VERTS; x++)   //    matrix to 0
        adjMat[x][y] = 0;
  theStack = new StackK();
  for(int i=0;i<MAX_VERTS;i++)
       addVertex((char)('A'+i));

  }


// ------------------------------------------------------------
public void move(int row, int col)
{

}
// ------------------------------------------------------------
public void addVertex(char lab)
   {
   VertexKList[nVerts++] = new VertexK(lab);
   }
// ------------------------------------------------------------
public void addEdge(int start, int end)
  {
  adjMat[start][end] = 1;
  }

 // ------------------------------------------------------------
public void displayVertexK(int v)
  {
  System.out.print(VertexKList[v].label);
  }


 // ------------------------------------------------------------

public void dfs()  // depth-first search
  {                                 
  VertexKList[0].wasVisited = true;  
  displayVertexK(0);                 
  theStack.push(0);

  displayAdj();


  while( !theStack.isEmpty() )      
     {

     int v = getAdjUnvisitedVertexK( theStack.peek() );
     if(v == -1)                    
        theStack.pop();
     else                           
        {
        VertexKList[v].wasVisited = true;  
        displayVertexK(v);                 
        theStack.push(v);                
        }
     }  // end while

  // stack is empty, so we're done
  for(int j=0; j<nVerts; j++)          // reset flags
     VertexKList[j].wasVisited = false;
  }  // end dfs

 // ------------------------------------------------------------
// returns an unvisited VertexK adj to v
public int getAdjUnvisitedVertexK(int v)
  {
  for(int j=0; j<nVerts; j++)
     if(adjMat[v][j]==1 && VertexKList[j].wasVisited==false)
        return j;
  return -1;
  }  // end getAdjUnvisitedVertexK()
// ------------------------------------------------------------
public void displayAdj()
{
   for(int i=0;i<nVerts;i++){
       for(int k=0;k<nVerts;k++)
           System.out.print(adjMat[i][k]);
   System.out.println("");
   }
}
 // ------------------------------------------------------------
}  // end class GraphK

public class KnightApp
{
public static void main(String[] args)
  {
  GraphK k = new GraphK();

  k.displayAdj();


  }  // end main()
}  // end class DFSApp

簡単にするために、ボードのサイズを 5x5 にすることにしました。私はそれをグーグルで検索し、いくつかの解決策を見ましたが、それらのほとんどは私には意味がありませんでした. DFS メソッドを利用するにはどうすればよいですか? DFSを使わずに再帰を使えばある程度実装できると思います。ただし、DFS をどこから始めればよいかさえもわかりません。

どこから始めればよいか、誰かが私にガイダンスを与えることができますか? 私は解決策を求めているのではなく、始める場所が必要なだけです

前もって感謝します。

4

1 に答える 1

1

深さ優先検索自体は、グラフのノードを列挙するための一般的な戦略です。ユーザー定義のスタックによって再帰的または反復的に実装できます。検索するグラフは明示的にエンコードできます。これは通常、アプローチが説明されているときに行われます。

ただし、あなたの場合、グラフ(ゲームのある種の決定木)を明示的にエンコードする必要はありません。新しい後続ノードは、グローバル状態 (ボードを表す) 上の新しい状態を表す実行可能な移動を選択することによって生成できます。再帰的な評価の後、移動を元に戻して、次の実行可能な移動に進みます。このアプローチを使用すると、再帰を介してバックトラッキングを実装できます。

于 2016-03-22T11:46:12.580 に答える