Files
python/TangDou/LanQiaoBei/LanQiao12XuanBaSaiZhongJi/4.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
1009 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int w, m, n;
/*
测试用例:
6 8 2
答案:4
*/
//计算点的行号
int row(int i) {
if (i % w == 0) //值是宽度的倍数
return i / w;
else //值不是宽度的倍数
return i / w + 1;
}
//计算点的列号
int col(int i) {
if (row(i) % 2 == 0) { //该值在偶数行
if (i % w == 0) // 找规律可知,偶数行能被整除肯定在第一列
return 1;
//在偶数行(从大到小)但不能被整除,在倒数第j%w列,即正数第w-j%w+1列
return w - i % w + 1;
} else { //在奇数行
if (i % w == 0) return w; //找规律可知,偶数行能被整除肯定在最后一列
return i % w; //在奇数行(从小到大)但不能被整除,在第j%w列
}
}
int main() {
cin >> w >> m >> n;
int s = abs(row(n) - row(m)) + abs(col(n) - col(m));
printf("%d\n", s);
return 0;
}