移动距离描述:
找规律:
- 将所有元素全部减去1。所得矩阵列数=a%w,所得行数=a/w;
x1 = n/w;
x2 = m/w;
y1 = n%w;
y2 = m%w;
- 但是注意行数若为奇数,则注意列数反过来
if(x1%2==1) y1=w-y1-1;
if(x2%2==1) y2=w-y2-1;
完整代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int w,n,m;
int main(){
cin>>w>>n>>m;
m--;n--;// 对应存下来
int x1,x2;
int y1,y2;
x1 = n/w;
x2 = m/w;
y1 = n%w;
y2 = m%w;
if(x1%2==1) y1=w-y1-1;
if(x2%2==1) y2=w-y2-1;
cout<<abs(y1-y2)+abs(x1-x2)<<endl;
}