写状态转移弄了很久,老了,不记得自己的数组是怎么标号的了。

#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");*/
} void showdecantor(int code,int n){
int a[];
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=;i<n;i++){
printf(" %d",a[i]);
if(i%==)
printf("\n");
}
printf("\n");
} int encode(int *a)
{
int res=;
for(int i=;i<; i++)
{
if(a[i])
res|=;
res<<=;
}
return res>>;
} void decode(int code,int *a)
{
for(int i=; i>=; i--)
{
a[i]=code&;
code>>=;
}
} struct dat
{
int cur;
int pre;
char cha;
} d,dt,data[]; queue<dat> q; void show(dat d)
{
stack<int> s;
s.push(d.cur);
while()
{
if(d.pre==-)
break;
s.push(d.pre);
d=data[d.pre];
} printf("%d\n",s.size()-);
int cur=s.top(); string ans=""; while(!s.empty())
{
int nex=s.top();
s.pop();
//printf("%c\n",data[cur].cha);
if(data[cur].cha!='N'){
if(ans.length()>=){
cout<<ans<<endl;
ans="";
}
ans+=data[cur].cha;
}
//showdecantor(cur,8);
cur=nex;
} if(data[cur].cha!='N'){
if(ans.length()>=){
cout<<ans<<endl;
ans="";
}
ans+=data[cur].cha;
} cout<<ans<<endl;
} int s,t; inline bool found(int code)
{
return (code==t);
} int A(int code){
int a[];
decantor(code,a,);
swap(a[],a[]);
swap(a[],a[]);
swap(a[],a[]);
swap(a[],a[]);
return cantor(a,);
} int B(int code){
int a[];
decantor(code,a,);
int t1=a[];
int t2=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=t1; a[]=a[];
a[]=a[];
a[]=a[];
a[]=t2; return cantor(a,);
} int C(int code){
int a[];
decantor(code,a,);
int t=a[];
a[]=a[];
a[]=a[];
a[]=a[];
a[]=t;
return cantor(a,);
} void bfs()
{
memset(data,-,sizeof(data)); d.cur=s;
d.pre=-;
d.cha='N';
data[d.cur]=d; if(found(d.cur))
{
printf("0\n\n");
return;
}
q.push(d);
while(!q.empty())
{
d=q.front();
q.pop(); dt.cur=A(d.cur);
if(data[dt.cur].cur==-){
dt.pre=d.cur;
dt.cha='A';
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
} dt.cur=B(d.cur);
if(data[dt.cur].cur==-){
dt.pre=d.cur;
dt.cha='B';
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
} dt.cur=C(d.cur);
if(data[dt.cur].cur==-){
dt.pre=d.cur;
dt.cha='C';
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
}
} cout<<"NOFOUND"<<endl;
} int main()
{
int a[];
for(int i=; i<; i++)
a[i]=i+; s=cantor(a,); for(int i=; i<; i++)
scanf("%d",&a[i]); t=cantor(a,);
//decantor(t,a,8); bfs();
}

洛谷 - P2730 - 魔板 Magic Squares - bfs的更多相关文章

  1. 洛谷 P2730 魔板 Magic Squares 解题报告

    P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...

  2. 洛谷 P2730 魔板 Magic Squares

    P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...

  3. [洛谷P2730] 魔板 Magic Squares

    洛谷题目链接:魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都 ...

  4. 洛谷P2730 魔板 [广搜,字符串,STL]

    题目传送门 魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有 ...

  5. P2730 魔板 Magic Squares

    题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...

  6. P2730 魔板 Magic Squares (搜索)

    题目链接 Solution 这道题,我是用 \(map\) 做的. 具体实现,我们用一个 \(string\) 类型表示任意一种情况. 可以知道,排列最多只有 \(8!\) 个. 然后就是直接的广搜了 ...

  7. 哈希+Bfs【P2730】 魔板 Magic Squares

    没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...

  8. 【简●解】 LG P2730 【魔板 Magic Squares】

    LG P2730 [魔板 Magic Squares] [题目背景] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 ...

  9. [USACO3.2]魔板 Magic Squares

    松下问童子,言师采药去. 只在此山中,云深不知处.--贾岛 题目:魔板 Magic Squares 网址:https://www.luogu.com.cn/problem/P2730 这是一张有8个大 ...

随机推荐

  1. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  2. 卸载 linux http

    当我们想卸载httpd 的时候,使用rpm -qa httpd 的时候,我们发现有很多的依赖包.我们耐心的想一个一个的卸载的时候(使用rpm -e httpd-*),还会进入死循环.解决的办法是:使用 ...

  3. Android开发Tips(3)

    欢迎Follow我的GitHub, 关注我的CSDN. 我会介绍关于Android的一些有趣的小知识点. 本文是第三篇, 其余第一篇, 第二篇. imageMogr2/auto-orient/stri ...

  4. The most widely used name server software: BIND

    https://www.isc.org/downloads/bind/ The most widely used name server software: BIND BIND is open sou ...

  5. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. HDU 2108 Shape of HDU (判断是不是凸多边形 叉乘)

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. JfreeChart折线图 CSDN-李鹏飞

    今天公司里分配给我的工作是JfreeChart折线图本人之前也没接触过如今让我们大家一起完毕! 在这个公司,用到了太多的JfreeChart,今天就对折线图作一个总结,希望对大家有点帮助,我这里直接是 ...

  8. mysqld与mysqld_safe的区别

    文章1: 直接运行mysqld程序来启动MySQL服务的方法很少见,mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启动它.用mysqld_safe脚本来启动M ...

  9. liberOJ#6006. 「网络流 24 题」试题库 网络流, 输出方案

    #6006. 「网络流 24 题」试题库     题目描述 假设一个试题库中有 n nn 道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性.现要从题库中抽取 m mm 道题组成试卷.并要求 ...

  10. Freemarker 中的哈希表(Map)和序列(List)

    freemarlker中的容器类型有: 哈希表:是实现了TemplateHashModel或者TemplateHashModelEx接口的java对象,经常使用的实现类是SimpleHash,该类实现 ...