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

#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. Mecanim动画编辑器 - 加入动画层实现并行动作

    1.创建新的状态层 a) 通过下图的1button创建一个新的层   b) 通过下图2属性设置图层的权重.假设为0,则该图层的状态不会影响到总的状态机  c) Mask是设置动画的Avatar的关联节 ...

  2. Error: cannot call methods on draggable prior to initialization; attempted to call

    cannot call methods on draggable prior to initialization; attempted to call  报这个问题的根本原因是由于你的引用文件有问题 ...

  3. 自己动手写CPU之第九阶段(4)——载入存储指令实现思路

    将陆续上传新书<自己动手写CPU>,今天是第40篇,我尽量每周四篇,可是近期已经非常久没有实现这个目标了,一直都有事,不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9%A9 ...

  4. hdu 4707 Pet(DFS &amp;&amp; 邻接表)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. C#读取指定路径下的Config配置文件

    ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @"F:\App1. ...

  6. C#高级编程六十九天----DLR简介 .在.NET中使用DLR(转载) 我也来说说Dynamic

    DLR 一.近年来,在TIOBE公司每个月发布的编程语言排行榜中,C#总是能挤进前十名,而在最近十年来,C#总体上呈现上升的趋势.C#能取得这样的成绩,有很多因素,其中它在语言特性上的锐意进取让人印象 ...

  7. 创建一个zookeeper的会话(实现watcher)

    在先前的章节中,我们利用zkCli去了解了一下主要的zookeeper的操作.在接下来的章节中,我们将会学习一下在应用中是怎样利用zookeeper的api的.接下来我们将利用一个程序展示一下,怎样来 ...

  8. 云打印-Beta-凡事预则立

    凡事预则立 课程名称:软件工程1916|W(福州大学) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标:Beta冲刺 团队队员 队员学号 队员姓名 个人博客地址 备注 221600 ...

  9. Hdu3785

    <span style="color:#6600cc;">/* G - 寻找大富翁 Time Limit:1000MS Memory Limit:32768KB 64b ...

  10. PHP的date 函数

    <!DOCTYPE html> <html> <body> <?php echo "今天是 " . date("Y/m/d&qu ...