import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
{
Ideone obj = new Ideone();
if (!obj.loadMap())
{
System.
out.
println("wrong inputs"); return;
}
int c = obj.astarsearch();
if (c < 0)
{
System.
out.
println("failure"); }
else
{
System.
out.
println("success: " + c
); }
obj.printMap();
}
int[][] map = null;
int width = 0;
int height = 0;
Node start = null;
Node goal = null;
boolean loadMap()
{
Scanner sc
= new Scanner
(System.
in); if (!sc.hasNextInt())
{
return false;
}
width = sc.nextInt();
if (!sc.hasNextInt())
{
return false;
}
height = sc.nextInt();
map = new int[height][width];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (!sc.hasNextInt())
{
return false;
}
map[y][x] = sc.nextInt();
switch (map[y][x])
{
case 0:
{
break;
}
case 1:
{
start = new Node(x, y);
break;
}
case 2:
{
goal = new Node(x, y);
break;
}
case 3:
{
break;
}
default:
{
return false;
}
}
}
}
return start != null && goal != null;
}
class Node
{
public int x;
public int y;
Node(int x, int y)
{
this.x = x;
this.y = y;
}
}
void printMap()
{
if (map == null)
{
return;
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
System.
out.
print(map
[y
][x
] + " "); }
}
}
int parentValue(int x, int y)
{
return y * width + x;
}
int parentX(int pv)
{
return pv % width;
}
int parentY(int pv)
{
return pv / width;
}
double distance(int x1, int y1, int x2, int y2)
{
int x = x1 - x2;
int y = y1 - y2;
int s = x * x + y * y;
return Math.
sqrt((double)s
); }
double cost(int fromX, int fromY, int toX, int toY)
{
return 0;
}
int astarsearch()
{
if (start == null || goal == null)
{
return -1;
}
boolean[][] open = new boolean[height][width];
boolean[][] close = new boolean[height][width];
double[][] hstar = new double[height][width];
double[][] fstar = new double[height][width];
int[][] parent = new int[height][width];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
hstar[y][x] = distance(x, y, goal.x, goal.y);
}
}
open[start.y][start.x] = true;
fstar[start.y][start.x] = hstar[start.y][start.x];
Node n;
for (;;)
{
n = null;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (!open[y][x])
{
continue;
}
if (n == null)
{
n = new Node(x, y);
}
else if (fstar[y][x] < fstar[n.y][n.x])
{
n.y = y;
n.x = x;
}
}
}
if (n == null)
{
return -1;
}
if (n.x == goal.x && n.y == goal.y)
{
break;
}
open[n.y][n.x] = false;
close[n.y][n.x] = true;
double gstar = fstar[n.y][n.x] - hstar[n.y][n.x];
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
if (dy == 0 && dx == 0)
{
continue;
}
int x = n.x + dx;
int y = n.y + dy;
if (x < 0 || x >= width || y < 0 || y >= height)
{
continue;
}
if (map[y][x] == 3)
{
continue;
}
double fdash = gstar
+ hstar[y][x]
+ cost(n.x, n.y, x, y);
if (open[y][x] || close[y][x])
{
if (fdash >= fstar[y][x])
{
continue;
}
}
open[y][x] = true;
close[y][x] = false;
fstar[y][x] = fdash;
parent[y][x] = parentValue(n.x, n.y);
if (map[y][x] == 0)
{
map[y][x] = 8;
}
}
}
}
while (n.x != start.x || n.y != start.y)
{
if (map[n.y][n.x] == 8)
{
map[n.y][n.x] = 7;
}
int pv = parent[n.y][n.x];
n.x = parentX(pv);
n.y = parentY(pv);
}
int c = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (map[y][x] >= 7)
{
c++;
}
}
}
return c;
}
}