HDU 6171 Admiral(双向宽搜)

不过好像可以不用pair<ll,ll>

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
map< pair<ll,ll> ,int>mp1,mp2;
int mp[10][10];
struct zb{
    int x,y;
    zb(){}zb(int _x,int _y){
        x=_x;y=_y;
    }
}mm[25];
struct node{
    int a[22];
    zb o;
    pair<ll,ll> hh;
     pair<ll,ll> vl(){
        ll x1=0,x2=0;
        for(int i=1;i<=10;i++)x1=x1*10+a[i];
        for(int i=11;i<=21;i++)x2=x2*10+a[i];
        return make_pair(x1,x2);
    }
};
int mx[]={-1,-1,1,1};
int my[]={0,-1,0,1};
bool isok(int x,int y){
    return x>=y&&y<=6&&y>=1;
}
node q1[111111],q2[111111];
int main()
{
    int tmp=1;
    for(int i=1;i<=6;i++)
    for(int j=1;j<=i;j++){
        mp[i][j]=tmp++;
        mm[mp[i][j]]=zb(i,j);
    }
    node ed;
    for(int i=1;i<=6;i++){
        for(int j=1;j<=i;j++){
            ed.a[mp[i][j]]=i-1;
        }
    }
    ed.o=zb(1,1);
    ed.hh=ed.vl();
    int t;
    scanf("%d",&t);
    while(t--){
        int l1,l2,r1,r2;
        l1=l2=r1=r2=0;
        mp1.clear();mp2.clear();
        node st;zb oo;
        for(int i=1;i<=21;i++){
            scanf("%d",&st.a[i]);
            if(st.a[i]==0){
                st.o=mm[i];
            }
            
        }
        st.hh=st.vl();
        if(st.hh==ed.hh)printf("0\n");
        else {        
            q1[r1++]=st;
            q2[r2++]=ed;
            mp1[st.hh]=1;
            mp2[ed.hh]=1;
            int ans,step=0,f=0;
            for(int ca=1;ca<=10;ca++){
                step++;
                int l=l1,r=r1;
                while(l!=r&&f==0){
                    node now=q1[l++],tmp;
                    for(int i=0;i<4;i++){
                        tmp=now;
                        tmp.o.x+=mx[i];
                        tmp.o.y+=my[i];
                        if(isok(tmp.o.x,tmp.o.y)){
                        //    printf("%d %d\n",tmp.o.x,tmp.o.y);
                            swap(tmp.a[mp[tmp.o.x][tmp.o.y]],tmp.a[mp[now.o.x][now.o.y]]);
                            tmp.hh=tmp.vl();
                            if(mp1.find(tmp.hh)==mp1.end()){
                                if(mp2.find(tmp.hh)!=mp2.end()){
                                    f=1;
                                    ans=step;
                                }
                                else {
                                    mp1[tmp.hh]=1;
                                    q1[r1++]=tmp;
                                }
                            }
                        }
                        if(f)break;
                    }
                }
                l1=l;
                if(f)break;
                step++;
                l=l2,r=r2;
                while(l!=r&&f==0){
                    node now=q2[l++],tmp;
                    for(int i=0;i<4;i++){
                        tmp=now;
                        tmp.o.x+=mx[i];
                        tmp.o.y+=my[i];
                        if(isok(tmp.o.x,tmp.o.y)){
                            swap(tmp.a[mp[tmp.o.x][tmp.o.y]],tmp.a[mp[now.o.x][now.o.y]]);
                            tmp.hh=tmp.vl();
                            if(mp2.find(tmp.hh)==mp2.end()){
                                if(mp1.find(tmp.hh)!=mp1.end()){
                                    f=1;
                                    ans=step;
                                }
                                else {
                                    mp2[tmp.hh]=1;
                                    q2[r2++]=tmp;
                                }
                            }
                        }
                        if(f)break;
                    }
                }
                l2=l;
            }        
            if(f)printf("%d\n",ans);
            else printf("too difficult\n");    
        }        
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值