在 Java2D阵列安装NxN puzzle。 我有以下班子:
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)
How should I represent the goal Node?
In the second class I declare the goal node as int[][]
but I want it to be a node so that I can give it to the manhattan function.
2)
In the Node class, I have a state int state[][]
which represent the state of the node. Now my problem is how do i get access to the individual coordinates in the state in the nodes. Assuming the goalNode declaration is correct then I must be able to call the manhattan like this:
manhattan(startNode, goalNode)
计算从目前的 no到目标节点。
Edit
我需要以以下方式使用手法:
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