Huawei trial -- subway Transfer

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 B (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 station names output: output the least number of stations passed, with the input start point and end point, the transfer site only calculates the input sample once: A1 A3 output sample: 3

[Code]
# Include <stdio. h> # include <stdlib. h> # include <string. h> int jishi3 (char * Start, char * End) {int A1 [] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // distance from line A to station T1: int A2 [] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6 }; // distance between line A and station t2: int B1 [] = {6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // distance from line B to station T1 int B2 [] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6}; // The distance from line B to station T2 // The distance from line Achar AA [20] [5] = {"A1", "A2 ", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "T1", "A10", "A11 ", "A12", "A13", "T2", "A14", "A15", "A16", "A17", "A18 "}; // line bchar BB [17] [5] = {"B1", "B2", "B3", "B4", "B5", "T1 ", "B6", "B7", "B8", "B9", "B10", "T2", "B11", "B12", "B13", "B14 ", "B15"}; int COUNT = 0; int I; int tmp1, tmp2; int poss, pose; // The start and end points are all transfer station if (start [0] = 'T' & End [0] = = 'T') Count = 6; // the start and end points are all on line A. Else if (start [0]! = 'B' & End [0]! = 'B') {for (I = 0; I <20; I ++) {If (strcmp (START, AA [I]) = 0) poss = I; if (strcmp (end, AA [I]) = 0) pose = I;} COUNT = 1 + (poss-pose)> 0 )? (Poss-pose): (pose-poss);} // the start and end points are all on line B else if (start [0]! = 'A' & End [0]! = 'A') {for (I = 0; I <17; I ++) {If (strcmp (START, BB [I]) = 0) poss = I; if (strcmp (end, BB [I]) = 0) pose = I;} COUNT = 1 + (poss-pose)> 0 )? (Poss-pose): (pose-poss);} // the start and end points are respectively on two lines. else {// the start point is on line, end Point on line B if (start [0] = 'A') {for (I = 0; I <20; I ++) if (strcmp (start, AA [I]) = 0) {poss = I; break;} for (I = 0; I <17; I ++) if (strcmp (end, bb [I]) = 0) {pose = I; break ;}// start point on line B, end point on line A else {for (I = 0; I <20; I ++) if (strcmp (end, AA [I]) = 0) {pose = I; break;} for (I = 0; I <17; I ++) if (strcmp (START, BB [I]) = 0) {poss = I; brea K ;}} tmp1 = A1 [poss] + B1 [pose]; tmp2 = a2 [poss] + B2 [pose]; Count = (tmp1> tmp2 )? Tmp2: tmp1)-1;} return count;} int main (void) {char start [10], end [10]; int count; scanf ("% s ", start); scanf ("% s", end); Count = jishi3 (START, end); printf ("% d \ n", count); Return 0 ;}


Huawei trial -- subway Transfer

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.