神奇bug,没有记录pre就show了,找了1个小时。

#include <bits/stdc++.h>
using namespace std;
#define ll long long int C=; 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;
} d,dt,data[]; queue<dat> q; void showbit(int s){
string str;
for(int i=;i<;i++){
str=char((s&)+'')+str;
s>>=;
if(i%==)
str='\n'+str;
}
cout<<str<<endl;
} void show(dat d)
{
//cout<<"show"<<endl;
stack<int> s;
s.push(d.cur);
while()
{
if(d.pre==-)
break;
s.push(d.pre);
d=data[d.pre];
} printf("%d",s.size()-); int cur=s.top(); while(!s.empty())
{
int nex=s.top();
//showbit(cur); s.pop(); for(int i=;i>=;i--){
if(((cur>>i)&)!=((nex>>i)&)){
//cout<<bitset<32>(cur)<<endl;
//cout<<bitset<32>(nex)<<endl<<endl;
int t=-i;
printf("%d%d",(t+)/,(t%)==?:t%);
}
}
printf("\n");
cur=nex;
} //showbit(cur);
} int t; inline bool found(int code)
{
return (code==t);
} void bfs(int s)
{
memset(data,-,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(); //cout<<bitset<32>(d.cur)<<endl; for(int i=;i<;i++){
for(int j=i+;j<;j++){
if((abs(i-j)==||((i/==j/)&&(abs(i-j)==)))&&(((d.cur>>i)&)!=((d.cur>>j)&))){
//cout<<"i="<<i<<" j="<<j<<endl;
dt.cur=(d.cur^((<<i)^(<<j)));
/*if(bitset<32>(d.cur).count()!=8&&bitset<32>(dt.cur).count()!=8){
cout<<bitset<32>(d.cur)<<endl;
cout<<bitset<32>(dt.cur)<<endl;
}*/ if(data[dt.cur].cur==-){
dt.pre=d.cur;
data[dt.cur]=dt;
q.push(dt);
if(found(dt.cur)){
show(dt);
return;
}
}
}
}
}
}
} int main()
{
int a[];
for(int i=; i<; i++)
{
scanf("%1d",&a[i]);
} int s=encode(a); /*printf("s=%d\n",s);
cout<<bitset<32>(s)<<endl; decode(s,a);
for(int i=0; i<16; i++)
{
printf("%d%c",a[i]," \n"[i%4==3]);
}*/ for(int i=; i<; i++)
{
scanf("%1d",&a[i]);
}
t=encode(a); /*printf("t=%d\n",t);
cout<<bitset<32>(t)<<endl; decode(t,a);
for(int i=0; i<16; i++)
{
printf("%d%c",a[i]," \n"[i%4==3]);
}*/ bfs(s);
}

洛谷 - P1225 - 黑白棋游戏 - bfs的更多相关文章

  1. 洛谷 - P2578 - 九数码游戏 - bfs

    https://www.luogu.org/problemnew/show/P2578 一个挺搞的东西,用康托展开做记忆化搜索可以少一个log的查询. #include <bits/stdc++ ...

  2. 黑白棋游戏 (codevs 2743)题解

    [问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...

  3. 用Dart写的黑白棋游戏

    2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...

  4. [CareerCup] 8.8 Othello Game 黑白棋游戏

    8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. Wh ...

  5. 洛谷P1118 数字三角形游戏

    洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直 ...

  6. 洛谷P1274-魔术数字游戏

    Problem 洛谷P1274-魔术数字游戏 Accept: 118    Submit: 243Time Limit: 1000 mSec    Memory Limit : 128MB Probl ...

  7. 洛谷P1288 取数游戏II(博弈)

    洛谷P1288 取数游戏II 先手必胜的条件需要满足如下中至少 \(1\) 条: 从初始位置向左走到第一个 \(0\) 的位置,经过边的数目为偶数(包含 \(0\) 这条边). 从初始位置向右走到第一 ...

  8. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  9. 洛谷 1144 最短路计数 bfs

    洛谷1144 最短路计数 传送门 其实这道题目的正解应该是spfa里面加一些处理,,然而,,然而,,既然它是无权图,,那么就直接bfs了,用一个cnt记录一下每一个点的方案数,分几种情况讨论一下转移, ...

随机推荐

  1. Android新手入门2016(7)--布局

    布局,这个在服务端变成是没有的,也是服务端的人学习client的一道坎吧. 曾经用cocos2d-x写小游戏的时候就是这个非常难懂,或者能用,可是理解不多的话,非常难写出好的布局,难以适合商业化的应用 ...

  2. Git多账号登陆

        最近工作上遇到了使用git+repo的情况,需要用公司的邮箱和账号名重新申请ssh公私密钥,而我本身在github上也有一些开源项目,这里就是记录一下我是如何实现git多账号登陆的.   取消 ...

  3. WCF 内存入口检查失败 Memory gates checking failed

    在做JC系统时,出现这样的错误: 出现该错误信息的原因是因为WCF服务激活之前,系统应该具有的最小内存量不足config文件中设置的百分比.我是在本机调试的时候出现的. 解决方法:        关闭 ...

  4. find the longest of the shortest (hdu 1595 SPFA+枚举)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. LeetCode_3Sum Closest

    一.题目 3Sum Closest Total Accepted: 32191 Total Submissions: 119262My Submissions Given an array S of  ...

  6. Linux下VLAN功能的实现 (转)

    1.Linux网络栈下两层实现 1.1简介     VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linux中网络栈下两层的实现,再去看如何把VLAN这个功能附加上去.下两层涉及到具体的硬件 ...

  7. Android 实例解说加入本地图片和调用系统拍照图片

    在项目的开发过程我们离不开图片.而有时候须要调用本地的图片,有时候须要调用拍照图片.同一时候实现拍照的方法有两种,一种是调用系统拍照功能.还有一种是自己定义拍照功能. 而本博文眼下仅仅解说第一种方法, ...

  8. add time to file name

    add time to file name echo 123 > $(date +"%Y%m%d_%H%M%S").now; mv /mnt/mongodb_data/dat ...

  9. strong and weak 强引用和弱引用的差别

    (weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放  ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 .这个对象将被释放,全部剩余 ...

  10. navcat for mysql 连接远程数据库 教程

    1.首先进入数据库: mysql -uroot -p 2.然后打开数据库设置远程连接权限: mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'ID ...