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);
}

洛谷 - P2578 - 九数码游戏 - bfs的更多相关文章

  1. [ZJOI2005]九数码游戏(BFS+hash)

    Solution 这题的话直接上BFS就可以了,因为要输出方案,所以我们要开一个pre数组记录前驱,最后输出就可以了. 对于状态的记录,一般都用哈希来存,但因为这道题比较特殊,它是一个排列,所以我们可 ...

  2. 洛谷 - P1379 - 八数码难题 - bfs

    https://www.luogu.org/problemnew/show/P1379 #include <bits/stdc++.h> using namespace std; #def ...

  3. 洛谷 - P1225 - 黑白棋游戏 - bfs

    神奇bug,没有记录pre就show了,找了1个小时. #include <bits/stdc++.h> using namespace std; #define ll long long ...

  4. LG2578 「ZJOI2005」九数码游戏 bfs

    问题描述 LG2578 题解 用string+map去重. bfs即可. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace ...

  5. 万圣节后的早晨&&九数码游戏——双向广搜

    https://www.luogu.org/problemnew/show/P1778 https://www.luogu.org/problemnew/show/P2578 双向广搜. 有固定起点终 ...

  6. [ZJOI2005]九数码游戏

    [ZJOI2005]九数码游戏 题目描述 输入输出格式 输入格式: 输入文件中包含三行三列九个数,同行的相邻两数用空格隔开,表示初始状态每个方格上的数字.初始状态不会是目标状态. 输出格式: 如果目标 ...

  7. 洛谷P1118 数字三角形游戏

    洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直 ...

  8. 洛谷P1274-魔术数字游戏

    Problem 洛谷P1274-魔术数字游戏 Accept: 118    Submit: 243Time Limit: 1000 mSec    Memory Limit : 128MB Probl ...

  9. 洛谷P1288 取数游戏II(博弈)

    洛谷P1288 取数游戏II 先手必胜的条件需要满足如下中至少 \(1\) 条: 从初始位置向左走到第一个 \(0\) 的位置,经过边的数目为偶数(包含 \(0\) 这条边). 从初始位置向右走到第一 ...

随机推荐

  1. 前端高频面试题 JavaScript篇

    以下问题都来自于互联网前端面经分享,回答为笔者通过查阅资料加上自身理解总结,不保证解答的准确性,有兴趣讨论的同学可以留言或者私信讨论. 1.JS的异步机制? 2.闭包如何实现? 3.原型链.继承? 4 ...

  2. win7下 安装 source code pro

    转: http://my.oschina.net/yearnfar/blog/325107 source code pro 字体安装, 一. 去 https://github.com/adobe-fo ...

  3. HTML页面底部无用留白

    HTML页面底部无用留白,可以再footer样式中加入: overflow: hidden; 如有错误,请您指正~

  4. hibernate载入持久化对象的两种方式——get、load

    一.get与load对照 在hibernate中get和load方法是依据id取得持久化对象的两种方法.但在实际使用的过程中总会把两者混淆,不知道什么情况下使用get好,什么时候使用load方法效率更 ...

  5. openwrt mt7620 内存大小检测

    单独编译内核: make V=s target/linux/install 相调函数调用流程: init/main.c : start_kernel() -> setup_arch(&c ...

  6. easyUI datagrid组件能否有display:none的隐藏效果

    这个项目用了JQ easyUI datagrid 组件,我今天做了一个页面,页面有个div层,div里放了一个easyUI datagrid,页面初始化时div隐藏(display:none),通过点 ...

  7. (转载)解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes

    今天想用用MySQL 数据库  谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...

  8. js中字符串函数indexOf与search的区别

    IndexOf()方法是用来判断一个字符串是否存在于一个更长的字符串中.从长字符串左端到右端来搜索,如果存在该子字符串就返回它所处的位置(即索引).如果在被搜索的字符串没有找到要查找的字符串返回-1. ...

  9. Delphi通过Get获取来自PHP的返回值

    Delphi代码 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contro ...

  10. shell脚本怎么调试

    shell是Linux系统上常用的一种脚本语言.一般从事web后台开发的从业者,都会用到shell,因此shell调试也是一项必备的技能.本文教你如何进行shell脚本调试. 工具/原料   Linu ...