简体   繁体   English

Java中的Farmer,W​​olf,Goat和Cabbage广度优先和深度优先搜索

[英]Farmer, Wolf, Goat and Cabbage Breadth-first and Depth-first Search in Java

So, I started this problem where I have to bring a cabbage, wolf, and goat across the river without leaving the cabbage with the goat or the wolf and goat by themselves on the same side. 所以,我开始出现这个问题,我必须把白菜,狼和山羊带到河对岸,而不要将卷心菜与山羊或狼和山羊一起放在同一侧。

I started and got sorely confused about how to approach this. 我开始并对如何处理这个问题感到非常困惑。 Basically I was thinking of adding a bunch of vertexes that would lead to the correct outcome, and just have the program demonstrate breadth-first and depth-first searching without having a complex vertices generation process. 基本上我正在考虑添加一组顶点,这些顶点将导致正确的结果,并且只需让程序演示广度优先和深度优先搜索,而无需复杂的顶点生成过程。 Am I thinking about this correctly, or is there a better approach? 我是否正确地思考这个问题,还是有更好的方法?

Here is my code for the main method so far. 这是我目前为止主要方法的代码。

package project3;

import java.util.*;
import java.io.*;


public class Project3 extends Network{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new Project3().run();
} //main method

public void run()
{
    String start ="fwgcR",
           finish = "Rfwgc";

    addVertex(start);
    addVertex("fwgRc");
    addVertex("fwcRg");
    addVertex(finish);


    //Breadth First iterator
    Iterator<String> itr = network.breadthFirstIterator (start);
        while (itr.hasNext())
            System.out.print (itr.next() + " ");

    //Depth First Iterator    
    itr = network.depthFirstIterator (start);
        while (itr.hasNext())
            System.out.print (itr.next() + " ");


    } // method run  
}

The usual approach for problem-solving search is to develop a state space representation. 解决问题的常用方法是开发状态空间表示。 You seem to want to explicitly represent all the states and transitions in a graph, but the more common approach is to design a state representation and then a process that will generate the successor states from a given state. 您似乎希望明确表示图中的所有状态和转换,但更常见的方法是设计状态表示,然后是从给定状态生成后继状态的进程。 The search process then relies on the successor state generator to expand a current state and carry out the search. 然后,搜索过程依赖于后继状态生成器来扩展当前状态并执行搜索。

I strongly recommend using a state generating function rather than building a complete state graph from the start. 我强烈建议使用状态生成函数,而不是从一开始就构建完整的状态图。

In your case, the state representation might be as simple as the current side of the river for each object (farmer, wolf, goat, and cabbage). 在您的情况下,状态表示可能与每个对象(农民,狼,山羊和卷心菜)的当前河流一样简单。 The successor states for a given state are the farmer and possibly one of the objects currently on the same side as the farmer switching sides. 给定州的继承国是农民,可能是农民转换方面同一方面的目标之一。 You would probably want to filter out inadmissible states as part of the successor generation function. 您可能希望过滤掉不允许的状态,作为后继生成函数的一部分。 I don't recommend a string to represent the states, although it could work. 我不推荐使用字符串来表示状态,尽管它可以工作。 An array of four booleans (each representing, say, "right side of the river") would be easier to work with. 一组四个布尔(每个代表,例如,“河的右侧”)将更容易使用。

I don't know why you want to use both breadth-first and depth-first search, but yes, either of those can be used to find the correct order of steps to a solution. 我不知道你为什么要同时使用广度优先深度优先搜索,但是,可以使用其中任何一个来找到解决方案的正确步骤顺序。

Using strings to represent the nodes in your graph is a bad idea. 使用字符串来表示图表中的节点是个坏主意。 It will be much more trouble than necessary to generate the outgoing vertices from each node, and to avoid adding redundant nodes. 从每个节点生成传出顶点并避免添加冗余节点将比不必要的麻烦多得多。

The next thing you need to do is define a class to represent each node on the graph, and write a function to find adjacent nodes, filtering out any nodes with the goat and cabbage or goat and wolf on the same side of the river and alone. 接下来你需要做的是定义一个类来表示图上的每个节点,并编写一个函数来查找相邻的节点, 过滤掉山羊和卷心菜或山羊和狼在河的同一侧的任何节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM