欸我一直以为双向bfs是搜完一半再搜另一半呢,妹想到是两个一起搜

然后队列里放的结构体里不能直接存答案,所以做一个邻接表一样的东西,直接指向需要的字符即可

记录状态用康托展开来hash

以及居然是多组数据啊?!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=500005,has[9]={1,1,2,6,24,120,720,5040,40320},dx[4]={-3,3,-1,1};
int v1[N],v2[N];
char a[30],d1[10]={"udlr"},d2[10]={"durl"};
string b="123456780";
struct qw
{
int a;
char ch;
qw(int A=0,char C=0)
{
a=A,ch=C;
}
}pre[N];
struct qwe
{
int a;
string c;
qwe(int A=0,string C="")
{
a=A,c=C;
}
}e;
void print(int x)
{
if(pre[x].a==-1)
return;
print(pre[x].a);
printf("%c",pre[x].ch);
}
int ha(string e)
{
int s=0,i,j,k;
for(i=0;i<9;i++)
{
k=0;
for(j=0;j<i;j++)
if(e[j]>e[i])
k++;
s+=k*has[i];
}
return s;
}
int main()
{
while(gets(a))
{
int k=0;
e.c="";
for(int i=0,j=0;i<strlen(a);i++)
if(a[i]!=' ')
{
if(a[i]=='x')
e.a=j,e.c+='0';
else
e.c+=a[i];
j++;
}
for(int i=0;i<9;i++)
if(e.c[i]!='0')
for(int j=0;j<i;j++)
if(e.c[j]!='0'&&e.c[j]>e.c[i])
k++;
memset(v2,0,sizeof(v2));
memset(v1,0,sizeof(v1));
if(k&1)
printf("unsolvable\n");
else
{
qwe f,g;
int a=2;
queue<qwe>q1,q2;
v1[ha(e.c)]=1;
pre[1].a=-1,pre[2].a=-1;
f=qwe(8,b);
v2[ha(f.c)]=2;
q1.push(e),q2.push(f);
while(!q1.empty()&&!q2.empty())
{
f=q1.front();
q1.pop();
int p=ha(f.c);
if(v2[p])
{
print(v1[p]);
for(int k=v2[p];pre[k].a!=-1;k=pre[k].a)
printf("%c",pre[k].ch);
printf("\n");
break;
}
for(int i=0;i<4;i++)
if((i!=0||f.a>=3)&&(i!=1||f.a<=5)&&(i!=2||f.a%3!=0)&&(i!=3||f.a%3!=2))
{
int x=f.a+dx[i];
g=f;
swap(g.c[f.a],g.c[x]);
int q=ha(g.c);
if(v1[q])
continue;
v1[q]=++a;
g.a=x;
pre[a]=qw(v1[p],d1[i]);
q1.push(g);
}
f=q2.front();
q2.pop();
p=ha(f.c);
if(v1[p])
{
print(v1[p]);
for(int k=v2[p];pre[k].a!=-1;k=pre[k].a)
printf("%c",pre[k].ch);
printf("\n");
break;
}
for(int i=0;i<4;i++)
if((i!=0||f.a>=3)&&(i!=1||f.a<=5)&&(i!=2||f.a%3!=0)&&(i!=3||f.a%3!=2))
{
int x=f.a+dx[i];
g=f;
swap(g.c[f.a],g.c[x]);
int q=ha(g.c);
if(v2[q])
continue;
v2[q]=++a;
g.a=x;
pre[a]=qw(v2[p],d2[i]);
q2.push(g);
}
}
printf("unsolvable\n");
}
}
return 0;
}

hdu 1043 Eight的更多相关文章

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

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

  2. HDU 1043 Eight(八数码)

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

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

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

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

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

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

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

  6. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  7. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  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 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  10. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

随机推荐

  1. Happy Three Friends

    Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  2. [HAOI2011]Problem b 题解

    题目大意: 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y)=k. 思路: 设f(k)为当1≤x≤n,1≤y≤m,且n≤m,使gcd(x,y)=k的数对 ...

  3. hdu 1179最大匹配

    #include<stdio.h> #include<string.h> #define N 200 int map[N][N],visit[N],link[N],n,m; i ...

  4. es6异步编程 Promise 讲解 --------各个优点缺点总结

    //引入模块 let fs=require('fs'); //异步读文件方法,但是同步执行 function read(url) { //new Promise 需要传入一个executor 执行器 ...

  5. P1547 Out of Hay 洛谷

    https://www.luogu.org/problem/show?pid=1547 题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的 ...

  6. AOJ731(不等式)

    题意:有n(n<=100)个石头,每个石头的价值在Ai~Bi(1<=Ai<=Bi<=10000)之间,将这些石头分给两个人,求两个人的最大总价值差的最小值 分析: 与一般的求最 ...

  7. SAP EP 设置Portal别名安全模式

    Securing the Portal Alias Cookie Context We recommend that you set the portal alias cookie to be del ...

  8. Centos7最小安装下Install Clamav(2017-06-09最后更新)

    If you are installing ClamAV for the first time, you have to add a new user and group to your system ...

  9. bzoj 1266 [AHOI2006] 上学路线 route 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23989499 [原题] 1266: [AHOI2006]上学路线route Time ...

  10. Struts2.3动态调用报 No result defined for action 错误

    struts 2.3.16  採用动态调用发现不工作报404 not found,网上查找原因: 1.由于:struts2中默认不同意使用DMI 所以:须要在配置文件里打开: <constant ...