题目描述
X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3...
当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为6时,开始情形如下:
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 .....
我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动)
输入为3个整数w m n,空格分开,都在1到10000范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n 两楼间最短移动距离。
样例输入
6 8 2
样例输出
4
思路:可以去推公式求解,我写的是暴力模拟,很难受,开大了数组,运行错误1分也没得到,也给自己个警醒吧
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int a[10005][105];
int b[2][2];
int main()
{
int s=1;
int w,m,n;
cin>>w>>m>>n;
int ss=0;
for(int t=1;t<=10000;t++)
{
if(t%2==1)
{
for(int j=1;j<=w;j++)
{
a[t][j]=s;
s++;
if(a[t][j]==m||a[t][j]==n)
{
b[ss][0]=t;
b[ss][1]=j;
ss++;
}
}
}
else
{
for(int j=w;j>=1;j--)
{
a[t][j]=s;
s++;
if(a[t][j]==m||a[t][j]==n)
{
b[ss][0]=t;
b[ss][1]=j;
ss++;
}
}
}
if(ss==2)
{
break;
}
}
printf("%d",abs(b[0][0]-b[1][0])+abs(b[0][1]-b[1][1]));
return 0;
}