2011-11-22 27 views
1

我在Java中使用二維數組實現NxN拼圖。我有以下類:在NxN 2D陣列中獲取節點狀態

public class Node { 

    //private members 
    private int boardSize; 
    private int row, col; 

    int state[][] = new int[][]{}; //the state of a node 

    // the total cost from root node to current node 
    private int pathCost; 
    // this is the heuristic cost from the current node to the goal node 
    private int heuristicCost; 
    // functionCost = pathCost + heuristicCost 
    private int funcitonCost; 
    // parent of the current node 
    private Node parentNode; 

    ...... 

    // I have here all accessor functions and functions that return x and y cordinates when a //number in the array is given. 
} 

public class A*Algo { 



    private int[][] goalNode ={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}}; 

    private NodeComparator nodeComparator = new NodeComparator(); 
    private PriorityQueue<Node> openList = null; // open list 
    private PriorityQueue<Node> closedList = null;  // closed list 
    private int steps = 0; 
    private int BOARDSIZE; 


      // constructor 
      public AStar(Node startNode, int boardSize){ 
     //this.node = new Node(tiles, null, boardSize); 
     //this.tiles = tiles; 
     this.BOARDSIZE = boardSize; 
    /* this.succesorNodes = new FifoNodeStore(); 
     this.fringeNodes = new FifoNodeStore();*/ 
     this.openList = new PriorityQueue<Node>(0, nodeComparator); 
     this.closedList = new PriorityQueue<Node>(0, nodeComparator); 

     startNode.setParentNode(null); 
     startNode.setPathCost(0); 
       // PROBLEM :::: goalNode must be a Node and not int[][] 
       // How can i represent the goal node? 
     startNode.setHeuristicCost(manhattan(startNode, goalNode)); 
     this.addToOpenList(startNode); 
     this.search(startNode); 
    } 



    public int manhattan(Node currentNode, Node goalNode) { 
    return Math.abs(currentNode.x - goalNode.x) + Math.abs(currentNode.y - goalNode.y); 
    } 

}

我有以下兩個問題:

1) 我應如何表示的目標節點? 在第二個類中,我將目標節點聲明爲int [] [],但我希望它是一個節點,以便我可以將它賦予曼哈頓函數。

2) 在Node類中,我有一個狀態int state[][],它代表節點的狀態。 現在我的問題是我如何獲得在節點中的狀態中的個人座標。假設goalNode decalaration是正確的,那麼我必須能夠調用曼哈頓這樣的:

manhattan(startNode, goalNode) 

從當前節點到目的節點計算。

我在這裏有點困惑。

感謝您的幫助。

編輯

時使用曼哈頓heureustic通過以下方式要求:

the sum of the vertical and horizontal distances from 
      the current node to the goal node/tile 

          +(plus) 

the number of moves to reach the goal node from the initial position 

回答

1

1)您能夠代表目標節點Node goalNode。你可以在你的Node類中有一個構造函數/函數/屬性來設置節點的狀態。 是這樣的:

public class Node { 

    //private members 
    private int boardSize; 
    private int row, col; 

    int state[][] = new int[][]{}; //the state of a node 

    public Node(int[][] nodeState) 
    { 
     state=nodeState; 
    } 

    ........ 
} 

2)如果我理解你正確地您正在尋找這樣的事情:

public int Manhattan(Node current Node goal){ 
    int dist = 0; 
    for(int x = 0; x < current.row; x++) 
     for(int y = 0; y < current.col; y++) 
      dist += Math.abs(current.state[x][y] - goal.state[x][y]); 
} 
+0

感謝。你能幫我解決第二個問題嗎? –

+0

你需要更清楚地解釋你的問題是什麼。 – Aliza

+0

好的,謝謝,我已經編輯了我的帖子,並要求曼哈頓的heureustic。通常我必須爲每個節點計算曼哈頓到目標節點,以便我可以知道哪個節點比另一個節點更好,以便獲得最佳路徑。 –