您的位置:首页 > 其它

BZOJ1193: [HNOI2006]马步距离

2018-01-31 21:24 453 查看
Description

在国际象棋和中国象棋中,马的移动规则相同,都是走“日”字,我们将这种移动方式称为马步移动。如图所示,

从标号为 0 的点出发,可以经过一步马步移动达到标号为 1 的点,经过两步马步移动达到标号为 2 的点。任给

平面上的两点 p 和 s ,它们的坐标分别为 (xp,yp) 和 (xs,ys) ,其中,xp,yp,xs,ys 均为整数。从 (xp,yp)

出发经过一步马步移动可以达到 (xp+1,yp+2)、(xp+2,yp+1)、(xp+1,yp-2)、(xp+2,yp-1)、(xp-1,yp+2)、(xp-2,

yp+1)、(xp-1,yp-2)、(xp-2,yp-1)。假设棋盘充分大,并且坐标可以为负数。现在请你求出从点 p 到点 s 至少

需要经过多少次马步移动?



Input

只包含4个整数,它们彼此用空格隔开,分别为xp,yp,xs,ys。并且它们的都小于10000000。

Output

含一个整数,表示从点p到点s至少需要经过的马步移动次数。

Sample Input

1 2 7 9

Sample Output

5

题目传送门

生病在家颓,他们今天做比赛,很多人做了这道题,过来看看

第一反应:A*加上SPFA,不过一想,细思极恐,他们都会A*??这么牛??不对不对肯定没这么难,于是再想

找规律吧,列了个20*20的暴力,结果无用

啊?我现在这么菜的吗??

不对,一定有别的办法

首先他的坐标太大,得缩

那就缩吧!具体看代码

话说我们班人实力挺强的说

代码如下:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int
4000
dx[9]={0,1,2,1,2,-1,-2,-1,-2};
int dy[9]={0,2,1,-2,-1,2,1,-2,-1};
struct node
{
int x,y,d;
node()
{
d=0;
}
}list[1100];
bool v[21][21];
int main()
{
int sx,sy,ex,ey;
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
int x=abs(sx-ex),y=abs(sy-ey);
int ans=0;
while(x>10||y>10)
{
if(x<y) swap(x,y);
if(x-4>y*2) x-=4;
else x-=4,y-=2;
ans+=2;
}
int head,tail;
list[1].x=x;list[1].y=y;
head=tail=1;
memset(v,false,sizeof(v));
v[x][y]=true;
while(head<=tail)
{
node tno=list[head];
for(int i=1;i<=8;i++)
{
int tx=tno.x+dx[i],ty=tno.y+dy[i];
if(tx>20||ty>20) continue;
if(tx==0&&ty==0)
{
ans+=tno.d+1;
printf("%d\n",ans);
return 0;
}
if(v[tx][ty]==false)
{
v[tx][ty]=true;
list[++tail].x=tx;
list[tail].y=ty;
list[tail].d=tno.d+1;
}
}
head++;
}
return 0;
}


by_lmy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: