"Programming Marathon" "011-hero"

Source: Internet
Author: User

"Programming Marathon algorithm directory >>>" "011-hero" "Project Download >>>" 1 Topic Description

500 years ago, Nowcoder was the most remarkable swordsman in our country. He is handsome and smart and ^_^. Suddenly one day, Nowcoder beloved Princess was trapped in a huge maze by the devil. Nowcoder heard that the news has been two days later, he knew that the princess in the maze can persist T days, he hurried to the maze, began to search everywhere for the whereabouts of the princess. Time 1.1 points of the past, Nowcoder still can't find the princess. Finally, when he found the princess, the beautiful princess was dead. Since then Nowcoder sullen, Chafanbusi not think, a year later followed the Princess and went. T_T 500 years later today, Nowcoder dreams to you, I hope you help him to determine whether he had the opportunity to find the princess in a given time. He will provide you with a map of the maze and the time remaining T. Please judge if he can save the beloved princess.

1.1 Input Description:

Topics include multiple sets of test data.
Each set of test data starts with three integers n,m,t (00), each representing the length and height of the maze, and the number of days the princess can hold.
followed by M-line, N-column characters, consisting of ".", "*", "P", "S". which
"." represents an open space that can be walked.
"*" stands for the wall, Nowcoder cannot pass from this.
"P" is where the princess is located.
"S" is the starting position of the Nowcoder.
Each time period Nowcoder can only select "Up, down, left, right" in any direction to go one step.
The input ends with 0 0 0.

1.2 Output Description:

If the princess can be rescued within the specified time output "YES", otherwise output "NO".

1.3 Input Example:
4 4 10............S**P0 0 0
1.4 Output Example:
YES
2 ideas for solving problems

You can use breadth-first traversal. From the starting point lap by Loop, the first place to traverse is the least time, each lap consumes a unit of time.

Figure 2-1 Breadth-first traversal

3 Algorithm Implementation
ImportJava.util.LinkedList;ImportJava.util.List;ImportJava.util.Scanner;/** * Solution One * Author: Wang Junshu * time:2016-05-09 18:58 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/w Ang-jun-chao * declaration:all rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {//Scanner Scanner = new Scanner (system.in);Scanner Scanner =NewScanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {//Maze size            intcol = Scanner.nextint ();introw = Scanner.nextint ();//Time remaining            intTime = Scanner.nextint ();if(Row = =0&& col = =0&& time = =0) { Break; }//nowcoder start position            intPX =0;intPY =0;//Princess's location            intSX =0;intSY =0;Char[] Maze =New Char[Row] [Col]; for(inti =0; i < row;                i++) {String line = Scanner.next (); Maze[i] =New Char[Col]; for(intj =0; J < Col; J + +) {Maze[i][j] = Line.charat (j);if(Maze[i][j] = =' P ') {px = i;                    py = j; }Else if(Maze[i][j] = =' S ') {SX = i;                    sy = j;        }}} System.out.println (Findpath (Maze, px, py, SX, SY, time));    } scanner.close (); }/** * Maze find the shortest path, use breadth first to traverse * * @param Maze Maze * @param px nowcoder start position. The horizontal axis * @param The starting position of the Py Nowcoder. Ordinate * @param The location of the SX Princess. Horizontal axis * @param The position of the Princess Sy. Ordinate * @param Time remaining * @return YES: The princess can be found in time, no: Cannot find the princess within time    Private StaticStringFindpath(Char[] Maze,intPxintPyintSxintSyintTime) {//Maze size        introw = Maze.length;intCol = maze[0].length;//Record breadth-first processing, current coordinates to be processedList<integer> Curr =NewLinkedlist<> ();//Record breadth-first processing, next lap coordinatesList<integer> next =NewLinkedlist<> ();        Curr.add (px); Curr.add (PY);//Mark the location that has been visitedMaze[px][py] =' * '; while(!curr.isempty ()) {px = Curr.remove (0); PY = Curr.remove (0);if(px = = SX && py = = sy) {return "YES"; }//System.out.println ("(" + px + "," + py + ")");            //go up            if(PX-1>=0&& MAZE[PX-1][PY]! =' * ') {Next.add (PX-1);                Next.add (PY); MAZE[PX-1][py] =' * '; }//GO right            if(py +1< col && Maze[px][py +1] !=' * ') {next.add (px); Next.add (py +1); Maze[px][py +1] =' * '; }//Go down            if(px +1< row && Maze[px +1][PY]! =' * ') {Next.add (px +1);                Next.add (PY); MAZE[PX +1][py] =' * '; }//Go left            if(PY-1>=0&& Maze[px][py-1] !=' * ') {next.add (px); Next.add (PY-1); Maze[px][py-1] =' * '; }//The current layer finishes processing            if(Curr.isempty ()) {//The remaining time is reducedtime--;//time is running out and not found                if(Time <0) {return "NO"; }//processing next layer                Else{list<integer> queue = Curr;                    Curr = Next;                Next = queue; }            }        }return "NO"; }}
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" "011-hero"

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.