Minimum number of steps

Source: Internet
Author: User

As a C-learning, to be changed to use Java to write, the early stage is really a bit uncomfortable, so: well learn it

The topic is this:

Time limit:MS | Memory limit:65535 KB Difficulty:4
Describe

This has a maze, with 0~8 rows and 0~8 columns:

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

0 represents the road, and 1 represents the wall.

Now enter the coordinates of a road as the starting point, and then enter a road as the coordinates as the end point, ask at least a few steps to reach the end from the starting point?

(Note: One step is to move from one sitting punctuation to the next to the left and right adjacent coordinate points, such as: from (3,1) to (4,1). )

Input
the first line enters an integer n (0<n<=100), which indicates that there are n sets of test data;
Then n rows, each row has four integers a,b,c,d (0<=a,b,c,d<=8) representing the row, column, and end line of the starting point, respectively.
Output
The
output is at least a few steps away.
Sample input
1  5 1  6 7
Sample output
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.

Minimum number of steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.