tjut 3095

#include<cstring>  
#include<string>  
#include<iostream>  
#include<queue>  
#include<cstdio>  
#include<algorithm>  
#include<map>  
#include<cstdlib>  
#include<cmath>  
#include<vector>  
//#pragma comment(linker, "/STACK:1024000000,1024000000");  
  
using namespace std;  
  
#define INF 0x3f3f3f3f  
  
long long d[30];  
int g[5][5];  
  
struct node  
{  
    int g[5][5];  
    int step;  
    int x1,y1;  
    int x2,y2;  
} p1;  
  
map<long long ,int >mp[2];  
queue<node>que;  
  
int dir[][5]= {{0,1},{0,-1},{1,0},{-1,0}};  
  
bool inbound(node k,int x,int y)  
{  
    if(x>=0&&x<5&&y>=0&&y<5)  
    {  
        if(k.g[x][y]==INF||k.g[x][y]==12) return false;  
        else return true;  
        return false;  
    }  
    return false;  
}  
  
long long getval(node k)  
{  
    long long sum=0;  
    for(int i=0; i<5; i++)  
    {  
        for(int j=0; j<5; j++)  
        {  
            if(k.g[i][j]!=INF)  
            {  
                if(k.g[i][j]/10) sum=sum*100+k.g[i][j];  
                else sum=sum*10+k.g[i][j];  
            }  
        }  
    }  
    return sum;  
}  
  
void bfsx(int flag)  
{  
    mp[flag].clear();  
    node k;  
    while(!que.empty()) que.pop();  
    que.push(p1);  
    mp[flag][getval(p1)]=1;  
    while(!que.empty())  
    {  
        k=que.front();  
        que.pop();  
        long long pri=getval(k);  
        if(flag)  
        {  
            if(mp[0][pri])  
            {  
                if(mp[0][pri]+k.step-2<=20)  
                {  
                    printf("%d\n",mp[0][pri]+k.step-2);  
                    return ;  
                }  
                else continue;  
            }  
        }  
        for(int i=0; i<4; i++)  
        {  
            node temp=k;  
            int s=temp.x1+dir[i][0];  
            int t=temp.y1+dir[i][1];  
            if(inbound(temp,s,t))  
            {  
                temp.step++;  
                if(temp.step>12&&!flag) continue;  
                else if(temp.step>10&&flag) continue;  
                swap(temp.g[temp.x1][temp.y1],temp.g[s][t]);  
                long long pri=getval(temp);  
                if(mp[flag][pri]) continue;  
                mp[flag][pri]=temp.step;  
                temp.x1=s,temp.y1=t;  
                que.push(temp);  
            }  
        }  
        for(int i=0; i<4; i++)  
        {  
            node temp=k;  
            int s=temp.x2+dir[i][0];  
            int t=temp.y2+dir[i][1];  
            if(inbound(temp,s,t))  
            {  
                temp.step++;  
                if(temp.step>12&&!flag) continue;  
                else if(temp.step>10&&flag) continue;  
                swap(temp.g[temp.x2][temp.y2],temp.g[s][t]);  
                long long pri=getval(temp);  
                if(mp[flag][pri]) continue;  
                mp[flag][pri]=temp.step;  
                temp.x2=s,temp.y2=t;  
                que.push(temp);  
            }  
        }  
    }  
    if(flag) printf("No solution!\n");  
}  
  
int main()  
{  
    memset(g,0x3f,sizeof g);  
    g[0][2]=12;  
    g[1][1]=1,g[1][2]=2,g[1][3]=3;  
    g[2][0]=4,g[2][1]=5,g[2][2]=6,g[2][3]=7,g[2][4]=8;  
    g[3][1]=9,g[3][2]=10,g[3][3]=11;  
    g[4][2]=12;  
    memcpy(p1.g,g,sizeof g);  
    p1.x1=0,p1.y1=2;  
    p1.x2=4,p1.y2=2;  
    p1.step=1;  
    bfsx(0);  
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        memset(g,0x3f,sizeof g);  
        for(int i=0; i<5; i++)  
        {  
            if(i==0||i==4) scanf("%d",&g[i][2]);  
            else if(i==1||i==3) for(int j=1; j<=3; j++) scanf("%d",&g[i][j]);  
            else for(int j=0; j<5; j++) scanf("%d",&g[i][j]);  
        }  
        int flag=0;  
        for(int i=0; i<5; i++)  
        {  
            for(int j=0; j<5; j++)  
            {  
                if(g[i][j]==0)  
                {  
                    g[i][j]=12;  
                    if(!flag) p1.x1=i,p1.y1=j;  
                    else p1.x2=i,p1.y2=j;  
                    flag=1;  
                }  
            }  
        }  
        memcpy(p1.g,g,sizeof g);  
        p1.step=1;  
        bfsx(1);  
    }  
    return 0;  
}  



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值