设为首页 加入收藏

TOP

poj 1184
2014-11-23 20:10:26 来源: 作者: 【 】 浏览:12
Tags:poj 1184

经典的宽搜题目,感觉最好的办法应该是双向广搜。

不过用简单的启发式搜索可以飘过。

#include 
#include 
#include 
#include 
#include 
using namespace std;
int a,b;
char ans[1111111][7];
int inf[7]={1,1,10,100,1000,10000,100000};
struct D
{
    int key;
    char x,sum,now;
    bool operator <(const struct D & xx) const
    {
        if(sum==xx.sum)
        return nowxx.sum;
    }
};
priority_queue  q;

int cal(int key,int x,int b)
{
    int from[7],to[7];
    for(int i=1;i<=6;i++)
    {
        from[i]=key%10;
        key/=10;
        to[i]=b%10;
        b/=10;
    }
    int ret=0;
    for(int i=1;i<=6;i++)
    if(i!=x)
    {
        ret+=from[i]!=to[i];
    }
    sort(from+1,from+1+6);
    sort(to+1,to+1+6);
    for(int i=1;i<=6;i++)
    {
        if(from[i]-to[i]>0)
        ret+=from[i]-to[i];
        else
        ret+=to[i]-from[i];
    }
    return ret;
}

int bfs(int a,int b)
{
    memset(ans,100,sizeof(ans));
    ans[a][1]=0;
    struct D xx;
    xx.key=a;
    xx.sum=0+cal(a,1,b);
    xx.x=1;
    xx.now=0;
    q.push(xx);
    while(1)
    {
        int key=q.top().key;
        int x=q.top().x;
        int tmp=ans[key][x];
        q.pop();
        if(key==b)
        return tmp;
        if(x<6&&tmp+10&&tmp+1 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 4339 Query(两种思路求解) 下一篇hdu 1085

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)