/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static double heuristic( int dx, int dy ) {
return dx + dy;
}
static class INode {
int x;
int y;
boolean walkable = true;
private double g;
private double h;
private boolean closed;
private boolean open;
private INode parent;
public INode(int x, int y) {
this.x = x;
this.y = y;
}
public void setG(double g) {
this.g = g;
}
public double getG() {
return g;
}
public void setH(double h) {
this.h = h;
}
public double getH() {
return h;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isClosed() {
return closed;
}
public void close() {
closed = true;
}
public boolean isOpened() {
return open;
}
public void open() {
open = true;
}
public void setParent(INode node) {
this.parent = node;
}
}
static class Grid {
final int SIZE = 32;
INode[][] nodes = new INode[SIZE][SIZE];
{
getNode(5, 5).walkable = false;
getNode(5, 6).walkable = false;
getNode(6, 5).walkable = false;
}
boolean valid( int x, int y ) {
return 0 <= x && x < SIZE && 0 <= y && y < SIZE;
}
public INode getNode(int x, int y) {
return nodes[x][y] != null ? nodes[x][y] : (nodes[x][y] = new INode(x, y));
}
public INode[] getNeighbors(int x, int y) {
List<INode> result = new ArrayList<>(8);
for ( int ix = x - 1; ix <= x + 1; ix += 1 ) {
for ( int iy = y - 1; iy <= y + 1; iy += 1 ) {
if ( !(ix == x && iy == y) && valid( ix, iy ) && getNode( ix, iy ).walkable ) {
result.add( getNode( ix, iy ) );
}
}
}
return result.toArray( new INode[0] );
}
}
static Grid grid = new Grid();
static INode findMinF( List<INode> nodes ) {
}
private static double Weight = 1d;
public static int[][] backtrace(INode node) {
List<int[]> result = new ArrayList<>();
INode p = node;
while ( p != null ) {
result.add( new int[] {p.getX(), p.getY()} );
p = p.parent;
}
return result.toArray( new int[0][] );
}
}
public static int[][] findPath( int startX, int startY, int endX, int endY) {
ArrayList<INode> openList = new ArrayList<INode>();
INode startNode = grid.getNode(startX, startY);
double SQRT2
= Math.
sqrt(2); INode node, neighbor;
INode[] neighbors;
int x, y;
double ng;
startNode.setG(0);
startNode.
setH( heuristic
( Math.
abs(startX
- endX
),
Math.
abs(startY
- endY
) ) );
openList.add(startNode);
startNode.open();
while ( !openList.isEmpty() ) {
node = findMinF( openList );
openList.remove(node);
node.close();
if ( node.getX() == endX && node.getY() == endY ) {
//Success
return Util.
backtrace(node
); }
neighbors = grid.getNeighbors( node.getX(), node.getY() );
for ( int i = 0; i < neighbors.length; i++ ) {
neighbor = neighbors[i];
if ( neighbor.isClosed() ) {
continue;
}
x = neighbor.getX();
y = neighbor.getY();
ng = node.getG() + ( ((x - node.getX())&(y - node.getY())) == 0 ? 1.0 : SQRT2);
if ( !neighbor.isOpened() || ng < neighbor.getG() ) {
neighbor.setG( ng );
neighbor.
setH( Weight
* heuristic
(Math.
abs(x
- endX
),
Math.
abs(y
- endY
)) ); neighbor.setParent( node );
}
if ( !neighbor.isOpened() ) {
openList.add(neighbor);
neighbor.open();
}
}
}
//fail
return new int[0][0];}
public static void main
(String[] args
) { int[][] path = findPath( 1, 1, 10, 10 );
for ( int[] dot : path ) {
System.
out.
printf( "[%2d, %2d]%n", dot
[0], dot
[1] ); }
}
}