周赛 HDU 1043 Eight

13 篇文章 0 订阅

题意:经典的八数码问题。

学习了康托展开,理解之后很好实现。

采用了最朴素的BFS作法,继续学习A*和IDA*

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 500005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define Rep(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)

using namespace std;
struct kdq
{
    string op;
    char Map[4][4] ;
    int x ,y ;
    void show()
    {
        Rep(i,0,2)
        {
            Rep(j,0,2)
            cout <<char(Map[i][j] + '0');
            cout <<endl;
        }
        cout <<endl;
    }
} ;
string state[Max] ;

int fac[12] = { 0, 1, 2, 6, 24, 120,720, 5040, 40320, 362880, 3628800} ;
int kangtuo(kdq a)
{
    int ans = 0 ;
    Rep(i,0,8)
    {
        int num = 0 ;
        Rep(j,i + 1 ,8)
        {
            if(a.Map[i / 3][i % 3] > a.Map[j / 3 ][j % 3])
                num ++ ;
        }
        ans += num * (fac[8 - i]) ;
    }
    return ans ;
}
int vis[Max] ;
int mx[4] = {0,0,1,-1} ;
int my[4] = {1,-1,0,0} ;
char opp[4] = {'l' ,'r','u','d'} ;//注意这里的移动方向要与坐标的移动方向完全相反,因为我们是从最终状态开始搜的,所以从开始到结果的方向是相反的。
int inmap(kdq a )
{
    if(a.x >= 0 && a.y <3 && a.x < 3 && a.y >= 0)return 1;
    return 0 ;
}
void bfs()
{
    queue<kdq>q ;
    kdq init ;
    init.Map[0][0] = 1 ;
    init.Map[0][1] = 2 ;
    init.Map[0][2] = 3 ;
    init.Map[1][0] = 4 ;
    init.Map[1][1] = 5 ;
    init.Map[1][2] = 6 ;
    init.Map[2][0] = 7 ;
    init.Map[2][1] = 8 ;
    init.Map[2][2] = 9 ;
    init.x = 2 ;
    init.y = 2 ;
    init.op.clear() ;
    q.push(init) ;
    //init.show() ;
    while(!q.empty())
    {
        kdq temp = q.front() ;
        q.pop() ;
        int cc = kangtuo(temp) ;
        state[cc] = temp.op ;
        reverse(state[cc].begin(),state[cc].end()) ;//注意得取反
        Rep(i,0,3)
        {
            kdq next = temp ;
            next.x = temp.x + mx[i] ;
            next.y = temp.y + my[i] ;
            //temp.show() ;
            if(inmap(next))
            {
                swap(next.Map[next.x][next.y] ,next.Map[temp.x][temp.y]) ;
                int num = kangtuo(next) ;
                if(!vis[num])
                {
                    next.op += opp[i] ;
                    vis[num] = 1 ;
                    //next.show() ;
                    //getchar() ;
                    q.push(next) ;
                }
            }
        }
    }
}

char a[10000] ;
int main()
{
    bfs() ;
    while(gets(a))
    {
        int l = strlen(a) ;
        kdq now ;
        int tx = 0 ;
        Rep(i,0,l - 1)
        {
            if(a[i] >= '0' && a[i] <= '8')
            {
                now.Map[tx / 3] [tx % 3] = a[i] - '0' ;
                tx ++ ;
            }
            else if(a[i] == 'x')
            {
                now.Map[tx / 3][tx % 3] = 9 ;
                tx ++ ;
            }
        }
        //now.show() ;
        int num = kangtuo(now) ;
        if(!vis[num])
            cout <<"unsolvable"<<endl;
        else
            cout <<state[num]<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值