https://www.luogu.org/problemnew/show/P2578

一个挺搞的东西,用康托展开做记忆化搜索可以少一个log的查询。

#include <bits/stdc++.h>
using namespace std;
#define ll long long static const int fac[] = {, , , , , , , , , ,}; // 阶乘 //康托展开
int cantor(int *a,int n)
{
int code=;
for(int i=; i<n; i++)
{
int x=;
int c=,m=;//c记录后面的阶乘
for(int j=i+; j<n; j++)
{
if(a[j]<a[i])x++;
m*=c;
c++;
}
code+=x*m;
}
//printf("cantor=%d\n",code);
return code;
} //逆康托展开
void decantor(int code,int *a,int n)
{
bool vis[]= {};
for(int i=; i<n; i++)
{
int r = code / fac[n-i-];
code %= fac[n-i-];
int cnt = ;
int j;
for(j=; j<=n; j++)
{
if(vis[j]==)
{
cnt++;
if(cnt==r+)
{
a[i]=j;
vis[j]=;
break;
}
}
}
} /*printf("decantor=");
for(int i=0; i<n; i++)
{
printf(" %d",a[i]);
}
printf("\n");*/
} ll rot1(ll code)
{
int a[];
decantor(code,a,);
int t=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=t;
int ans=cantor(a,);
return ans;
} int rot2(int code)
{
int a[];
decantor(code,a,);
int t=a[];
a[]=a[];
a[]=a[];
a[]=t;
int ans=cantor(a,);
return ans;
} struct dat
{
int cur;
int pre;
} d,dt,data[]; queue<dat> q; void print(int code)
{
int a[],n=; bool vis[]= {};
for(int i=; i<n; i++)
{
int r = code / fac[n-i-];
code %= fac[n-i-];
int cnt = ;
int j;
for(j=; j<=n; j++)
{
if(vis[j]==)
{
cnt++;
if(cnt==r+)
{
a[i]=j;
vis[j]=;
break;
}
}
}
} printf("\n");
for(int i=; i<n; i++)
{
printf("%d%c",a[i]-," \n"[i%==]);
}
} void show(dat d)
{
stack<ll> s;
s.push(d.cur);
while(d.pre!=-)
{
s.push(d.pre);
d=data[d.pre];
} printf("%d",s.size()-); while(!s.empty())
{
print(s.top());
s.pop();
}
} inline bool found(int code){
return !code;
} void bfs(int s)
{
//memset(data,0,sizeof(data)); d.cur=s;
d.pre=-; if(found(d.cur))
{
show(d);
return;
} data[d.cur]=d;
q.push(d); while(!q.empty())
{
d=q.front();
q.pop(); int t1=rot1(d.cur);
if(data[t1].cur)
;
else
{
dt.cur=t1;
dt.pre=d.cur;
if(found(dt.cur))
{
show(dt);
return;
}
data[t1]=dt;
q.push(dt);
} int t2=rot2(d.cur);
if(data[t2].cur)
;
else
{
dt.cur=t2;
dt.pre=d.cur;
if(found(dt.cur))
{
show(dt);
return;
}
data[t2]=dt;
q.push(dt);
}
} printf("UNSOLVABLE\n");
} int main()
{
int a[];
for(int i=; i<; i++)
{
scanf("%d",&a[i]);
a[i]++;
} int s=cantor(a,);
bfs(s);
}

最新文章

  1. UE4 Android相对路径转绝对路径方法笔记
  2. Spring学习之AOP总结帖
  3. 在stream流和byte[]中查找(搜索)指定字符串
  4. BZOJ 4665: 小w的喜糖
  5. 转载Mongondb
  6. hdu-5681 zxa and wifi(dp)
  7. java基础之运算符
  8. MindManager 安装注册
  9. 【新建项目&amp;使用viewPager】实现一个Android电子书阅读APP
  10. vs2012 asp调试设置 清单目录
  11. 关于CoordinatorLayout的用法——复杂交互的克星
  12. 41. First Missing Positive(困难, 用到 counting sort 方法)
  13. Python Redis 的安装
  14. python爬虫的重定向问题
  15. ecshop 商品属性显示方法
  16. iOS进阶之UDP代理鉴权过程
  17. Grunt、Gulp区别 webpack、 requirejs区别
  18. [IDEA_1] IDEA 使用指南
  19. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON SigmaImage2
  20. linux系统挂载U盘,中文文件名乱码解决方案

热门文章

  1. JNI之—— Eclipse配置C/C++开发环境
  2. SolidEdge如何快速绘制并完全定义槽型孔
  3. linux字符设备驱动程序框架(老方法)
  4. VBA 把电信的电话费用表转换成部门电话费用明细表(图文)
  5. C#.net word excel powerpoint (ppt) 转换成 pdf 文件
  6. PJzhang:python基础入门的7个疗程-two
  7. Java学习之集合
  8. 【转载】图说OOP基础(一)
  9. HDU 1312 Red and Black 第一题搜索!
  10. 混合minxins