洛谷 - P2730 - 魔板 Magic Squares - bfs
写状态转移弄了很久,老了,不记得自己的数组是怎么标号的了。
#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的更多相关文章
- 洛谷 P2730 魔板 Magic Squares 解题报告
		
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
 - 洛谷 P2730 魔板 Magic Squares
		
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
 - [洛谷P2730] 魔板 Magic Squares
		
洛谷题目链接:魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都 ...
 - 洛谷P2730 魔板 [广搜,字符串,STL]
		
题目传送门 魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有 ...
 - P2730 魔板 Magic Squares
		
题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...
 - P2730 魔板 Magic Squares (搜索)
		
题目链接 Solution 这道题,我是用 \(map\) 做的. 具体实现,我们用一个 \(string\) 类型表示任意一种情况. 可以知道,排列最多只有 \(8!\) 个. 然后就是直接的广搜了 ...
 - 哈希+Bfs【P2730】 魔板 Magic Squares
		
没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...
 - 【简●解】 LG P2730 【魔板 Magic Squares】
		
LG P2730 [魔板 Magic Squares] [题目背景] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 ...
 - [USACO3.2]魔板 Magic Squares
		
松下问童子,言师采药去. 只在此山中,云深不知处.--贾岛 题目:魔板 Magic Squares 网址:https://www.luogu.com.cn/problem/P2730 这是一张有8个大 ...
 
随机推荐
- ScSPM
			
Linear Spatial Pyramid Matching using Sparse Coding for Image Classification (CVPR'09) 稀疏编码系列: (一)-- ...
 - BUPT复试专题—中位数(2014-2)
			
题目描述 给定一个长度为N的非降数列,求数列的中位数.中位数:当数列的项数N为奇数吋,处于中间位置的变最值即为中位数:当N 为偶数时,中位数则为处于中间位置的两个数的平均数. 输入 输入数据第一行 ...
 - php编译安装后,加扩展模块
			
1.进入php源码包中,找到需要安装的扩展模块目录. cd /root/php-5.6.26/ext/mbstring 2.在扩展模块目录,运行phpize程序. /usr/local/bin/php ...
 - PS 如何把大嘴变小嘴
			
Photoshop整容教程:让MM美唇大嘴变小嘴 2009-06-17 14:15作者:佚名出处:天极网软件频道责任编辑:王健 下面就开始实际操作了. 1.首先从Photosh ...
 - CF 568A(Primes or Palindromes?-暴力推断)
			
A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...
 - typedef和#define的差别——————【Badboy】
			
typedef 和#define 都经常使用来定义一个标识符及keyword的别名.但他们之间有关键的差别. typedef 是语言编译过程的一部分; #define是宏定义语句,它本身并不在编译过程 ...
 - c++ string 之 find_first_not_of 源码
			
一:实现之前先说一所find_first_not_of姊妹函数() (1)find_first_of(string &str, size_type index = 0):(find_first ...
 - Mono 和 .NET Core比翼双飞
			
大家好,今天给大家分享.NET 蓝图之下的Mono和.NET Core 话题,微软在Build 2019 大会上给.NET 做了一个五年规划,所以分享的主题就是<Mono和.NET Core 比 ...
 - appium部分api
			
转自:http://www.aichengxu.com/view/41510 使用的语言是java,appium的版本是1.3.4,java-client的版本是java-client-2.1.0,建 ...
 - Python中文问题研究
			
我曾经在深入浅出java中文问题系 列中研究过java的中文问题,现在中文问题已经不再羁绊我在java世界中漫游的脚步了.最近,对Python产生了浓厚的兴趣,谁知道跟中文问题这个 老朋友又一次不期而 ...