解题思路: 参考 Deeemo
1.第一步定位给定的坐标所在的层数:round = min(xPos, num - 1 - xPos, yPos, nums - 1 - yPos)
2.根据图示以及规律 ,计算在该层数之外的路径长度。
3 .令 xPos -= round,yPos -= round,将坐标平移到较小的矩形的边上。
4.对于target 在新的矩阵中是哪一条边 ,进行具体判断 ,然后计算返回。 (具体看代码 , 对应着图示看更好理解,自己假定不同的 target)
int orchestraLayout(int num, int xPos, int yPos)
{
int round = min( min(xPos, num - 1 - xPos), min(yPos, num - 1 - yPos) );
int newNum = num - round * 2; //新的行数
//获取新的开始值
int start = (int) (((long long) num * num - (long long) newNum*newNum) % 9 + 1);
xPos = xPos - round; //新的位置
yPos = yPos - round;
//-1 , +1 是为了保证数据在1 - 9之间
if (xPos == 0) //最上面一行
return (start + yPos - 1) % 9 + 1;
else if (yPos == newNum - 1) //最右面一列
return (start + newNum - 1 + xPos - 1) % 9 + 1;
else if (xPos == newNum - 1) //最下面一行
return (start + (long long)(newNum - 1) * 3 - yPos - 1) % 9 + 1 ;
else //最左面一列
return (start + (long long)( newNum - 1 ) * 4 - xPos - 1) % 9 + 1;