"Programming Marathon Count" "015-Walk The Maze"

Source: Internet
Author: User

"Programming Marathon algorithm Directory" " 015-Maze" "Project Download >>>" 1 Topic Description

Nowcoder Favorite Amusement Park Maze game, he and the small friends who first out of the maze.
Now give you the map of the maze, can you help him figure out how many steps it takes to get out of the maze?

1.1 Input Description:

The input contains multiple sets of data.
Each set of data contains a 10*10, by "#" and "." Composition of the maze. Where "#" represents the Wall; "." Representative access.
The entry is in the second column of the first row; The exit is in the last row nineth column.
From any one "." Point can go up and down in four directions "." Point.

1.2 Output Description:

For each set of data, the output from the inlet to the exit takes a few steps.

1.3 Input Example:
#.#########........##........##........##........##........##........##........##........#########.##.#########........#########.##........##.#########........#########.##........##.######.#########.#
1.4 Output Example:
1630
2 ideas for solving problems

Because test instructions is using the fewest steps out of the maze, the way to use breadth-first traversal is to take one step at the end of each process, with the least number of steps to reach the exit first. According to the input example, the maze of the Walk Method 1 is shown.

Figure 1 Maze Find Shortest path

3 Algorithm Implementation
ImportJava.util.ArrayDeque;ImportJava.util.Queue;ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-11 18:59 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public  class Main {    Private Final Static intN =Ten; Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnextline ()) {Char[] Maze =New CharN [N]; for(inti =0; i < N;            i++) {Maze[i] = Scanner.nextline (). ToCharArray ();        } System.out.println (Minstep (Maze));    } scanner.close (); }/** * To use the least steps out of the maze, the maze size fixed to 10*10, starting point for (0, 1), the end point for (9, 8) * * @param Maze Maze * @return out of the fan The least number of steps in the palace, go back-1 */    Private Static int Minstep(Char[] Maze) {//Record the location of the current processing layerQueue<integer> Curr =NewArraydeque<> (n * n);//Record the location of the next layer of processingQueue<integer> next =NewArraydeque<> (n * n);//Can be moved in four directions, two a group        int[] D = {1,0,0,1, -1,0,0, -1};//Add starting point;Curr.add (0); Curr.add (1);intXintY//Record the minimum number of steps        intStep =0; while(!curr.isempty ())            {x = Curr.remove (); y = Curr.remove ();//Find the end location            if(x = =9&& y = =8) {returnStep }//processing (x, Y) position in four directions             for(inti =0; i < d.length; i + =2) {intt = x + d[i];intv = y + d[i +1];if(T >=0&& T < N && v >=0&& v < N && maze[t][v] = ='. ') {//tag has been visitedMAZE[T][V] =' # ';//access to the location added to the queueNext.add (t);                Next.add (v); }            }//The current layer has been processed            if(Curr.isempty ()) {//number of steps plus onestep++;//processing next layerqueue<integer> temp = Curr;                Curr = Next;            Next = temp; }        }//Execute to this note cannot find a way out        return-1; }}
4 Test Results 5 Other information

Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon Count" "015-Walk The Maze"

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.