标签:

6 12
3
#include <iostream>
#include <cmath>
using namespace std;
int x, y;
void getLocal(int s, int& rx, int& ry)
{
rx = (int)(ceil(sqrt(s)));
int left = rx*rx - 2*rx + 2;
ry = s - left + 1;
}
int getShortLen()
{
int ans = 0;
int rx, cx, ry, cy;
getLocal(x, rx, cx);
getLocal(y, ry, cy);
if(rx == ry)
{
return abs(cx - cy);
}
ans += 2 * (ry - rx -1) + 1; //求出行跳转的数量
int left = cx + 1;
int right = cx + 2 * (ry-rx) - 1;
if(!(cx%2)) //偶数比奇数列多1
{
ans++;
left--;
right++;
}
if(cy >= left && cy <= right)
{
if(cy%2)
{
return ans+1;
}
else
{
return ans;
}
}
else
{
ans += min(abs(cy-left), abs(cy-right));
return ans;
}
}
int main(void)
{
int temp;
while(cin >> x >> y)
{
int ans = 0;
if(x > y)
{
temp = x;
x = y;
y = temp;
}
ans = getShortLen();
cout << ans << endl;
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/q_l_s/article/details/45478989