2011-06-02 108 views
0

我需要從包含多個「迷宮」表示爲鄰接表的文本文件做一個曲線圖。列表如下:創建從一個文本文件中的迷宮圖:Java的

A,G 
A,B,F 
B,A,C,G 
C,B,D,G 
D,C,E 
E,D,F,G 
F,A,E 
G,B,C,E 

D,F 
A,B,G 
B,A,C,E,G 
C,B,D,E,G 
D,C 
E,B,C,G 
F,G 
G,A,B,C,E,F 

F,A 
A,B,G 
B,A,G 
C,D,G 
D,C,G 
E,F,G 
F,E,G 
G,A,B,C,D,E,F 

每個「迷宮」的第一行包含迷宮(第一個字母)和迷宮(第二個字母)的結束節點的起始節點。

我分析的文本文件導入所有行(包括空行)的ArrayList,遂成線的ArrayLists的ArrayList(獨立迷宮的列表)。我通過將整行文本分成空白行來做到這一點。我現在的問題是,我無法弄清楚如何使用我的節點類,從這些「迷宮」構造圖。這裏是我的節點類:

package algo2; 

import java.util.ArrayList; 


public class Node<T>{ 

    private T value; //this Node<T>'s value 
    public ArrayList<Node<T>> connections; 
    public boolean isStart; 
    public boolean isEnd; 
    public boolean visited; 

    public Node(T value){ 
     this.value = value; 
     connections = new ArrayList<Node<T>>(); 
    } 

    //returns the value of this Node<T> 
    public T getValue(){ 
     return value; 
    } 

    //returns true if the node is connected to any other nodes 
    public boolean isConnected(){ 
     if(connections == null){ 
      return false; 
     } 
     return true; 
    } 

    //returns a list of nodes connected to this node 
    public ArrayList<Node<T>> getConnections(){ 
     return connections; 
    } 

    //sets the node's value 
    public void setValue(T value){ 
     this.value = value; 
    } 

    //adds a connection from this node to the passed node 
    public void connect(Node<T> node){ 
     connections.add(node); 
    } 

    public String toString(){ 
     return value+""; 
    } 
} 

有人可以指出我在正確的方向嗎?

回答

0

你應該在一個單獨的類編寫額外的代碼來構建迷宮。這將需要文本輸入,並輸出一組連接節點。節點本身不應該「構建」任何東西 - 它們只是構建塊。

下面是如何將它們連接一些僞代碼:

nameToNodeMap -> dictionary of string to node 
for each line in the file 
    previous token = null 
    loop: 
    try to get a token (letter, separated by commas) 
    if there was no token 
     break out of the loop 
    else 
     if nameToNodeMap contains that token 
      get the node for that token 
     else 
      create a node 
      add it to nameToNodeMap, with the token as the key 
     if previous node != null 
      link previous node to this node 
     previous nope = current node 
     goto loop 
0

一種解決方案將是使用在輸入分割()與適當的節點的構造函數(注意,我取代的字符串爲參數T代表簡單):

class Node { 
    public String value; 
    public String[] connections; 
    public boolean visited; 

    public Node (String value, String[] connections) { 
     this.value = value; 
     this.connections = connections; 
     visited = false; 
    } 

    public boolean isConnected(Node that) { 
     boolean res = false; 
     for (int i=0; !res && i<this.connections.length; i++) { 
      res = (this.connections[i] == that.value); 
     } 
     return res; 
    } 

    // more stuff ... :) 
} 

//read start, end nodes 

ArrayList<Node> nodes = new ArrayList<Node>(); 
for (each line in maze) { 
    String[] nodeList = line.split(","); 
    nodes.add(nodeList[0], Arrays.copyOfRange(nodeList, 1, nodeList.length)); 
} 

我認爲這將是比試圖保持每個節點內的節點列表簡單,只要你的應用程序的其它部分很容易簡單地檢查兩個節點被連接或如果有人訪問過。 (也請注意,您可能需要向Node類添加更多代碼以跟蹤開始和結束節點。)

1

讓我們專注於設置1個迷宮,然後我們可以重複所有他們。 我試圖寫一個Java的語法友好的算法。

所以,這裏是我們的第一個迷宮的ArrayList變量,按照我的理解......

List<String> preNodes, which contains: 
{"A,G", "A,B,F", "B,A,C,G", "C,B,D,G", "D,C,E", "E,D,F,G", "F,A,E", "G,B,C,E"}; 

因爲第一個String有着特殊的意義,讓我們分離它從休息。 (即將其設置爲單獨的字符串變量,並將其從ArrayList中移除)。

String startAndEnd, which contains: "A,G"; 
List<String> preNodes, which contains: 
{"A,B,F", "B,A,C,G", "C,B,D,G", "D,C,E", "E,D,F,G", "F,A,E", "G,B,C,E"}; 

現在,我們首先構建我們需要的所有節點,然後我們可以將它們連接起來。

//Define container for nodes 
HashMap<String, Node> nodes = new HashMap<String, Node>(); 
//Create a node for each letter 
for(String s : preNodes) { 
    String nodeName = s.charAt(0) + ""; 
    nodes.put(nodeName, new Node()); 
} 
//Link them up appropriately 
for(String s : preNodes) { 
    String[] splitS = s.split(","); //1 letter in each array cell. 
    Node current = nodes.get(splitS[0]); //Get the node we're going to link up. 
    for(int i=1; i<splitS.length; i++) { 
     current.connect(nodes.get(splitS[i])); 
    } 
} 
//Finally, set the start and end Node. 
String[] splitStartAndEnd = startAndEnd.split(","); 
nodes.get(splitStartAndEnd[0]).isStart = true; 
nodes.get(splitStartAndEnd[1]).isEnd = true; 

這應該這樣做,我想;現在「節點」包含了整個迷宮,全部聯繫起來。我在你的代碼捕獲的錯誤,但:你isConnected()函數將返回false,如果connections.isEmpty(),如果它不爲空。它不應該爲null,因爲您在構造函數中用新列表初始化它。