用Java实现BFS

用Java实现BFS,java,breadth-first-search,game-development,Java,Breadth First Search,Game Development,我是Java的初学者,我需要一些帮助 我正在尝试实现广度优先搜索算法来解决一个益智游戏(解锁我的Android游戏)。我已经完成了GUI,但是我仍然坚持使用算法 到目前为止,我可以计算每个块的可用移动,它们应该是根节点的子节点。每个节点(linkedlist)都有每个块的位置,所有节点都存储在一个集合中 我现在需要的是将每个节点标记为已访问,这样我就不会进入无限循环 我将非常感谢任何帮助,如果我有任何错误,请纠正我 提前感谢:) 这是java中广度优先搜索的一个示例,如果您提供一些代码,我们可以

我是Java的初学者,我需要一些帮助

我正在尝试实现广度优先搜索算法来解决一个益智游戏(解锁我的Android游戏)。我已经完成了GUI,但是我仍然坚持使用算法

到目前为止,我可以计算每个块的可用移动,它们应该是根节点的子节点。每个节点(linkedlist)都有每个块的位置,所有节点都存储在一个集合中

我现在需要的是将每个节点标记为已访问,这样我就不会进入无限循环

我将非常感谢任何帮助,如果我有任何错误,请纠正我

提前感谢:)

这是java中广度优先搜索的一个示例,如果您提供一些代码,我们可以帮助您调整它以适应您的

publicstaticvoidbfs(BinaryTree.Node root)
public static void bfs(BinaryTree.Node<Integer> root)
{
    BinaryTree.Node<Integer> temp; //a binary tree with a inner generic node class
    Queue<BinaryTree.Node<Integer>> queue = new LinkedList<>(); //can't instantiate a Queue since abstract, so use LLQueue
    if (root == null)
    {
        return;
    }
    queue.add(root);
    while (!queue.isEmpty())
    {
        temp = queue.poll(); //remove the node from the queue
        //process current node
        System.out.println(temp.data);
        //process the left child first
        if (temp.left != null)
        {
            queue.add(temp.left);
        }
        //process the right child after the left if not null
        if (temp.right != null)
        {
            queue.add(temp.right);
        }
    }
}
{ BinaryTree.Node temp;//具有内部泛型节点类的二叉树 Queue Queue=new LinkedList();//由于抽象,无法实例化队列,因此请使用LLQueue if(root==null) { 返回; } 添加(根); 而(!queue.isEmpty()) { temp=queue.poll();//从队列中删除节点 //处理当前节点 系统输出打印LN(温度数据); //先处理左边的子对象 如果(左侧温度!=null) { 队列添加(临时左侧); } //如果不为null,则处理左后的右子级 如果(临时正确!=null) { 队列添加(临时右侧); } } }
@ 我无法评论aaronman的链接(没有足够的代表),但访问的字段是Node类中的公共字段成员

public class Node{
     public boolean visited;
     public Object data;
     //other fields

      public Node(Object data){
           this.visited = false;
           this.data = data;
      }
 }
至于print节点,我假设aaronman只是将Node对象传递给print方法,它只是显示Node类可能持有的任何数据

public void print(Node node){
     System.out.println(node.data);
}
请试试这个:

import java.util.ArrayList;
import java.util.List;

public class TreeTraverse {

    static class Node{
        Node(int data){
            this.data = data;
            this.left = null;
            this.right = null;
            this.visited = false;
        }
        int data;
        Node left;
        Node right;
        boolean visited;
    }

    public static void main(String[] args) {
        //The tree:
        //   1
        //  / \
        // 7   9
        // \  / \
        //  8 2 3

        Node node1 = new Node(1);
        Node node7 = new Node(7);
        Node node9 = new Node(9);
        Node node8 = new Node(8);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.left = node7;
        node1.right = node9;
        node7.right = node8;
        node9.right = node3;
        node9.left = node2;
        System.out.println("BFS: ");
        breadthFirstSearch(node1);

    }

    private static void breadthFirstSearch(Node node){
        List<Node> al = new ArrayList<>();
        al.add(node);
        while(!al.isEmpty()){
            node = al.get(0);
            if(node.left != null){
                int index = al.size();
                al.add(index,node.left);
            }
            if(node.right != null){
                int index = al.size();
                al.add(index,node.right);
            }
            System.out.print(al.get(0).data+" ");
            al.remove(0);


        }
    }

}
import java.util.ArrayList;
导入java.util.List;
公共类树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树型树{
静态类节点{
节点(int数据){
这个数据=数据;
this.left=null;
this.right=null;
this.visted=false;
}
int数据;
左淋巴结;
节点权;
参观;
}
公共静态void main(字符串[]args){
//树:
//   1
//  / \
// 7   9
// \  / \
//  8 2 3
节点node1=新节点(1);
节点node7=新节点(7);
节点node9=新节点(9);
节点node8=新节点(8);
节点node2=新节点(2);
节点node3=新节点(3);
node1.left=node7;
node1.right=node9;
node7.right=node8;
node9.right=node3;
node9.left=node2;
System.out.println(“BFS:”);
横向优先搜索(节点1);
}
专用静态空白宽度优先搜索(节点){
List al=新的ArrayList();
al.add(节点);
而(!al.isEmpty()){
node=al.get(0);
if(node.left!=null){
int index=al.size();
al.add(索引,节点,左);
}
if(node.right!=null){
int index=al.size();
al.add(索引,节点右侧);
}
System.out.print(al.get(0.data+);
al.移除(0);
}
}
}

有关更多信息,请访问:。

如果您使用链表上的
Deque
界面,您可以轻松地将BFS修改为DFS(如果需要)。方法
printNode()
visited()
在哪里定义?如何在
al.add(index,node.left)中模拟已访问的
,为什么不直接执行所有添加(node.left)
<代码>索引始终指向列表的最后一个位置。。。
import java.util.ArrayList;
import java.util.List;

public class TreeTraverse {

    static class Node{
        Node(int data){
            this.data = data;
            this.left = null;
            this.right = null;
            this.visited = false;
        }
        int data;
        Node left;
        Node right;
        boolean visited;
    }

    public static void main(String[] args) {
        //The tree:
        //   1
        //  / \
        // 7   9
        // \  / \
        //  8 2 3

        Node node1 = new Node(1);
        Node node7 = new Node(7);
        Node node9 = new Node(9);
        Node node8 = new Node(8);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.left = node7;
        node1.right = node9;
        node7.right = node8;
        node9.right = node3;
        node9.left = node2;
        System.out.println("BFS: ");
        breadthFirstSearch(node1);

    }

    private static void breadthFirstSearch(Node node){
        List<Node> al = new ArrayList<>();
        al.add(node);
        while(!al.isEmpty()){
            node = al.get(0);
            if(node.left != null){
                int index = al.size();
                al.add(index,node.left);
            }
            if(node.right != null){
                int index = al.size();
                al.add(index,node.right);
            }
            System.out.print(al.get(0).data+" ");
            al.remove(0);


        }
    }

}