洛谷 - P2578 - 九数码游戏 - bfs
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的更多相关文章
- [ZJOI2005]九数码游戏(BFS+hash)
Solution 这题的话直接上BFS就可以了,因为要输出方案,所以我们要开一个pre数组记录前驱,最后输出就可以了. 对于状态的记录,一般都用哈希来存,但因为这道题比较特殊,它是一个排列,所以我们可 ...
- 洛谷 - P1379 - 八数码难题 - bfs
https://www.luogu.org/problemnew/show/P1379 #include <bits/stdc++.h> using namespace std; #def ...
- 洛谷 - P1225 - 黑白棋游戏 - bfs
神奇bug,没有记录pre就show了,找了1个小时. #include <bits/stdc++.h> using namespace std; #define ll long long ...
- LG2578 「ZJOI2005」九数码游戏 bfs
问题描述 LG2578 题解 用string+map去重. bfs即可. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace ...
- 万圣节后的早晨&&九数码游戏——双向广搜
https://www.luogu.org/problemnew/show/P1778 https://www.luogu.org/problemnew/show/P2578 双向广搜. 有固定起点终 ...
- [ZJOI2005]九数码游戏
[ZJOI2005]九数码游戏 题目描述 输入输出格式 输入格式: 输入文件中包含三行三列九个数,同行的相邻两数用空格隔开,表示初始状态每个方格上的数字.初始状态不会是目标状态. 输出格式: 如果目标 ...
- 洛谷P1118 数字三角形游戏
洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直 ...
- 洛谷P1274-魔术数字游戏
Problem 洛谷P1274-魔术数字游戏 Accept: 118 Submit: 243Time Limit: 1000 mSec Memory Limit : 128MB Probl ...
- 洛谷P1288 取数游戏II(博弈)
洛谷P1288 取数游戏II 先手必胜的条件需要满足如下中至少 \(1\) 条: 从初始位置向左走到第一个 \(0\) 的位置,经过边的数目为偶数(包含 \(0\) 这条边). 从初始位置向右走到第一 ...
随机推荐
- qt动画入门
Qt-4.6新增了Animation Framework(动画框架),让我们可以方便的写一些生动的程序. 不必像曾经的版本号一样,全部的控件都枯燥的呆在伟大光荣的QLayout里,或许它们可以唱个歌, ...
- SQLDMO注冊
在维护人事系统时.师姐给我们提出一个功能上有问题. 备份数据库时.报黄页.然后须要我们寻找原因,作出解决方式. 一開始我从原先在本机上公布的系统入手,发现没有出现故障.可是.当对程序进行调试时,就出现 ...
- 使用Axis2开发WebService
一.准备 1.下载Axis2.eclipse插件 axis2-1.6.2-war.zip: http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core ...
- css hack (ie6-ie9+)
IE6 css hack: 1. *html Selector {} /* Selector 表示 css选择器 下同 */ 2. Selector { _property: value; } /* ...
- 在docker里查看java进程
先使用命令查看docker的运行进程 docker ps [root@localhost logs]# docker ps CONTAINER ID IMAGE ...
- Java 三大特性:封装、继承、多态
一.封装: 对内 可封装自己的实现细节,使之信息隐藏不被其它类破坏: 对外 各个类之间分工明确, 其它类无需关心实现细节,类中细节发生改变,其它类也无需作出更改: 二.继承: 从已有的类中派生出新的类 ...
- FZU1977 Pandora adventure —— 插头DP
题目链接:https://vjudge.net/problem/FZU-1977 Problem 1977 Pandora adventure Accept: 597 Submit: 2199 ...
- Jackson 对象与json数据互转工具类JacksonUtil
1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...
- 合并table中某一列相邻的相同的行
合并table中某一列相邻的相同的行1. [代码]合并table中某一列相邻的相同的行 <!DOCTYPE html><html> <head> ...
- fuse的mount机制-流程及参数
在bbfs中,传递的参数有两个目录,fuse将一个目录挂载在另一个目录下. 在ssfs中,传递的参数只有一个目录(传递两个目录fuse会出错). 问题:那么fuse的mount机制到底需要几个目录参数 ...