I am solving an NxN puzzle in java 2D array. I can move to the empty tile in say four directions : LEFT, RIGHT, DOWN or UP. My question is if i get the indices(row en column values) of the empty tiles(node), how can i know if i must move LEFT, RIGHT, DOWN or UP to generate its successor(neighbour) nodes.
For instance if it is a 1Dimensional array of 9 elements then i can do something like: index is the index of the empty tile
if(index == 0){
tempSuccessorNodes.add(new Node(swap(0,1,arrayPosition),curNode));
tempSuccessorNodes.add(new Node(swap(0,3,arrayPosition),curNode));
}
else if(index == 1){
tempSuccessorNodes.add(new Node(swap(0,1,arrayPosition),curNode));
tempSuccessorNodes.add(new Node(swap(1, 4, arrayPosition),curNode));
tempSuccessorNodes.add(new Node(swap(1, 2, arrayPosition),curNode));
}
....
if(index == 8){
tempSuccessorNodes.add(new Node(swap(8, 7, arrayPosition),curNode));
tempSuccessorNodes.add(new Node(swap(8, 5, arrayPosition),curNode));
}
to generate the successors of the current node. But here am dealing with NxN(which 3×3 is an instance). How do i know if i must move LEFT, RIGHT, DOWN or UP after knowing the indices of the empty cell/tile/node?
I have a question here i posted earlier which relates to the same task am handling
Thanks
You’ll have to take two things into account, to determine in which direction you can move: