洛谷 - 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个大 ...
随机推荐
- 子结点childNodes
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- HDU 4334 Trouble(哈希|线性查找)
给定五个集合.问是否能从五个集合各取一个元素,使得元素之和为0. 这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份. 这里说还有一种. 对于这五个集合分为三组.1 ...
- net spy memcached 使用demo
package memcached; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.mem ...
- ORACLE 8i 遇到报错:ORA-01631: max # extents (505) reached in table
近期在客户的一个8i生产库上使用statspack.发现alert中有报错: Mon Jun 16 13:17:52 2014 Errors in file /oracle/8.1.7/admin/p ...
- 杭电1596find the safest road(spfa)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 基于mac系统的apacheserver的使用流程
打开终端.输入下面命令:sudo apachectl start 此时Apache已经开启.在浏览器中输入本地ip地址能够看到it works! 打开前往----电脑------Macintosh H ...
- MySQL优化之——触发器
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46763665 触发器是一个特殊的存储过程,不同的是存储过程要用CALL来调用,而触 ...
- HDOJ1002
#include <iostream>#include<stdio.h> using namespace std;int main(){ int T, count_T; cha ...
- SWT 安装
下载地址: http://www.eclipse.org/windowbuilder/download.php 看到页面如下:选择图片标记的 3.7 (Indigo) update site lin ...
- !important的用法(IE6 兼容的解决方法)
我们知道,CSS写在不同的地方有不同的优先级, .css文件中的定义 < 元素style中的属性,但是如果使用!important,事情就会变得不一样. 首先,先看下面一段代码: <!DO ...