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