洛谷 - 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\) 这条边). 从初始位置向右走到第一 ...
随机推荐
- virtualenv 配置python3环境
virtualenv -p /usr/bin/python3 py3env source py3env/bin/activate pip install package-name
- [CSS3] Identify Interactive HTML Elements with CSS Link Pseudo Classes
The interactive pseudo-classes for links (and buttons) allow us to make sure the user knows what ele ...
- CMS - 认识目录
Tips:如果网页图片(文字)看不清,请按CTRL+鼠标滚轮 一个完整的小程序模板目录结构如下! 本章节给出的建议: 1.推荐使用flex布局 2.其它图片路径建议引入网络路径(tabBar不支持网络 ...
- weex splash页面
1.Splash.vue <!-- splash页面 --> <template> <div class="wrap" @focus="ro ...
- c++面试题总结(4)
一.找错题 试题1: void test1() { ]; "; strcpy( string, str1 ); } 试题2: void test2() { ],str1[]; int i; ...
- JS判断访问设备(userAgent)加载不同页面 JS判断客户端操作系统类型(platform)
//平台.设备和操作系统 var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platfor ...
- CF 535c Tavas and Karafs
Tavas and Karafs Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- openwrt procd 运行的一些log
void procd_inittab(void) { #define LINE_LEN 128 FILE *fp = fopen(tab, "r"); struct init_ac ...
- LeetCode(3)题解: Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest ...
- OpenCV2.3.1在CentOS6.5下的安装
安装的linux版本号是centos6.5.选择的是opencv2.3.1.不是非常新的版本号. 由于在安装opencv2.4.9的时候.make的过程中出现了问题. 一:安装依赖包 依赖包用yum安 ...