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

#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");*/
} struct dat
{
int cur;
int pre;
} d,dt,data[]; queue<dat> q; void show(dat d)
{
//cout<<"s"<<endl;
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 s,t; inline bool found(int code)
{
return (code==t);
} int up(int code){
int a[];
decantor(code,a,);
for(int i=;i<;i++){
if(a[i]==){
if(i>=){
swap(a[i],a[i-]);
return cantor(a,);
}
}
}
return -;
} int down(int code){
int a[];
decantor(code,a,);
for(int i=;i<;i++){
if(a[i]==){
if(i<=){
swap(a[i],a[i+]);
return cantor(a,);
}
}
}
return -;
} int right(int code){
int a[];
decantor(code,a,);
for(int i=;i<;i++){
if(a[i]==){
if(i%!=){
swap(a[i],a[i+]);
return cantor(a,);
}
}
}
return -;
} int left(int code){
int a[];
decantor(code,a,);
for(int i=;i<;i++){
if(a[i]==){
if(i%){
swap(a[i],a[i-]);
return cantor(a,);
}
}
}
return -;
} void bfs()
{
memset(data,-,sizeof(data)); d.cur=s;
d.pre=-;
data[d.cur]=d; if(found(d.cur))
{
printf("0\n");
return;
}
q.push(d); while(!q.empty())
{
d=q.front();
q.pop(); dt.cur=up(d.cur);
if(dt.cur!=-&&data[dt.cur].cur==-){
dt.pre=d.cur;
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
} dt.cur=down(d.cur);
if(dt.cur!=-&&data[dt.cur].cur==-){
dt.pre=d.cur;
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
} dt.cur=left(d.cur);
if(dt.cur!=-&&data[dt.cur].cur==-){
dt.pre=d.cur;
data[dt.cur]=dt;
if(found(dt.cur)){
show(dt);
return;
}
q.push(dt);
} dt.cur=right(d.cur);
if(dt.cur!=-&&data[dt.cur].cur==-){
dt.pre=d.cur;
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]++;
t=cantor(a,); for(int i=; i<; i++)
scanf("%1d",&a[i]); s=cantor(a,);
//decantor(t,a,8); bfs();
}

洛谷 - P1379 - 八数码难题 - bfs的更多相关文章

  1. 洛谷——P1379 八数码难题

    P1379 八数码难题 双向BFS 原来双向BFS是这样的:终止状态与起始状态同时入队,进行搜索,只不过状态标记不一样而已,本题状态使用map来存储 #include<iostream> ...

  2. 洛谷 P1379 八数码难题 解题报告

    P1379 八数码难题 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初 ...

  3. 洛谷P1379八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中. 要求解的问题是:给出一种初始布局(初始状态)和目标布局(为 ...

  4. 洛谷 P1379 八数码难题 Label:判重&&bfs

    特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...

  5. 洛谷 P1379 八数码难题(map && 双向bfs)

    题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...

  6. 洛谷—— P1379 八数码难题

    https://daniu.luogu.org/problem/show?pid=1379 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示 ...

  7. 洛谷P1379 八数码难题

    传送门 1.先用dfs枚举9!的全排列,存到hash数组里(类似离散化),因为顺序枚举,就不需要排序了 2.朴素bfs,判重就用二分找hash:如果发现当前状态=要求状态,输出步数结束程序 上代码 # ...

  8. 洛谷 P1379 八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...

  9. 洛谷 P1379 八数码难题 题解

    我个人感觉就是一道bfs的变形,还是对bfs掌握不好的人有一定难度. 本题思路: 大体上用bfs搜,用map来去重,在这里只需要一个队列,因为需要较少步数达到的状态一定在步数较多的状态之前入队列. # ...

随机推荐

  1. swift,demo,ios8

    swift交流群:342581988,欢迎增加. 刚刚写的小 demo.搞得还是不是太好.请大家拍砖! 能够直接复制执行 import UIKit class ViewController: UIVi ...

  2. HDU 2955 Robberies(01背包变形)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. MYSQL使用inner join 进行 查询/删除/修改示例

    代码如下: --查询 SELECT tp.tp_id, tp.tpmc, tp.leveid, tp.tpdz, tp.jgm, tp.scsj, tp.pbzyid, tp.ksbfsj, tp.j ...

  4. sublime 中配置 python 运行

    运行是按快捷键 ”ctrl + B“ Preferences->Browse Packages->Python->Python.sublime-build 编辑这个文件.   修改成 ...

  5. Snail—iOS网络学习之得到网络上的数据

    在开发项目project中,尤其是手机APP,一般都是先把界面给搭建出来.然后再从网上down数据 来填充 那么网上的数据是怎么得来的呢,网络上的数据无非就经常使用的两种JSON和XML 如今 大部分 ...

  6. MapReduce算法形式三:cleanup

    案例三:cleanup 其实这个案例可以不用写这么复杂,不用cleanup也能写,但是为了,突显,突显,突显(重要的事说四遍)cleanup的重要性,琢磨了半天,恩,这样写既可以突显cleanup又显 ...

  7. debian配置集锦

    1 关闭蜂鸣 在/etc/bash.bashrc中加入下面的行: setterm -blength=0 2 debian bash路径显示太长 将.bashrc中的 else PS1='${debia ...

  8. ORA-01031: insufficient privileges 解决办法

    sysdba不能远程登录这个也是一个很常见的问题了. 碰到这样的问题我们该如何解决呢? 我们用sysdba登录的时候,用来管理我们的数据库实例,特别是有时候,服务器不再本台机器,这个就更是有必要了. ...

  9. 使用Qt发送HTTPS请求

    示例代码: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QNetworkAc ...

  10. 在Windows上使用libcurl发起HTTP请求

    curl下载地址https://curl.haxx.se/download.html 当前最新版本为7.61.0 将下载的curl-7.61.0.zip解压,得到curl-7.61.0 在目录curl ...