洛谷 - P1225 - 黑白棋游戏 - bfs
神奇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的更多相关文章
- 洛谷 - P2578 - 九数码游戏 - bfs
https://www.luogu.org/problemnew/show/P2578 一个挺搞的东西,用康托展开做记忆化搜索可以少一个log的查询. #include <bits/stdc++ ...
- 黑白棋游戏 (codevs 2743)题解
[问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...
- 用Dart写的黑白棋游戏
2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...
- [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 ...
- 洛谷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\) 这条边). 从初始位置向右走到第一 ...
- 「区间DP」「洛谷P1043」数字游戏
「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...
- 洛谷 1144 最短路计数 bfs
洛谷1144 最短路计数 传送门 其实这道题目的正解应该是spfa里面加一些处理,,然而,,然而,,既然它是无权图,,那么就直接bfs了,用一个cnt记录一下每一个点的方案数,分几种情况讨论一下转移, ...
随机推荐
- 转: 环信联合创始人:App主流反垃圾服务难点和技术实现全解析
转:http://science.china.com.cn/2016-03/24/content_8659834.htm 发布时间: 2016-03-24 13:15:02 | 来源: 全球财经网 ...
- SolidEdge 如何绘制局部视图 局部放大图
创建局部视图(局部放大图),先选择要创建局部放大图的视图,然后绘制一个小圆,然后绘制一个大圆即可. 如果要绘制不规则形状的局部放大图,则点击了局部放大图之后,点击绘制草图的按钮 随后可以用相切 ...
- bootstrap之ScrollTo
ScrollTo package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiObject; ...
- Intel Active Management Technology
http://en.wikipedia.org/wiki/Intel_Active_Management_Technology Intel Active Management Technology F ...
- AVL树,红黑树,B-B+树,Trie树原理和应用
前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...
- Android的onMeasure方法
在Android开发中,当Android原生控件不能满足我们的需求的时候,就需要自定义View.View在屏幕上绘制出来先要经过measure(计算)和layout(布局). 什么时候调用onMeas ...
- jquery一个比较好的轮播图jQuery.kinMaxShow介绍
kinMaxShow API 可选参数以及详解 kinMaxShow 主参数详解 参数名称 默认值 简单释义 height 500 [整型 (单位:像素)]焦点图高度,必须设置 缺省则启用默认高度 5 ...
- HBase在滴滴出行的应用场景和最佳实践
摘要: 主要介绍了HBase和Phoenix在滴滴内部的一些典型案例.文章已在CSDN极客头条和<程序员>杂志发表,应朋友邀请,分享到云栖社区,希望给大家带来启发和帮助. 背景 对接业务类 ...
- Java Management Extensions (JMX) Flume
Lesson: Overview of the JMX Technology (The Java™ Tutorials > Java Management Extensions (JMX)) h ...
- poj 2228 Naptime(DP的后效性处理)
\(Naptime\) \(solution:\) 这道题不做多讲,它和很多区间DP的套路一致,但是这一道题它不允许断环成链,会超时.但是我们发现如果这只奶牛跨夜休息那么它在不跨夜的二十四个小时里一定 ...