地铁换乘

来源:互联网 发布:python if false 编辑:程序博客网 时间:2024/04/19 18:10

这里写图片描述

【】【】【】【】只编写了AB AA BB的情况,BA和AB类似,就不编写了
ps:AB的意思是第一个字符串在A中,第二个字符串在B中

#include <iostream>#include <algorithm>#include <vector>#include <string>#include <math.h>using namespace std;int main(){    string strA[] = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","T1","A10","A11","A12","A13","T2","A14","A15","A16","A17","A18"};    string strB[] = {"B1","B2","B3","B4","B5","T1","B6","B7","B8","B9","B10","T2","B11","B12","B13","B14","B15"};    vector<string>strVecA(strA,strA+20);    vector<string>strVecB(strB,strB+17);    string str1,str2;    while(cin>>str1>>str2)    {        ///////得到str1,str2在A,B线的位置        vector<string>::iterator it2A,it2B,it1A,it1B,itT1A,itT1B,itT2A,itT2B;        it1A = find(strVecA.begin(),strVecA.end(),str1);        it1B = find(strVecB.begin(),strVecB.end(),str1);        it2A = find(strVecA.begin(),strVecA.end(),str2);        it2B = find(strVecB.begin(),strVecB.end(),str2);        itT1A = find(strVecA.begin(),strVecA.end(),"T1");        itT1B = find(strVecB.begin(),strVecB.end(),"T1");        itT2A = find(strVecA.begin(),strVecA.end(),"T2");        itT2B = find(strVecB.begin(),strVecB.end(),"T2");        ///////////        int pos = 0;        if(it1A != strVecA.end() && it2A != strVecA.end())//////AA        {            pos = abs(it1A - it2A)+1;        }        else if(it1A != strVecA.end() && it2B != strVecB.end())/////AB        {            if(it1A < itT2A && it2B < itT2B)            {                int posT1A = abs(itT1A-it1A);                int posT1B = abs(itT1B-it2B);                int posT1 = posT1A+posT1B;                int posT2A = abs(itT2A-it1A);                int posT2B = abs(itT2B-it2B);                int posT2 = posT2A+posT2B;                pos = ((posT2 > posT1)?(posT1):(posT2)) + 1;            }            else if(it1A < itT2A && it2B >= itT2B)            {                if(it1A > itT1A)                {                    int posT2A = abs(itT2A-it1A);                    int posT2B = abs(itT2B-it2B);                    int posT2 = posT2A+posT2B;                    pos = posT2 + 1;                }                else                {                    int posT1A = abs(itT1A-it1A);                    int posT1B = abs(itT1B-it2B);                    int posT1 = posT1A+posT1B - 1;                    int posT2A = abs(itT2A-it1A);                    int posT2B = abs(itT2B-it2B);                    int posT2 = posT2A+posT2B;                    pos = ((posT2 > posT1)?(posT1):(posT2)) + 1;                }            }            else if(it1A >= itT2A && it2B < itT2B)            {                if(it2B < itT1B)                {                    int posT1A = abs(itT1A-it1A);                    int posT1B = abs(itT1B-it2B);                    int posT1 = posT1A+posT1B;                    int posT2A = abs(itT2A-it1A);                    int posT2B = abs(itT2B-it2B);                    int posT2 = posT2A+posT2B - 1;                    pos = ((posT2 > posT1)?(posT1):(posT2)) + 1;                }                else                {                    int posT1A = abs(itT1A-it1A);                    int posT1B = abs(itT1B-it2B);                    int posT1 = posT1A+posT1B;                    int posT2A = abs(itT2A-it1A);                    int posT2B = abs(itT2B-it2B);                    int posT2 = posT2A+posT2B;                    pos = ((posT2 > posT1)?(posT1):(posT2)) + 1;                }            }            else if(it1A >= itT2A && it2B >= itT2B)            {                //int posT1A = abs(itT1A-it1A);                //int posT1B = abs(itT1B-it2B);                //int posT1 = posT1A+posT1B;                int posT2A = abs(itT2A-it1A);///                int posT2B = abs(itT2B-it2B);///                int posT2 = posT2A+posT2B;                pos = posT2 + 1;            }        }        else if(it1B != strVecB.end() && it2B != strVecB.end())//////BB        {            if((it1B <= itT2B && it1B >= itT1B) || (it2B <= itT2B && it2B >= itT1B))            {                pos = abs(it1B - it2B) + 1;            }            else if((it1B <= itT1B && it2B <= itT1B) || (it1B >= itT2B && it2B >= itT2B))            {                pos = abs(it1B - it2B) + 1;            }            else if((it1B <= itT1B && it2B >= itT2B) || (it2B <= itT1B && it1B >= itT2B))            {                pos = abs(it1B - it2B) + 1 - 1;            }        }        else if(it1B != strVecB.end() && it2A != strVecA.end())/////BA        {            int posT1A = abs(itT1A-it2A);            int posT1B = abs(itT1B-it1B);            int posT1 = posT1A+posT1B;            int posT2A = abs(itT2A-it2A);            int posT2B = abs(itT2B-it1B);            int posT2 = posT2A+posT2B;            pos = ((posT2 > posT1)?(posT1):(posT2)) + 1;        }        cout<<pos<<endl;        str1.clear();        str2.clear();    }    system("pause");    return 0;}
0 0
原创粉丝点击