描述:
如图所示,蜂窝小区,以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()
{
如图所示,蜂窝小区,以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;
}