HDU 1043 Eight

选择了最简单的康托展开+打表。注意问题:多组输入。

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <stdlib.h>
//#include <windows.h>
using namespace std;
//for(i=1;i<n;i++)
//scanf("%d",&n);
//printf("\n",);

struct node
{
    char ch[3][3];
}x,y,temp;

int jiecheng[15];
char ch[50],inf[20],tteemmpp;
queue<node>q;
bool flg[400000];
string ans[400000];

int f()
{
    int ans=0;
    for(int i=0;i<9;i++)
    {
        int x=0;int c=1,m=1;
        for(int j=i+1;j<9;j++)
        {
            if(inf[j]<inf[i])x++;
            m*=c;c++;
        }
        ans+=x*m;
    }
    return ans+1;
}

int main()
{
    int i,j,k,l;

    jiecheng[1]=1;
    for(i=1;i<11;i++)
    jiecheng[i]=i*jiecheng[i-1];

    for(i=0;i<400000;i++)
    {
        ans[i]="";
    }


    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    {
        x.ch[i][j]=i*3+j+1;
        inf[i*3+j]=x.ch[i][j];
    }

    flg[f()]=1;

    q.push(x);int xx,yy;

    while(!q.empty())
    {
        y=q.front();
        q.pop();

        for(i=0;i<3;i++)
        for(j=0;j<3;j++)
        {
            if(y.ch[i][j]==9)
            {
                xx=i;yy=j;
            }
            inf[i*3+j]=y.ch[i][j];
        }
        int tt=f();


        if(yy+1<3)
        {
            temp=y;
            tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx][yy+1];temp.ch[xx][yy+1]=tteemmpp;
            for(i=0;i<3;i++)
            for(j=0;j<3;j++)
            inf[i*3+j]=temp.ch[i][j];
            k=f();
            if(flg[k]==0)
            {
                q.push(temp);
                ans[k]="l"+ans[tt];flg[k]=1;
            }

        }

        if(yy-1>=0)
        {
            temp=y;
            tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx][yy-1];temp.ch[xx][yy-1]=tteemmpp;
            for(i=0;i<3;i++)
            for(j=0;j<3;j++)
            inf[i*3+j]=temp.ch[i][j];
            k=f();
            if(flg[k]==0)
            {
                q.push(temp);
                ans[k]="r"+ans[tt];flg[k]=1;
            }

        }


        if(xx+1<3)
        {
            temp=y;
            tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx+1][yy];temp.ch[xx+1][yy]=tteemmpp;
            for(i=0;i<3;i++)
            for(j=0;j<3;j++)
            inf[i*3+j]=temp.ch[i][j];
            k=f();
            if(flg[k]==0)
            {
                q.push(temp);
                ans[k]="u"+ans[tt];flg[k]=1;
            }

        }

        if(xx-1>=0)
        {
            temp=y;
            tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx-1][yy];temp.ch[xx-1][yy]=tteemmpp;
            for(i=0;i<3;i++)
            for(j=0;j<3;j++)
            inf[i*3+j]=temp.ch[i][j];
            k=f();
            if(flg[k]==0)
            {
                q.push(temp);
                ans[k]="d"+ans[tt];flg[k]=1;
            }

        }


    }


    while(gets(ch)>0)
    {
        j=0;
        for(i=0;i<strlen(ch);i++)
        {
            if(ch[i]!=' ')
            {
                  inf[j]=ch[i];
                  if(inf[j]=='x')  inf[j]=='9';
                  inf[j]-='0';
                  j++;
            }

        }

        int fin=f();
        /*if(fin==1)
        {
            cout<<""<<endl;
            continue;
        }*/

        if(flg[fin]==1) cout<<ans[fin]<<endl;
        else printf("unsolvable\n");

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值