AcWing 1219. 移动距离
原题链接
简单
作者:
冷丁Hacker
,
2020-09-15 09:33:38
,
所有人可见
,
阅读 465
一、Y总先让下标减一,后面求坐标就很简单
#include<bits/stdc++.h>
using namespace std;
int main()
{
int w,m,n;
int x1,y1,x2,y2;
cin>>w>>n>>m;
m--;n--;
x1=n/w;
x2=m/w;
y1=n%w;
y2=m%w;
if(x1%2)
y1=w-1-y1;
if(x2%2)
y2=w-1-y2;
int ans=abs(x1-x2)+abs(y1-y2);
cout<<ans<<endl;
return 0;
}
我没有将下标减一 硬找规律 很难写
#include<bits/stdc++.h>
using namespace std;
int w, a, b;
int main()
{
int row1, row2, line1, line2;
cin >> w >> a >> b;
int num = a % w;
if (a % w == 0) {
line1 = a / w;
if ((a / w) % 2 == 0) {
row1 = num + 1;
}
else {
row1 = w - num;
}
}
else {
line1 = a / w + 1;
if ((a / w) % 2 == 0)
row1 = num;
else row1 = w - num + 1;
}
int num1 = b % w;
if (b % w == 0) {
line2 = b / w;
if ((b / w) % 2 == 0) {
row2 = num1 + 1;
}
else {
row2 = w - num1;
}
}
else {
line2 = b / w + 1;
if ((b / w) % 2 == 0)
row2 = num1;
else row2 = w - num1 + 1;
}
int ans = abs(row1 - row2) + abs(line1 - line2);
cout << ans << endl;
return 0;
}