AcWing 1219. 移动距离
考察:模拟
没什么难度,主要是记录一下从0开始和从1开始计算行号与列号的区别
从0开始: 行 = n/w,列 = n%w
从1开始: 行 = (n-1)/w,列 = (n-1)%w+1
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 int w,n,m,ans,res; 6 int main() 7 { 8 scanf("%d%d%d",&w,&n,&m); 9 int r1 = (n-1)/w,c1 = (n-1)%w+1; 10 int r2 = (m-1)/w,c2 = (m-1)%w+1; 11 if(r1%2!=0) c1 = w+1-c1; 12 if(r2%2!=0) c2 = w+1-c2; 13 ans = abs(r2 - r1)+abs(c2-c1); 14 printf("%d\n",ans); 15 return 0; 16 }