[SCOI2005]骑士精神

题目

BZOJ

做法

爆搜是肯定的,考虑剪枝
\(g(n)+h(n)=f(n)\)\(h\)为不同点个数

My complete code

#include<bits/stdc++.h>
using namespace std;
typedef int LL;
const LL dx[]={0,1,1,-1,-1,2,2,-2,-2};
const LL dy[]={0,2,-2,2,-2,1,-1,1,-1};
const LL goal[6][6]={
    {0,0,0,0,0,0},
    {0,1,1,1,1,1},
    {0,0,1,1,1,1},
    {0,0,0,2,1,1},
    {0,0,0,0,0,1},
    {0,0,0,0,0,0}
};
LL n,m,a[7][7],ok;
char ch;
inline LL Check(){
    LL ret=0;
    for(LL i=1;i<=5;i++)
        for(LL j=1;j<=5;j++)
            ret+=(a[i][j]!=goal[i][j]);
    return ret;
}
inline bool Be(LL x,LL y){
    if(x<1||x>5||y<1||y>5) return false;
    return true;
}
inline void A_star(LL dep,LL x,LL y,LL Md){
    if(dep==Md){
        if(!Check())
            ok=true;
        return;
    }
    for(LL i=1;i<=8;i++){
        LL xx(x+dx[i]),yy(y+dy[i]);
        if(!Be(xx,yy)) continue;
        swap(a[x][y],a[xx][yy]);
        LL ret=Check();
        if(ret+dep<=Md)
            A_star(dep+1,xx,yy,Md);
        if(ok) return;
        swap(a[x][y],a[xx][yy]);
    }
}
int main(){
    LL T;
    cin>>T;
    while(T--){
        LL sx(0),sy(0);
        ok=false;
        for(LL i=1;i<=5;i++){
            for(LL j=1;j<=5;j++){
                char ch; scanf(" %c",&ch);
                if(ch=='*')
                    a[i][j]=2,sx=i,sy=j;
                else 
                    a[i][j]=ch-'0';
            }
        }
        if(!Check()){
            puts(0);
            continue;
        }
        bool f(false);
        for(LL d=1;d<=15;++d){
            A_star(0,sx,sy,d);
            if(ok){
                cout<<d<<endl;
                f=true;
                break;
            }
        }
        if(!f)
            cout<<-1<<endl;
    }return 0;
}

转载于:https://www.cnblogs.com/y2823774827y/p/10468229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值