AtCoder 184 C - Super Ryuma
https://atcoder.jp/contests/abc184/tasks/abc184_c
典型的分类讨论
①1或0步到
直接特判。
②2步到
与终点距离不大于3的点存在数组A,与起点距离不超过3的点存在数组B。
(1)横坐标值差与纵坐标之差的奇偶性相同;(判断斜线的可能性)
(2)对于A中的一个点可以一步走到终点;
(3)对于B中的一个点可以一步走到起点;
(4)A与B有共同区域。
③把棋盘按照国际象棋的方式黑白染色,容易发现两次斜向移动可以使得当前位置和结束位置的距离不超过1,那么3,步理论上可以完成所有移动了。(不要问我why)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a,b,c,d;
int cnt1,cnt2;
struct node
{
int x;
int y;
}A[1005],B[1005];
inline read()
{
int x=0,f=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch='-')
f=-1;
ch=getchar();
}
while(isdigit(ch))
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline bool dis(int x,int y,int xx,int yy)
{
if(abs(x-xx)+abs(y-yy)<=3)
return true;
else return false;
}
int main()
{
a=read(),b=read(),c=read(),d=read();
if(a==c&&b==d) cout<<"0";
else if(a+b==c+d) cout<<"1";
else if(a-b==c-d) cout<<"1";
else if(dis(a,b,c,d)) cout<<"1";
else
{
for(int i=a-3;i<=a+3;i++)
for(int j=b-3;j<=b+3;j++)
if(dis(i,j,a,b))
{
cnt1++;
A[cnt1].x=i;
A[cnt1].y=j;
}
for(int i=c-3;i<=c+3;i++)
for(int j=d-3;j<=d+3;j++)
if(dis(i,j,c,d))
{
cnt2++;
B[cnt2].x=i;
B[cnt2].y=j;
}
if ((abs(a-c)%2)==(abs(b-d)%2))
{
cout<<"2";
return 0;
}
else
{
for(int i=1;i<=cnt1;i++)
{
int x=A[i].x,y=A[i].y;
if(x-y==c-d||x+y==c+d)
{
cout<<"2";
return 0;
}
}
for(int i=1;i<=cnt2;i++)
{
int x=B[i].x,y=B[i].y;
if(x-y==a-b||x+y==a+b)
{
cout<<"2";
return 0;
}
}
for(int i=1;i<=cnt1;i++)
for(int j=1;j<=cnt2;j++)
if(A[i].x==B[j].x&&A[i].y==B[j].y)
{
cout<<"2";
return 0;
}
}
}
cout<<"3";
return 0;
}