BZOJ1193 [HNOI2006]马步距离(洛谷P2060)

292 篇文章 1 订阅
281 篇文章 1 订阅

BFS 贪心

BZOJ题目传送门
洛谷题目传送门

先贪心地尽量接近,然后BFS。

代码(有哪位dalao能看看为什么我答案+1才对):

#include<cstdio>
#include<cstring>
#include<algorithm>
#define abs(x) ((x)>0?(x):-(x))
using namespace std;
const int t1[8]={1,1,-1,-1,2,2,-2,-2};
const int t2[8]={2,-2,2,-2,1,-1,1,-1};
int xp,yp,xs,ys,q[100005][2],d[205][205];
inline int hhh(){
    int ans=0;
    for(;xs+ys>50;ans+=2){
        if (xs<ys) swap(xs,ys);
        xs-=4,ys-=2*(xs-4<ys*2);
    }
    return ans;
}
inline int bfs(){
    memset(d,-1,sizeof(d));
    int l=0,r=1; xs+=70,ys+=70;
    q[1][0]=q[1][1]=70,d[50][50]=0;
    while (l<=r){
        int s=q[++l][0],t=q[l][1];
        if (s==xs&&t==ys) return d[s][t];
        for (int i=0;i<8;i++){
            int x=s+t1[i],y=t+t2[i];
            if (x>=0&&x<205&&y>=0&&y<205&&d[x][y]==-1)
                d[x][y]=d[s][t]+1,q[++r][0]=x,q[r][1]=y;
        }
    }
}
int main(){
    scanf("%d%d%d%d",&xp,&yp,&xs,&ys);
    xs=abs(xs-xp),ys=abs(ys-yp);
    return printf("%d\n",hhh()+bfs()+1),0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值