【问题标题】:Knight's tour using DFS Java使用 DFS Java 的骑士之旅
【发布时间】:2022-01-03 00:17:08
【问题描述】:

我正在尝试实现Knight's tour

我一直在努力(更像是计划)它已经差不多好 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。

谁能给我一些关于从哪里开始的指导? 我不是在寻求解决方案,只是需要一个开始的地方

提前谢谢你。

【问题讨论】:

  • 什么是 DFS?和你的出发点有什么关系?你不应该能够从板上的任何领域开始吗?
  • DFS = 深度优先搜索图。起点,是指解决问题的起点。
  • DFS如何改变算法的选择? (即,warnsdorffs 规则与回溯相结合)抱歉,我可能无法为您提供帮助,因为我不了解此 DFS
  • 我也不确定,因为我正在使用的书(Lafore 的数据结构和算法)甚至没有提到 Warnsdorff 的规则。
  • warndorff 的规则说,您从那里开始以最少的可能移动次数进入场地(这大大减少了回溯),但棋盘越大,您拥有的次数越多具有相同数量的可能下一步移动的字段(在 8x8 板上,您有 2 个起点,然后您需要回溯)-松鼠和剔除稍后改进了该算法-但它似乎与您的 DFS 没有任何关系^ ^

标签: java algorithm knights-tour


【解决方案1】:

Depth first search 本身是枚举图节点的一般策略;它可以由用户定义的堆栈以递归或迭代方式实现。要搜索的图可以显式编码,这通常在解释方法时完成。

但是,在您的情况下,图形(这是某种游戏的决策树)不需要显式编码;可以通过选择一个可行的移动来生成新的后继节点,该移动代表全局状态(代表棋盘)上的新状态,并在递归评估后撤消移动以继续下一个可行移动。使用这种方法,可以通过递归实现回溯。

【讨论】:

  • @maytham-ɯɐɥʇʎɐɯ 感谢您的评论,我更新了答案。
  • 谢谢。我是否必须创建一个单独的方法来显示可行的动作并从那里开始?我应该从哪里开始?
  • @LookAtTheBigPicture 创建一些结构(数组等)来表示borard;然后创建一个方法来获取棋盘状态的所有可能移动以及执行和撤消移动的方法。最后,您需要一些代码来检测您是否已获得最终状态(即搜索树的叶子)。然后必须将这些基本构建块插入深度优先搜索。
  • 我的代码中没有将 adjMat 用作板吗?我以为我在 adjMat 中添加了“1”来代表骑士的动作。你是说我需要另一个矩阵吗?
  • 不,adjMat 似乎很适合。附加提示 - 当且仅当所有字段都已访问(包含字段)或某些字段未访问时,搜索已达到最终状态,但不可能进行合法移动。
猜你喜欢
  • 1970-01-01
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多