蜂窝小区最短距离

描述:
如图所示,蜂窝小区,以1为中心,顺时针编号,编号最大限定为100000。
求任意两编号之间的最短距离。
两个相邻小区的距离为1




示例:19到30的最短距离为5


实现如下三个接口:
/************************************************************************
Description  : 初始化蜂窝小区信息
Prototype    : void InitCellularDistrict(int iMaxSeqValue)
               Input Param  : iMaxSeqValue 蜂窝小区的最大值编号,注:编号从1开始
               Output Param : 无
               Return Value : 成功返回0,失败返回-1
/************************************************************************/
int InitCellularDistrict(int iMaxSeqValue)
{
    return -1;
}


/************************************************************************
Description  : 计算出蜂窝小区指定两点(编号值)之间的最短距离
Prototype    : int GetShortestPathLength(int iFirstValue, int iSecondValue)
               Input Param  : iFirstValue 起点编号值, iSecondValue 终点编号值
               Output Param : 无
               Return Value : 计算成功返回最短距离,失败返回-1
/************************************************************************/
int GetShortestPathLength(int iFirstValue, int iSecondValue)
{
    return -1;
}


/************************************************************************
Description  : 清空相关信息
Prototype    : void Clear()
               Input Param  : 无
               Output Param : 无
               Return Value : 无
/************************************************************************/
void Clear()
{


}


使用坐标系求解,思路在博客点击打开链接中有详细介绍。


#include "CellularDistrict.h"
#include <cmath>
int g_maxvalue = 0;   //记录最大编号


//获取蜂窝指定编号的坐标
void GetCoordinate(int seq_num,int *x,int *y,int *z)
{
	int cycle_num = 0;   //表示第几个圈
	int current_seq_num =1;  //第一个编号

	//计算第几个圈
	while(current_seq_num < seq_num)
	{
		cycle_num++;
		current_seq_num = current_seq_num + cycle_num * 6; 
	}

	if(cycle_num == 0)
	{
		*x = 0;
		*y = 0;
		*z = 0;
		return ;
	}

	//计算是第几条边(编号从 1~6)
	int side_num = (current_seq_num - seq_num) / cycle_num + 1;

	//计算是在边上的位置
	int side_pos = (current_seq_num - seq_num) % cycle_num;
	
	//获取坐标

	switch(side_num)
	{
	case 1:
		*x = cycle_num;
		*y = -cycle_num + side_pos;
		*z = side_pos;
		break;
	case 2:
		*x = cycle_num - side_pos;
		*y = side_pos;
		*z = cycle_num;
		break;
	case 3:
		*x = -side_pos;
		*y = cycle_num; 
		*z = cycle_num - side_pos;
		break;
	case 4:
		*x = -cycle_num;
		*y = cycle_num - side_pos;
		*z = - side_pos;
		break;
	case 5:
		*x = -cycle_num + side_pos;
		*y = -side_pos;
		*z = -cycle_num;
		break;
	case 6:
		*x = side_pos;
		*y = -cycle_num;
		*z = -cycle_num + side_pos;
		break;
	}
}

/************************************************************************
Description  : 初始化蜂窝小区信息
Prototype    : void InitCellularDistrict(int iMaxSeqValue)
               Input Param  : iMaxSeqValue 蜂窝小区的最大值编号,注:编号从1开始
               Output Param : 无
               Return Value : 成功返回0,失败返回-1
/************************************************************************/
int InitCellularDistrict(int iMaxSeqValue)
{
	if(iMaxSeqValue < 1 || iMaxSeqValue > 100000)
	{
		return -1;
	}

	g_maxvalue = iMaxSeqValue;
    return 0;
}

/************************************************************************
Description  : 计算出蜂窝小区指定两点(编号值)之间的最短距离
Prototype    : int GetShortestPathLength(int iFirstValue, int iSecondValue)
               Input Param  : iFirstValue 起点编号值, iSecondValue 终点编号值
               Output Param : 无
               Return Value : 计算成功返回最短距离,失败返回-1
/************************************************************************/
int GetShortestPathLength(int iFirstValue, int iSecondValue)
{
	if(iFirstValue > g_maxvalue || iFirstValue < 1 || iSecondValue > g_maxvalue || iSecondValue < 1)
	{
		return -1;
	}

	int x_1 = 0;
	int y_1 = 0;
	int z_1 = 0;
	int x_2 = 0;
	int y_2 = 0;
	int z_2 = 0;
	GetCoordinate(iFirstValue,&x_1,&y_1,&z_1);  //获取坐标
	GetCoordinate(iSecondValue,&x_2,&y_2,&z_2);

	int distance_x = (x_1 > x_2)? (x_1 - x_2):(x_2 - x_1);
	int distance_y = (y_1 > y_2)? (y_1 - y_2):(y_2 - y_1);
	int distance_z = (z_1 > z_2)? (z_1 - z_2):(z_2 - z_1);

	int shortest_distance = distance_x > distance_y? distance_x:distance_y;
	shortest_distance = shortest_distance > distance_z? shortest_distance:distance_z;

	return shortest_distance;
}

/************************************************************************
Description  : 清空相关信息
Prototype    : void Clear()
               Input Param  : 无
               Output Param : 无
               Return Value : 无
/************************************************************************/
void Clear()
{
	g_maxvalue = 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值