神奇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. BUPT复试专题—图像压缩存储(2015)

    题目描述 以二维数组表示图像,其值只有0.1两种,寻找两幅图像中最大的相同方阵   输入 第一行输入一个n,接下来的2n行输入两个n*n数组,寻找一个最大的m*m子区域,使得两个数组在该子区域完全相同 ...

  2. BUPT复试专题—串查找(?)

    https://www.nowcoder.com/practice/a988eda518f242c29009f8620f654ede?tpId=67&tqId=29642&rp=0&a ...

  3. Yii Criteria常用方法(select,join,where,日期,)

    $criteria = new CDbCriteria;  //select $criteria->select = '*';//默认* $criteria->select = 'id,n ...

  4. 【Unity 3D 游戏开发】Unity3D 入门 - 工作区域介绍 与 入门演示样例

    一. 工作区域具体解释 1. Scence视图 (场景设计面板) scence视图简单介绍 : 展示创建的游戏对象, 能够对全部的游戏对象进行 移动, 操作 和 放置; -- 演示样例 : 创建一个球 ...

  5. spark0.9.1集群模式执行graphx測试程序(LiveJournalPageRank,新增Connected Components)

    spark最新版公布了.之前的版本号就已经集成了graphx,这个版本号还改了一些bug. 我做了简单測试,只是网上关于集群模式执行spark资料太少了,仅仅有关于EC2(见參考资料1)的.可是还非常 ...

  6. HTTP请求中带有特殊字符"|",返回400错误

    Java平台,服务器是Tomcat8,前端ajax访问服务器时,F12返回400错误,经分析,URL地址中get传参值里面含有“|“, Invalid character found and RFC ...

  7. 使用iconv的包装类CharsetConverter进行编码转换的示例

    GitHub地址https://github.com/BuYishi/charset_converter_test charset_converter_test.cpp #include <io ...

  8. Android 返回键的处理

    多网友不明确怎样在Android平台上捕获Back键的事件.Back键是手机上的后退键,一般的软件不捕获相关信息可能导致你的程序被切换到后台.而回到桌面的尴尬情况,在Android上有两种方法来获取该 ...

  9. linux shell 突破

    targetDate=$(cat maxdayid); targartMonth=${targetDate::}; targartYear=${targetDate::}; echo $targart ...

  10. 恢复MySQL数据库删除的数据

    在日常运维工作中,对于数据库的备份是至关重要的!数据库对于网站的重要性使得我们对 MySQL 数据库的管理不容有失!然而是人总难免会犯错误,说不定哪天大脑短路了,误操作把数据库给删除了,怎么办? 下面 ...