You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
860 B

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int x = 20, w;
int m, n;
int main() {
cin >> w >> m >> n;
//构建蛇形矩阵
for (int i = 0; i < x * w; i++) {
int r, c;
r = i / w;
c = i % w;
if (r % 2) a[r][c] = (r + 1) * w - c;
else a[r][c] = i + 1;
}
/*
//输出构建结果
for (int i = 0; i < x; i++) {
for (int j = 0; j < w; j++)
printf("%3d ", a[i][j]);
printf("\n");
}*/
//找出m和n的行列值
int r1, c1, r2, c2;
for (int i = 0; i < x; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == m) r1 = i, c1 = j;
if (a[i][j] == n) r2 = i, c2 = j;
}
}
//计算距离
printf("%d", abs(r2 - r1 + c2 - c1));
return 0;
}