簡體   English   中英

我怎么知道我的頂點有連接?

[英]How do I know my vertex has a connection?

我正在使用圖形數據結構制作Hunt the Wumpus游戲。 它的每個頂點是一個大小為4的數組,代表每個方向,即北東南西。 方向存儲在枚舉中。 當一個頂點與另一個頂點相連時,它的鄰接關系應該發生變化,以便與另一個頂點相連(我對此仍然感到困惑-為什么我的頂點是一個大小為4的數組,但只有1個方向)。

當我將一個頂點連接到另一個頂點並打印出鄰居的數組列表時,我得到一個奇怪的輸出。 當我打印未連接的頂點時,鄰居會得到[null,null,null,null]。 當我打印連接的頂點時,我得到[null,null,null,[null,null,null,null]。 我不知道鄰居應該是什么-他們應該以表示方向枚舉索引或方向的整數形式打印出來嗎? 應該打印什么?

import java.util.ArrayList;
import java.util.Collection;

 enum Direction{
    EAST,
    WEST,
    NORTH,
    SOUTH
}
public class Vertex extends Agent implements Comparable<Vertex>{
    private Vertex[] connections;
    private Direction dir;
    private int cost;
    private boolean marked;


    public Vertex(double x0, double y0){
        super( x0,  y0);
        connections = new Vertex[4];
        this.dir = dir;
        this.cost = cost;
        this.marked = marked;
    }

    public Direction opposite(Direction dir) {
    //that returns the compass opposite of a direction (i.e. South for North...)
        if(dir == Direction.EAST){
            dir = Direction.WEST;
        }
        else if(dir == Direction.WEST){
            dir = Direction.EAST;
        }
        else if(dir == Direction.NORTH){
            dir = Direction.SOUTH;
        }
        else dir = Direction.NORTH;

        return dir;
    }
    void connect(Vertex other, Direction dir){
    //modify the object's adjacency list/map so that it connects with the other Vertex. This is a uni-directional link.
        connections[dir.ordinal()] = other;

        }

    Vertex getNeighbor(Direction dir){
    //returns the Vertex in the specified direction or null.
        return this.connections[dir.ordinal()];
        }

    Collection getNeighbors(){
    //returns a Collection, which could be an ArrayList, of all of the object's neighbors.
        ArrayList<Vertex> neighborList = new ArrayList<Vertex>();
            for (Direction dir: Direction.values()){
                neighborList.add(this.getNeighbor(dir));
            }

            return neighborList;
        }

    public int getCost(){
        return this.cost;
    }

    public int setCost(int c){
        return this.cost = c;
    }

    public boolean getMarked(){
        return this.marked;
    }

    public boolean setMarked(boolean m){
        return this.marked = m;
    }

    public int compareTo(Vertex other){
        return (this.cost - other.cost);
    }

    /*
    * returns a string containing the number of neighbors, the cost, and the marked flag
    */
    public String toString(){
        String s = "";
        s += this.getNeighbors() + "\n" ;
        s += "cost: " + this.cost + "\n";
        s += "marked: " + this.marked;
        return s;

    }

    public static void main (String args[]){
        Vertex newE = new Vertex(0.5, 0.5);
        Vertex newerE = new Vertex(0.123, 1.56);
        newE.connect(newerE, Direction.SOUTH);
        newE.setCost(1);
        newE.setMarked(true);
        System.out.println(newE.toString());
    }

}

好吧,當您獲得輸出[null,null,null,[null,null,null,null]]時,您知道存在對已連接節點的引用。 因此,第二個陣列由第一個陣列連接,反之則不然。 因為您的代碼沒有連接第二個節點,但是第一個已連接,我認為您不應該打印節點列表,這會導致問題。

一種情況是,節點連接在一起,因此您將父級連接到了子級,而子級連接到了父級。 現在打印出來時,您將獲得遞歸執行。

[null,null,null,child [parent [[null,null,null,child [...]]],null,null,null]]

這將導致異常。 您可以為此使用標識符,例如ID。 因此,您只需打印出所連接對象的ID,或者打印一個位置->

[1,NULL,NULL,2]

[(X,Y),(X,Y),(X,Y),(X,Y)]

只需使用一些東西來標識節點,之后您就可以輕松查看該節點是否存在或為空。

因此,另一點是,您知道要連接4個點,所以為什么不使用:

private Node north;
private Node south;
private Node west;
private Node east;

這將在以后變得更容易,並且您不再需要使用該枚舉。 而且您也可以刪除Vertex類中的方向,這樣也會使Vertex成為一個點,而點卻沒有方向:)

或者,您僅為此使用2x2網格,例如:

private Node[][] 

您可以通過知道訪問鄰居的位置來指定網格的大小。 例如,坐標[1,1]由[0,1],[1,0],[2,1],[1,2]連接。 您還可以輕松打印出整個網格。

等距網格示例

您可以將該計算結果放入算法中,以檢查鄰居。 因此,您不必在每個節點類中都保存節點。 您可以通過訪問此對象獲得渲染位置,例如(僅當您要渲染它時):

node[1][2].getX();

我也建議您看看樹形結構:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM