poj1077Eights

#include<iostream>
#include<string>
using namespace std;
#define MAX_SIZE 362880 //一共可能出现得状态,9!
struct node{
    char state[9];
    char space;//x所在位置
    int pre;
    int value_hash;
};
node q[MAX_SIZE];
char v[MAX_SIZE];
char move[MAX_SIZE];
bool flag=false;
int d[4][2] = {{-1, 0},{1, 0}, {0, -1}, {0, 1}};//u, d, l, r
int fac[9]={1,1,2,6,24,120,720,5040,40320};
int hash_map(char * s,int n) //找到每一个状态对应的hash值
{
    int code=0;
    for(int i=0;i<n;i++)
    {
        int cnt=0;
        for(int j=i+1;j<n;j++)
        {
            if(s[j]<s[i])
            {
                 cnt++;
            }
       }
       code+=fac[s[i]-1]*cnt;
   }
    return code;
}
void bfs(node & start)
{
    int x,y,u,new_x,new_y,hash_value;
    int front=0,rear=0;
    node now_node,ext_node;
    memset(v,0,MAX_SIZE);
    start.value_hash=hash_map(start.state,9);
    start.pre=-1;
    v[start.value_hash]=1;
    p[start.value_hash]=-1;
    q[rear++]=start;
    while(front<rear)
    {
        now_node=q[front++];
        int x_pos=now_node.space;
        x=x_pos/3;
        y=x_pos%3;
        for(int i=0;i<4;i++)
        {
            new_x=x+d[i][0];
            new_y=y+d[i][1];
            if(new_x<3 && new_x>=0 && new_y<3 && new_y>=0)
            {
                ext_node=now_node;
                ext_node.space=new_x*3+new_y;
                swap(ext_node.state[x_pos],ext_node.state[ext_node.space]);
                ext_node.value_hash=hash_map(ext_node.state,9);
                if(!v[ext_node.value_hash])
                {
                
                    move[ext_node.value_hash]=i;//记录到达此状态的走向
                    v[ext_node.value_hash]=1;
                    ext_node.pre=front-1;//记录其父节点
                    if(ext_node.value_hash==0)
                    {
                        int pre=front-1,h_value,j=1;
                        char path[1000];
                        path[0]=move[0];
                        while(pre!=0)
                        {
                            h_value=q[pre].value_hash;
                            path[j++]=move[h_value];
                            pre=q[pre].pre;
                    //        cout<<pre<<endl;
                        }
                        j--;
                        for(;j>=0;j--)
                        {
                            if(path[j]==0)
                                cout<<'u';
                            else if(path[j]==1)
                                cout<<'d';
                            else if(path[j]==2)
                                cout<<'l';
                            else
                                cout<<'r';
                        }
                        cout<<endl;
                        flag=true;
                        return;
                    }
                    q[rear++]=ext_node;
                }
            }
        }
    }

}
int main()
{
    node start;
    char c;
    for(int i=0; i<9; ++i)
    {
        cin>>c;
        if(c == 'x'){
            start.state[i] = 9;
            start.space = i;
        }
        else
            start.state[i] = c - '0';
    }

    bfs(start);
    if(!flag)
        cout<<"unsolvable"<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值