1219. 移动距离

在这里插入图片描述
思路: 利用坐标轴求出每个坐标的行数和列数,然后得出结果
代码

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    
    
    int w, m, n;
    cin >> w >> m >> n;
    m --, n -- ;

    int x1 = m / w, x2 = n / w;//求行数
    int y1 = m % w, y2 = n % w;
    //求列数
    if (x1 % 2) y1 = w - 1 - y1;
    if (x2 % 2) y2 = w - 1 - y2;

    cout << abs(x1 - x2) + abs(y1 - y2) << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45812180/article/details/114549628