Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
骑士';使用DFS-Java的旅行_Java_Algorithm - Fatal编程技术网

骑士';使用DFS-Java的旅行

骑士';使用DFS-Java的旅行,java,algorithm,Java,Algorithm,我正在努力实现 我已经做了(更像是计划)差不多2~3个小时了。我仍然没有取得任何进展。我似乎找不到出发点 下面是基本的dfs方法,我必须修改为骑士之旅版本 class StackK { private final int MAX_VERTS = 25; private int[] st; private int top; // ------------------------------------------------------------ public boolean isFull()

我正在努力实现

我已经做了(更像是计划)差不多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
k类
{
私人最终整数最大值=25;
私人互联网【】街;
私人int top;
// ------------------------------------------------------------
公共布尔值isFull()
{return(top==MAX_VERTS-1);}
// ------------------------------------------------------------
public StackK()//构造函数
{
st=new int[MAX_VERTS];//生成数组
top=-1;
}
// ------------------------------------------------------------
public void push(int j)//将项放在堆栈上
{st[++top]=j;}
// ------------------------------------------------------------
public int pop()//从堆栈中取出项
{return st[top-->;}
// ------------------------------------------------------------
public int peek()//在堆栈顶部查看
{返回st[top];}
// ------------------------------------------------------------
public boolean isEmpty()//如果堆栈上没有任何内容,则为true
{return(top==-1);}
// ------------------------------------------------------------
}//结束类StackK
类顶点
{
公共字符标签;//标签(例如“A”)
公众参观;
// ------------------------------------------------------------
公共VertexK(字符实验室)//构造函数
{
标签=实验室;
wasviest=false;
}
// ------------------------------------------------------------
}//结束类VertexK
类图形
{
私人最终整数最大值=5;
私有VertexK VertexKList[];//顶点列表
私有整数邻接矩阵[][];//邻接矩阵
private int nVerts;//当前顶点数
私人斯塔克塞斯塔克;
// ------------------------------------------------------------
public GraphK()//构造函数
{
VertexKList=新的顶点k[MAX_VERTS];
//邻接矩阵
adjMat=新整数[MAX_VERTS][MAX_VERTS];
nVerts=0;
for(int y=0;y是枚举图的节点的一般策略;它可以由用户定义的堆栈以递归或迭代方式实现。要搜索的图可以显式编码,这通常在解释方法时完成


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

什么是DFS?它与您的起点有什么关系?您不应该从板上的任何字段开始吗?DFS=图的深度优先搜索。所谓起点,我指的是起点解决问题的方法。DFS如何改变算法的选择?(即Warnsdorfs规则与回溯相结合)抱歉,我可能无法帮助您,因为我不知道此DFS,我也不确定我正在使用的书(Lafore的数据结构和算法)甚至没有提到沃恩多夫的规则。沃恩多夫的规则说,你从一开始就以尽可能少的动作进入场地(这大大减少了回溯),但棋盘越大,你就越经常有多个场地具有相同数量的下一步动作(在8x8电路板上,有两个起点需要回溯)-squirrel和cull后来改进了算法-但这似乎与您的DFS无关^^@maytham-ɯɥʇʎɐɯ感谢您的评论,我更新了答案。谢谢。我需要创建一个单独的方法来显示可行的动作并从那里开始吗?我应该从哪里开始?@lookatthebigprature创建一些结构(数组或类似的)来表示board;然后创建一个方法来获取一个board状态的所有可能移动,以及一个方法来执行和撤消移动。最后,您需要一些代码来检测您是否已获得最终状态(即搜索树的一个叶子)。这些基本的构建块必须插入深度优先搜索。adjMat不是用作我代码中的一块板吗?我以为我在adjMat中添加了“1”来表示骑士的移动。你是说我需要另一个矩阵吗?不,
adjMat
似乎适合于此。另外的提示-搜索已达到最后一步ate当且仅当所有字段都已访问(包含字段)或某些字段未访问,但无法进行合法移动时。