题意:就是恢复成1,2,3,4,5,6,7,8,0;

分析:暴力BFS预处理,所有路径,用康拓展开判重,O(1)打印 93ms 还是很快的

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
int fac[]= {,,,,,,,,,};
int aim;
int cantor(char s[])
{
int ans=;
for(int i=,j=; i<=; ++i,--j)
{
int tmp=;
for(int k=i+; k<=; ++k)
if(s[i]>s[k])++tmp;
ans+=(tmp*fac[j]);
}
return ans;
}
struct Node
{
char s[];
int hs;
};
struct asd
{
bool vis;
char c;
int pre;
}o[];
queue<Node>q;
void bfs()
{
Node a;
for(int i=; i<=; ++i)
a.s[i]=''+i;
a.s[]='';
aim=a.hs=cantor(a.s);
o[aim].vis=;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
int now=a.hs;
int x;
for(int i=; i<=; ++i)
if(a.s[i]=='')x=i;
if(x->)
{
bool flag=;
swap(a.s[x],a.s[x-]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='d';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x-]);
}
if(x+<)
{
bool flag=;
swap(a.s[x],a.s[x+]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='u';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x+]);
}
if(x%!=)
{
bool flag=;
swap(a.s[x],a.s[x-]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='r';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x-]);
}
if(x%)
{
bool flag=;
swap(a.s[x],a.s[x+]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='l';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x+]);
}
}
}
char str[],tmp[];
void print(int u)
{
while(u!=aim)
{
printf("%c",o[u].c);
u=o[u].pre;
}
printf("\n");
}
int main()
{
for(int i=;i<;++i)
o[i].vis=;
bfs();
while(gets(str))
{
int l=;
for(int i=; str[i]!=''; ++i)
{
if(str[i]=='x')tmp[++l]='';
else if(str[i]>=''&&str[i]<='')tmp[++l]=str[i];
}
int ans=cantor(tmp);
if(!o[ans].vis)
printf("unsolvable\n");
else
print(ans);
}
return ;
}

HDU 1043 Eight BFS的更多相关文章

  1. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  2. Eight HDU - 1043 (双向BFS)

    记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...

  3. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

  4. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  5. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  6. HDU - 1043 - Eight / POJ - 1077 - Eight

    先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  8. hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...

  9. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

随机推荐

  1. jsf2入门视频 教程

    jsf2.0 入门视频 教程   需要的看下.初次录视频.还有很多需要完善. JSF交流QQ群84376982 JSF入门视频下载地址  http://pan.baidu.com/s/1jG3y4T4 ...

  2. 同一台Windows机器中启动多个Memcached服务

    同一台Windows机器中启动多个Memcached服务 这就需要在一台机器上启动多个Memcached服务了. 假设Memcached在如下目录:C:\memcached\memcached.exe ...

  3. 5.3:从bean的实例中获取对象

    5.3  从bean的实例中获取对象 在getBean方法中,getObjectForBeanInstance是个高频率使用的方法,无论是从缓存中获得bean还是根据不同的scope策略加载bean. ...

  4. c++ 发布动态.so

    原文地址 代码改变世界 Posts - 105, Articles - 0, Comments - 1561 Cnblogs Dashboard Logout Home Contact Gallery ...

  5. encodeURIComponent()编码和decodeURIComponent()解码

    html1: <!DOCTYPE HTML> <meta charset=utf-8> <meta http-equiv="X-UA-Compatible&qu ...

  6. shuffle过程中的信息传递

    依据Spark1.4版 Spark中的shuffle大概是这么个过程:map端把map输出写成本地文件,reduce端去读取这些文件,然后执行reduce操作. 那么,问题来了: reducer是怎么 ...

  7. Java 内存结构备忘录

    本文详细描述了 Java 堆内存模型,垃圾回收算法以及处理内存泄露的最佳方案,并辅之以图表,希望能对理解 Java 内存结构有所帮助.原文作者 Sumith Puri,本文系 OneAPM 工程师编译 ...

  8. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  9. eclipse运行hadoop程序报错:Connection refused: no further information

    eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...

  10. aop aspect

    所以“<aop:aspect>”实际上是定义横切逻辑,就是在连接点上做什么,“<aop:advisor>”则定义了在哪些连接点应用什么<aop:aspect>.Sp ...