1211
This topic, really, with C + + is not difficult, I can write it in a few minutes, but changed to Java, I was cool ...
Really do not understand, Java Squadron implementation AH.
Read someone else's blog, just know to understand, Java really is abuse me million times ah ....
queue< type > que = new linkedlist<point> ();
This is implemented with classes packaged in Java,
such as queue<int> que = new linkedlist<point> ();
Two of these queues are frequently used, que.offer (content), to add elements to the end of a queue.
Que.poll () takes out the first element and deletes it from the queue.
,,
No nonsense, look at the code.
Import Java.util.Arrays;
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.Scanner;
Class Point
{
int x;
int y;
int step;
}
public class Main
{
static final int MAX = 9;
static int arr[][] = new int[][]{{1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,1},{1,0,0,1,1,0,0,0,1},
{1,0,1,0,1,1,0,1,1},{1,0,0,0,0,1,0,0,1},{1,1,0,1,0,1,0,0,1},
{1,1,0,1,0,1,0,0,1},{1,1,0,1,0,0,0,0,1},{1,1,1,1,1,1,1,1,1}};
Static point Start,end;
static final int N = 4;
static int x[] = new Int[n];
static int y[] = new Int[n];
public static void Main (string[] args)
{
Init ();
int n;
Scanner cin = new Scanner (system.in);
n = cin.nextint ();
start = new Point ();
end = new Point ();
for (int i = 0; i < n; i++)
{
Start.x = Cin.nextint ();
Start.y = Cin.nextint ();
Start.step = 0;
End.X = Cin.nextint ();
End.y = Cin.nextint ();
if (start.x = = End.X && Start.y = = end.y)
{
System.out.print (0 + "\ n");
Continue
}
BFS ();
}
}
static void BFS ()
{
Queue<point> que = new linkedlist<point> ();
Boolean mark[][] = new Boolean[max][max];
for (int i = 0; i < MAX; i++)
{
for (int j = 0; J < MAX; J + +)
{
MARK[I][J] = false;
}
}
Que.offer (start);
MARK[START.X][START.Y] = true;
while (!que.isempty ())
{
Point z = new Point ();
z = Que.poll ();
for (int i = 0; i < N; i++)
{
Point k = new Point ();
k.x = z.x + x[i];
K.Y = Z.y + y[i];
K.step = z.step+1;
if (k.x >= 0 && k.x < MAX && k.y >= 0 && k.y < MAX && Mark[k.x][k.y] = = False & amp;& arr[k.x][k.y] = = 0)
{
if (k.x = = End.X && k.y = = end.y)
{
System.out.print (k.step + "\ n");
Return
}
Que.offer (k);
}
}
}
}
static void Init ()
{
X[0] = 1;
X[1] =-1;
X[2] = x[3] = 0;
Y[0] = y[1] = 0;
Y[2] =-1;
Y[3] = 1;
}
}
Note that the start point and end point are considered to be the same point, and will time out if not considered.