English 中文(简体)
进入NxN 2D阵列的 no状态
原标题:Getting access to the state of a node in NxN 2D array

在 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
最佳回答

1) You can represent the goal node as Node goalNode. You can have a constructor/function/property in you Node class to set the state of the node. something like this:

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]);
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签