BZOJ 1085([SCOI2005]骑士精神-A*)

在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位。在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵坐标相差为1的格子)移动到空位上。给定一个初始的棋盘,怎样才能经过移动变成如下目标棋盘: 如果能在15步以内(包括15步)到达目标状态,则输出最小步数,否则输出-1。
BZOJ 1085([SCOI2005]骑士精神-A*)_第1张图片

用到了A*

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case %d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int 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;
}
typedef bool Board[5][5];
bool ans[5][5]={{1,1,1,1,1},
{0,1,1,1,1},
{0,0,0,1,1},
{0,0,0,0,1},
{0,0,0,0,0}};
bool judge(Board a,int x,int y) {
    if (x!=2||y!=2) return 0;
    Rep(i,5) Rep(j,5){
        if (a[i][j]!=ans[i][j]) return 0;
    }
    return 1;
}
int k;
bool eva(Board a,int l) {
    Rep(i,5) Rep(j,5) {
        if (a[i][j]^ans[i][j]) {
            if (++l>1+k) return 0;
        }
    }
    return 1;
}
int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; 
bool dfs(Board a,int x,int y,int l) {
    if (l==k) {
        if (judge(a,x,y)) return 1; 
        return 0;
    }
    if (!eva(a,l)) return 0;
    Rep(i,8) {
        int nowx = x+dir[i][0], nowy = y+dir[i][1];
        if (nowx>4||nowx<0||nowy>4||nowy<0) continue;
        swap(a[x][y],a[nowx][nowy]); 
        if (dfs(a,nowx,nowy,l+1)) return 1;
        swap(a[x][y],a[nowx][nowy]); 
    }
    return 0;
}
int main()
{
    freopen("bzoj1085.in","r",stdin);
// freopen(".out","w",stdout);

    int T=read();
    while(T--) {
        Board a;
        int x,y;
        Rep(i,5) {
            char s[10];
            cin>>s;
            Rep(j,5) {
                if (s[j]=='*') {
                    x=i,y=j,a[i][j]=0;
                } else a[i][j]=s[j]-'0';
            }
        } 
        for(k=1;k<=15;++k)
            if (dfs(a,x,y,0))  {
                printf("%d\n",k);break;     
            }
        if (k==16) puts("-1");

    }
    return 0;
}

你可能感兴趣的:(BZOJ 1085([SCOI2005]骑士精神-A*))