Solve a Bo asked questions, quite simple, write down to meet their own small sense of accomplishment

Source: Internet
Author: User

First issue: http://q.cnblogs.com/q/77610/

public static string[] Stops = new string[] {"AB", "BC", "CD", "DC", "DE", "AD", "CE", "EB", "AE"}; Algorithm: Enumerates a collection of paths between any 2 letters contained in the array. For example: A-c  has ABC (AB-BC), ADC (AD-DC)
View Code

I have only learned simple data structure, so the simple problem to take out Ah, forgive me ~

According to the problem, can be abstracted as, give you a set of points and points between the connection, and then give you a starting point and the end point, you have all the matching path printed out is finished.

Very simple, with a map ah, but the premise is that the input data can not form a ring, or the program will loop up the ~

Post code:

1  Packagecnblogs;2 3 ImportJava.util.Stack;4 5 /**6 * Created by Kischn on 2015/11/21.7  */8  Public classTest {9 Ten     /** One * Map with direction A * The data must be constructed to ensure that there is no ring -      */ -      Public Static Boolean[] m =New Boolean[26] [26]; the  -     /** - * Print your way through -      * @param the +      */ -      Public Static voidPrintway (stack<integer>) { +          for(Integer node:way) { ASystem.out.print ((Char) (node + ' A ')); at         } - System.out.println (); -     } -  -     /** - * Find the end point and record the path to go in      * @paramStack -      * @param the to      * @paramEnd +      */ -      Public Static voidFind (stack<integer> Stack, stack<integer>,intend) { the         if(Stack.isempty ())return; *Integer tmp =Stack.pop (); $ Way.add (TMP);Panax Notoginseng         if(TMP = = end) {//End node found - Printway (to); the}Else { +              for(inti = 0; I < 26; i++) { A                 if(M[tmp][i]) { the Stack.push (i); + find (Stack, it, end); -                 } $             } $         } - Way.pop (); -     } the  -      Public Static voidMain (string[] args) {WuyiString[] Stops =NewString[] {"AB", "BC", "CD", "DC", "DE", "AD", "CE", "EB", "AE" }; the         CharStart = ' A '; -         Charend = ' C '; Wu  -         Charb; About          for(inti = 0; i < stops.length; i++){ $A = Stops[i].charat (0); -b = Stops[i].charat (1); -M[a-' A '][b-' a '] =true; -         } AStack<integer>NewStack<integer>(); +stack<integer> stack =NewStack<integer>(); theStack.push (Start-' A '); -Find (Stack, the, end-' A '); $     } the  the}

Solve a Bo asked questions, quite simple, write down to meet their own small sense of accomplishment

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.