Java breadth-First search---Find the shortest path from point A to point B __java

Source: Internet
Author: User
Preface

The two basic search algorithms in search domain are breadth-first search and depth-first search.

Search algorithms can be used to find the connectivity of graphs.
A normal two-dimensional map, from point A to point B, has two problems. Can reach. How to go to point B with the shortest path.

This is what the search algorithm can solve.

To use the breadth-first search algorithm to search for the shortest path from point A to point B

According to the principle of breadth first, each step, will be next to all possible options after the team, before the next step

End tag:

End of search when queue is empty
Search End Implementation function when searching for specified results

In a two-dimensional map to calculate the minimum number of steps from A to B point of output the path of the minimum number of steps in Chinese version reference implementation

     * * A * B C * D G E * F * search from a              Minimum steps to f * Width first search: * Step One: * A team, the number of steps is 0 * A for the queue head * Second step: *
     See the next step for all of the options * have B and C, so the second can reach B and C, the steps are 1 * b, c team, and Mark B and C, said has passed.
     * A is useless, because a all possible points are already in the queue, so a team. * To determine whether to reach the target location, is to end the cycle. The current number of steps is the shortest step * Otherwise continue * At this time the queue head is b * Third step: * B, C is the team, so two must be in the third step in the expansion
     Show all possible.  * * 1.                  See all options for b next * have D, G * d, G team, Mark D, G * B is useless, b out Decide whether to reach the target location, yes, end the loop.  The current number of steps is the shortest step * Otherwise continue * At this time the queue head is c * 2.
     See all of the next steps in C * have G, E, but G has been marked through * so only e optional * E to team up, while marking E * C is invalid, c outTeam * At this time the queue head for D * Merge B, c expansion out of all the choices, there are D, G, E * So the third step can achieve is D, G, E, step number is 2 * To determine whether to reach the target location, is to end the cycle.  The current number of steps is the shortest step * Otherwise continue * Fourth Step: * Search D, G, e all extensions may * * 1. See all options in the next step * have F,f is the target, F team, at this time the number of steps is 3 * d out teams * to determine whether to reach the target location, yes, end Cycle.
     The current number of steps is the shortest step * Find F is the target, end traversal. * * A minimum number of steps to f is 3 * *
Code Implementation
/** * A map of 5 rows and 4 columns * Some of these places have obstacles that can't be found. * Find out the minimum number of steps from (1, 1) to (4, 3) * Output path/public class Walkinglabyrinth {static int m
    Axline = 6;
    static int maxrow = 5;
    Static int[][] map = new Int[maxline][maxrow];
    Static int[][] Mark = new Int[maxline][maxrow];
    static int targetline = 4;
    static int targetrow = 3;
    static int step =-1;
    Static int[][] Nextdirection = {0,-1}, {1, 0}, {0, 1}, {-1, 0}};
        public static void Main (string[] args) {map[1][3] = 1;
        MAP[3][3] = 1;
        MAP[4][2] = 1;
        MAP[5][4] = 1;
        Block all roads//map[4][4] = 1;
        MAP[5][3] = 1;
        BFS (1, 1); System.out.print (step = = 1?)
        "\ n Not arrived": "\ n Arrival");
    System.out.println ("Target position:" + targetline + "Row" + Targetrow + "column, steps:" + step);
        public static void BFs (int startLine, int startrow) {Mark[startline][startrow] = 1;
   node[] Queue = new node[maxline * MaxRow];     int head = 0;
        int tail = 0;
        Node tnode = new Node (startLine, StartRow, 0, head);
        queue[tail++] = tnode;

        int Tline, Trow; While [head < tail] {for (int i = 0; i < 4; i++) {tline = Queue[head].getline () + NEX
                TDIRECTION[I][0];

                Trow = Queue[head].getrow () + nextdirection[i][1];
                if (tline >= maxline | | trow >= MaxRow | | Tline < 1 | | Trow < 1) {continue; } if (Mark[tline][trow]!= 1 && map[tline][trow]!= 1) {tnode = new Node
                    (Trow, Tline, Queue[head].getstep () + 1, head);
                    queue[tail++] = tnode;
                    System.out.println ("Current position:" + tline + "Row" + Trow + "column, Step number:" + queue[tail-1].getstep ());
                Mark[tline][trow] = 1; } if (Tline = = Targetline && Trow = targetrow) {step = queUe[tail-1].getstep ();
                    System.out.println ("Through Path:");
                    int index = TAIL-1; for (int j = 0; J <= Step; j +) {System.out.println ("+ queue[index].getline () +", "+ queue[
                        Index].getrow () + ")");
                    index = QUEUE[INDEX].GETP ();
                } return;
        }} head++;
    Step =-1;
        private static class Node {int row, line, step;

        int p;
        public int Getp () {return p;
            Node (int line, int row) {this.line = line;
        This.row = row;
            Node (int row, int line, int step, int p) {this.row = row;
            This.line = line;
            This.step = step;
        THIS.P = p;
        public int GetRow () {return row; public int getline () {return Line;
        public int Getstep () {return step; }
    }
}
Results
Path: (4,3) (5,3) (5,2) (5,1) (4,1) (3,1) (2,1) (1,1)

to the target location: 4 rows 3 columns, steps: 7
a Little Harvest

The original simple simulation queue, can be simple to such a degree.
An array of two indexes.

int index = TAIL-1;
                    for (int j = 0; J <= Step; j +) {
                        System.out.println ("+ queue[index].getline () +", "+ queue[index].getrow () +") ");
                        index = QUEUE[INDEX].GETP ();
                    }

Feel my code is full of wisdom en, this is the legendary ノ*・ω・ (ノ), well, it's backwards.

Have seen before, after less than three months, and then look like a new one. It's too long to be used, or it will take a long time to become familiar. All right, take a peek at the code and leave a little impression. End

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.