AcWing 1219. 移动距离
原题链接
简单
作者:
hegehog
,
2020-07-09 18:15:17
,
所有人可见
,
阅读 653
C++代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int w, m, n;
int a[10005][10005];
int main()
{
cin >> w >> m >> n;
int index = 1;
for(int i = 0; index <= max(m, n); i ++)
{
if(i % 2 == 0)
for(int j = 0; j < w; j ++)
a[i][j] = index ++;
else
for(int j = w - 1; j > -1; j --)
a[i][j] = index ++;
}
int x1 = m / w, y1, x2 = n / w, y2;
if(m % w == 0) x1 -= 1;
if(n % w == 0) x2 -= 1;
for(int j = 0; j < w; j ++)
if(a[x1][j] == m) y1 = j;
for(int j = 0; j < w; j ++)
if(a[x2][j] == n) y2 = j;
cout << abs(x2 - x1) + abs(y2 - y1) << endl;
return 0;
}
//暴力模拟:两点间最短距离——只向下和向右走