Shortest path problem-this question is absolutely classic (Huawei's 2014 machine recruitment questions)

Source: Internet
Author: User

Problem description


Two subway lines are known, where A is the ring line and B is the east-west line. The lines are bidirectional. The names of the sites are as follows, and the interchange points of the two lines are represented by T1 and T2. Write a program, enter the names of the two sites at will, and output the minimum number of stations that need to pass by subway (including the input start and end points, and the transfer site is calculated only once ).
Subway Line A passes through the station: A1 ?? A2 ?? A3 ?? A4 ?? A5 ?? A6 ?? A7 ?? A8 ?? A9 ?? T1 ?? A10 ?? A11 ?? A12 ?? A13 ?? T2 ?? A14 ?? A15 ?? A16 ?? A17 ?? A18
Subway Line A (straight line) goes through the station: B1 ?? B2 ?? B3 ?? B4 ?? B5 ?? T1 ?? B6 ?? B7 ?? B8 ?? B9 ?? B10 ?? T2 ?? B11 ?? B12 ?? B13 ?? B14 ?? B15

Input: enter two different site names.
Output: outputs the least number of stations that have passed through, including the input start point and end point. The transfer site is calculated only once.

 

Optimized answer Portal: http://blog.csdn.net/ganze_12345/article/details/12419527


 

Analysis of ideas:

I don't know how you think about this question. I see that the first reflection of this question is that he asked me to find the shortest path, while the shortest path is commonly used by Dijkstra, floyd and A * algorithms, and A * algorithms are especially commonly used (I am familiar with A * algorithm and have worked on this project before ), so I started to write with the * algorithm. Start to build a graph by name .... to be honest, this graph has no regularity. We must build it A little bit, especially the connection from the node. It took me about 20 minutes to build the graph and then I began to write the * algorithm. A * the most critical issue of an algorithm is value generation. In fact, this problem is not A good choice for value generation. It took me 30 minutes to design A value-Substitute function, so I began to reflect on it. I thought that, in addition, I wrote the * algorithm again, in addition, debugging should take about one hour. However, Huawei only gave us one hour in total, so I began to deny this idea.

Then I naturally thought about it. For this type of question, we can use the depth traversal of the graph to get multiple paths, and then I can select the shortest one. This seems to be a good method, but the premise is that I have to build an image, so it will take 20 minutes to build the image, and it will take only 40 minutes to build the image, it may be difficult to consider fault tolerance and robustness. I began to deny my idea.

I think, is there a way to complete the code in the shortest time with higher encoding efficiency? I read the question carefully again. He only needs to output the least number of stations that have passed through, and does not need to specify which station is the same, so a very stupid but clear-thinking method came into my mind.

The analysis shows that:

This figure appears. We can divide all the lines into five segments and two points, with a total of seven conditions. Therefore, if I leave 7x7 = 49 in all cases, we only need to exchange the start and end points in many cases. The following code appears:

Total time: 49 minutes. As long as the idea is clear at the beginning, writing is not prone to errors, although this code is difficult to maintain.

 

# Include <math. h>/* determine the path of the name */int Which (string name) {if (name [0] = 'B') // assume that name1 is the start point, now, only the starting point {if (atoi (& name [1]) <= 5 & atoi (& name [1])> = 1) is considered) {// B is the first section return 1;} else if (atoi (& name [1]) <= 10 & atoi (& name [1])> = 6) {return 3;} else if (atoi (& name [1]) <= 15 & atoi (& name [1]) >=11) {return 5 ;}} else if (name [0] = 'A') {if (atoi (& name [1]) <= 13 & atoi (& name [1])> = 10) return 2; else if (atoi (& name [1]) <= 9 & atoi (& Name [1])> = 1) | (atoi (& name [1]) <= 18 & atoi (& name [1])> = 11 )) return 4;} else if (name [0] = 'T') {if (atoi (& name [1]) = 1) return 6; else if (atoi (& name [1]) <= 13) return 7;} elsereturn 0; // site not on this road} # define T1_1 (id) (6-id) // 1-> Number of T1 stations # define T2_1 (id) (T1_1 (id) + 5) // 1-> Number of T2 stations # define T1_2 (id) (id-8) // 2-> T1 # define T2_2 (id) (15-id) // 2-> T2 # define T1_3 (id) (id-4) // 3-> T1 # define T2_3 (id) (12-id) // 3-> T2 # define T1_5 (Id) (6 + id-10) // 5-> T1 # define T2_5 (id) (id-9) // 5-> T2 // interface function int BestRouting (string name1, string name2) {// divided into 5 segments and two nodes into consideration // B1-B51 // A10-A132 // B6-B103 // A1-A9 // A14-A184 + B11-B155 // T16 // T27 // 1-> (, 3, 4) paths can easily analyze int id1 = atoi (& name1 [1]), id2 = atoi (& name2 [1]); if (Which (name1) = 1) // start point: {if (Which (name2) = 1) on section 1 // return abs (id1-id2) + 1 on section 1; else if (Which (name2) = 2) // end section in 2 return T1_1 (id1) + id2-9; else if (Which (Name2) = 3) // the road section at 3 return T1_1 (id1) + id2-5; else if (Which (name2) = 4) {if (id2 <17) return T2_1 (id1) + id2-13; else if (id2 = 18) return T1_1 (id1) + 11; else {return T1_1 (id1) + 10-id2 ;}} else if (Which (name2) = 5) {return T2_1 (id1) + id2-10;} else if (Which (name2) = 6) return T1_1 (id1 ); else if (Which (name2) = 7) return T2_1 (id1);} else if (Which (name1) = 2) {if (Which (name2) = 1) return BestRouting (name2, name1); else If (Which (name2) = 2) return abs (id1-id2) + 1; else if (Which (name2) = 3) {if (id1 <= 11 & id2 <= 8) return T1_2 (id1) + T1_3 (id2)-1; if (id1> = 12 & id2> 8) return T2_2 (id1) + T2_3 (id2)-1; if (id1 <= 11 & id2> 8) return T2_2 (id1) + T2_3 (id2)-1; elsereturn T1_2 (id1) + T1_3 (id2)-1;} else if (Which (name2) = 4) {if (id2> = 3 & id2 <= 9) return T1_2 (id1) + 10-id2; else if (id2 <3) return T2_2 (id1) + id2-1 + 6; elsereturn T2_2 (id1) + Id2-13;} else if (Which (name2) = 5) return T2_2 (id1) + id2-10; else if (Which (name2) = 6) return T1_2 (id1 ); else if (Which (name2) = 7) return T2_2 (id1);} else if (Which (name1) = 3) {if (Which (name2) = 1) return BestRouting (name2, name1); else if (Which (name2) = 2) return BestRouting (name2, name1); else if (Which (name2) = 3) return abs (id1-id2) + 1; else if (Which (name2) = 4) {if (id2> = 3 & id2 <= 9) return T1_3 (id1) + 10-id2; else if (id2 <3) return T2_3 (id1) + id2-1 + 6; elsereturn T2_3 (id1) + id2-13;} else if (Which (name2) = 5) {return id2-id1 + 2;} else if (Which (name2) = 6) return T1_3 (id1); else if (Which (name2) = 7) return T2_3 (id1);} else if (Which (name1) = 4) {if (Which (name2) = 5) {if (id2> 5 & id2 <14) return 15-id1 + id2-10; else if (id2 <= 5) return id1 + 7 + T2_5 (id2 ); elsereturn id1-13 + T2_5 (id2);} else if (Which (name2) = 6) {if (Id1 <= 18 & id1> = 14) return 6 + id1-13; else if (id1 <= 9) return 11-id2; elsereturn 19-id1 + 10 ;} else if (Which (name2) = 7) {if (id1> = 5 & id1 <= 9) return 6 + id1-8; else if (id1 <5) return id1 + 6; elsereturn id1-12;} else if (Which (name2) = 4) {if (id1 <= 9 & id1> = 8) & (id2 <= 15 & id2> = 14) return 6 + 10-id1 + id2-13; else {if (id1 <= 9 & id2 <= 9) return abs (id1-id2) + 1; else if (id1> = 14 & id2> = 14) re Turn abs (id1-id2) + 1; else if (id1> id2) return 18-id1 + id2 + 1; else if (id1 <id2) return 18-id2 + id1 + 1; else if (id1 = id2) return 0 ;}} else if (Which (name2 )! = 0) return BestRouting (name2, name1);} else if (Which (name1) = 5) {if (Which (name2) = 6) return T1_5 (id1 ); else if (Which (name2) = 7) return T2_5 (id1); else if (Which (name2 )! = 0) return BestRouting (name2, name1);} else if (Which (name1) = 6) {if (Which (name2) = 6) return 0; else if (Which (name2) = 7) return 6; else if (Which (name2 )! = 0) return BestRouting (name2, name1);} else if (Which (name1) = 7) {if (Which (name2) = 7) return 0; if (Which (name2 )! = 0) return BestRouting (name2, name1);} return-1;} int main (int argc, char * argv []) {cout <BestRouting ("A1 ", "A11"); return 0 ;}


 

 

I was deeply touched by this incident. I remembered how a teacher taught us programming. She said that there will never be the best code or the worst code, and it will always be the best. I believe that many people will definitely think that this code is compiled by a new hand who just learned a few syntaxes. However, this new code can definitely help you a lot in Huawei's test room.

Of course, this method is good for discussing clear questions about this simple structure, but it certainly cannot be used for complicated structures.

If you have a better method, hope you can teach me. Thank you.


I saw a better method on the Internet. Although his code is a bit problematic, it is only a small problem, that is, the starting point is not a site, but it is worth learning.

Http://blog.csdn.net/suren__123/article/details/10985305

For your reference, it is indeed better than my method.

Related Article

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.