地铁换乘

//此题处理不是很好!有待改进!


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <limits>
#include <cassert>
using namespace std;

string route_lineBT1 = "ABCDEz";//0-5 z-"T1"
string route_lineBT2 = "ZKLMNO";//0-5 Z-"T2"
//string route_circle1 = "T1A10A11A12A13T2A14A15A16A17A18A1A2A3A4A5A6A7A8A9";
//string route_circle2 = "T1B6B7B8B9B10T2A14A15A16A17A18A1A2A3A4A5A6A7A8A9";
//string route_circle3 = "T1A10A11A12A13T2B10B9B8B7B6";
string route_circle1 = "zjklmZnopqrabcdefghi";//大环
string route_circle2 = "zFGHIJZnopqrabcdefghi";//下小环
string route_circle3 = "zjklmZJIHGF";//上环
//进行转换方便字符串处理
string str_A = "A1A2A3A4A5A6A7A8A9A10A11A12A13A14A15A16A17A18";
string str_B = "B1B2B3B4B5B6B7B8B9B10B11B12B13B14B15";
string str_a = "abcdefghijklmnopkr";
string str_b = "ABCDEFGHIJKLMNO";
//B1 - B5 T1 // 1
//T2 B11 -B15 //7
//other 3    //使俩者之后不大于第三者

/*
 此题代码未做过多放错处理,也没怎么优化。
 字符间转换主要是之前未注意到除了有A1 还有像 A10这种,为了弥补错误,加上之前写了不少代码,就直接转化成ABCD此种区分。(迫不得已)

  主要此题思路:
  此题分为5种情况。
  主要先拆出有:俩头俩段,加之中间3个圆。还有那个含最短路径的圆可以考虑接近重合可能,但此题特殊没必要考虑!

 此题需注意的一点是,分段计算时,中间公共的点会重复计算。所以的去重!	
*/
int translate(string str);
int line_search(string source,string str1,string str2);
int circle_search(string source,string str1,string str2);
int min_circle_num(string str1,string str2);
string map_str(string str_get1);
void main_fcn();
int main()
{
	string str;
	do{

	main_fcn();
	cout<<"y?n :";
	cin>>str;
	cout<<str<<endl;
	}while(str != "n");
	return 1;
}

void main_fcn()
{
	int num1,num2;
	int res(0);

	string str_get1,str_get2;
	string str_first,str_second;
	cin>>str_get1>>str_get2;

	str_first =	map_str(str_get1);
	str_second = map_str(str_get2);
	assert(!str_first.empty());//若转化出错即报错!
	assert(!str_second.empty());
	num1 = translate(str_first);
	num2 = translate(str_second);
	if(num1 > num2)//交换,有序
	{
		string temp;
		temp = str_first;
		str_first = str_second;
		str_second = temp;
	}

 
	switch (num1+num2)
	{
		
		case 2: //1 1
			
			res = line_search(route_lineBT1,str_first,str_second);
			
			break;
		case 4: //1 3
			res += line_search(route_lineBT1,str_first,string("z"));
			res += min_circle_num(str_second,"z")-1;//主要t1算俩次
			
			break;

		case 8: //1 7
		   	res += line_search(route_lineBT1,str_first,string("z")) -1;//主要t1,t2算俩次
			res += min_circle_num("z","Z");
			res += line_search(route_lineBT2,"Z",str_second)-1;
			break;
			
		case 6: //3 3
			res = min_circle_num(str_first,str_second);
			break;

		case 10://3 7 
			res += min_circle_num("Z",str_first);
			res += line_search(route_lineBT2,"Z",str_second)-1;//主要t1算俩次
			break;

		case 14: //7 7
		    res = line_search(route_lineBT2,str_first,str_second);
			break;
		default:
			res = 0;

	}

	cout <<"res:"<<res<<endl;
}

string map_str(string str_get1)//将A10 B14 映射为ABCD等
{
	string str_first("");
	int pos ;
	if(str_get1 == "T1") return "z";
	if(str_get1 == "T2") return "Z";
	if((pos = str_A.find(str_get1)) != -1)
	{
		if(pos == 0)
		str_first =str_a[0];
		else if(pos <= 16&&pos>0)
		str_first = str_a[pos/2];
		else
		str_first = str_a[9+(pos-18)/3];

		return str_first;
	}

	if((pos = str_B.find(str_get1)) != -1)
	{
		if(pos == 0)
		str_first =str_b[0];
		else if(pos <= 16&&pos>0)
		str_first = str_b[pos/2];
		else
		str_first = str_b[9+(pos-18)/3];

		return str_first;
	}

return str_first;
}

int translate(string str)//计算是属于哪个段
{
	//转换看是哪个区间
	if (route_lineBT1.find(str) != -1)
	 return	1;
	
	if(route_lineBT2.find(str) != -1)
	return	7;
	
	return 3;
}

int line_search(string source,string str1,string str2)//线性间距离
{

	return (abs(source.find(str1) - source.find(str2)))+1;
}

int circle_search(string source,string str1,string str2)//环状距离
{
	int num ,len;
	int pos1,pos2;
	len = source.length();
	if((pos1 = source.find(str1)) == -1)return INT_MAX;
	if((pos2 = source.find(str2)) == -1 )return INT_MAX;

	num = abs(pos1-pos2)+1;//线性长度

	return num < (len-num+2) ? num:(len-num+2);//自己实现最小化
}

int min_circle_num(string str1,string str2)//取最小的
{
	int min_val[3];

	min_val[0] = circle_search(route_circle1,str1,str2);
	min_val[1] = circle_search(route_circle2,str1,str2);
	min_val[2] = circle_search(route_circle3,str1,str2);
 
	for(int i = 1;i < 3;i++)
	{
		min_val[0] = min_val[0] < min_val[i]?min_val[0]:min_val[i];
	}

	return min_val[0];
}



很似纠结,提交了4次,结果abs,   不管是用

//#include <cmath> //竟然用不了abs
//#include <stdlib.h>

都不行,无赖之下重写了一个。!!!!!!

unsigned int abs_my(int num)
{
	return num > 0 ? num: - num;
}


不容易啊!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值