地铁换乘

这里写图片描述

【】【】【】【】只编写了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;
}






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值