2017-05-20 1 views
1

私はJavaでBFSアルゴリズムでサルとバナナの問題を解決しようとします。ここに私のコードは、これまででBFSアルゴリズムでサルとバナナを解決する

public class Program { 
    static final int[][] states={ 
      { 1, 1, 0, 0, 0, 0 }, //0 | 0 0 0 | 
      { 1, 4, 3, 4, 0, 0 }, //1 | 0 1 0 | 
      { 0, 3, 0, 0, 0, 0 }, //2 | 1 0 0 | 
      { 0, 4, 0, 0, 3, 1 }, //3 | 0 1 1 | 
      { 0, 0, 0, 3, 0, 0 }, //4 | 1 0 1 | 
      { 0, 0, 0, 1, 0, 1 }, //5 | 0 0 1 | 
    }; 
    static final String[] lblStates={ 
      "0 0 0", 
      "0 1 0", 
      "1 0 0", 
      "0 1 1", 
      "1 0 1", 
      "0 0 1" 
    }; 

    static class Node{ 
     public Node parent; 
     public int node; 

     Node(int node, Node parent) { 
      this.node = node; 
      this.parent = parent; 
     } 


    @Override 
    public boolean equals(Object obj) { 
     return this.node.equals(((Node)obj).node); 
    } 
    } 



    static void BFS(Node start, Node goal) throws InterruptedException { 
     if (start.equals(goal)){ 
      PrintPath(start); 
      return; 
     } 

     Queue<Node> open = new Queue<Node>(); 
     open.enqueue(start); 
     HashSet<Node> closed = new HashSet<Node>(); 

     while (!open.isEmpty()){ 
      Node x = open.dequeue(); 
      List<Node> successorsOfX = GetChildrenOf(x); 
      closed.add(x); 


      for (Node successor: successorsOfX) { 
       if (successor.equals(goal)){ 
        PrintPath(successor); 
        System.out.println(); 
        return; 
       }else if(!closed.contains(successor) && !open.equals(successor)){ 
        open.enqueue(successor); 
       } 
      } 
     } 
    } 


    static void PrintPath(Node node){ 
     if (node.parent != null){ 
      PrintPath(node.parent); 
      System.out.println(lblStates[node.node]); 
     }else { 
      System.out.println(lblStates[node.node]); 
     } 
    } 

    static List<Node> GetChildrenOf(Node parent){ 
     List<Node> result = new ArrayList<Node>(); 
     for (int i = 0; i <states.length ; i++) { 
      int[] cost=states[parent.node]; 
      if (!cost.equals(0)){ 
       Node newNode = new Node(i, parent); 
       result.add(newNode); 
       System.out.print(cost[i]); 
      } 
     } 
     System.out.println(); 
     return result; 
    } 


    public static void main(String[] args) throws InterruptedException { 
     int start = 0; 
     int goal = 4; 

     BFS(new Node(start, null), new Node(goal, null)); 
    } 
} 

条件は、(A1、A2、A3)

  1. A1です - サルはボックス
  2. A2上にある場合>参照 - >猿かどうかを確認しますボックス
  3. A3の近くにされて - ボックスはバナナ

スタート条件(0,0,0)、最終的な条件(1,0,1) 下であれば>見ます最終的に結果はそれが猿

しかし、私は実行するたびのパスである必要がありますそれは無限ループに陥り、パスを印刷しません。

答えて

1

クラスNodeでは、public int node;の代わりにpublic Integer node;を試してみてください。

それとも、私は等号(オブジェクトobj)リターンthis.node.equalsに実装しようreturn this.node.equals(((Node)obj).node);

1

私はあなたの問題はあなたがGetChildrenOf(...)で方法を新しい子ノードのインスタンスを作成しているので、HashSetのは、デフォルトの等号を使用しているので、あなたのclosedSetは((後継者を含むものとして登録したことがないということかもしれないと思います)メソッドが定義されていない場合、2つのインスタンスは新しい子ノードインスタンスを作成するため同じではないので、メソッドは何度も何度も後続ノードを追加し続け、無期限に移動します。

Nodeクラスのequals()メソッドを使用して、それが修正されているかどうかを確認します。

Go幸運!

+0

return this.node == ((Node)obj).node;に変更(((ノード)は、obj).nodeファイル)。それはメソッドequals(int)を解決できないと言います –

関連する問題