洛谷 - 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记录一下每一个点的方案数,分几种情况讨论一下转移, ...
随机推荐
- 赵雅智_Swift(2)_swift常量和变量
分号 Swift 并不强制要求你在每条语句的结尾处使用分号(;) 你打算在同一行内写多条独立的语句必需要用分号 let cat = "? ?? ? "; println(cat) ...
- 深入研究Clang(五) Clang Lexer代码阅读笔记之Lexer
作者:史宁宁(snsn1984) Clang的Lexer(词法分析器)的源代码的主要位置例如以下: clang/lib/Lex 这里是基本的Lexer的代码: clang/include/cla ...
- android-----JNI中的log打印
1. 导入log头文件 在你使用的 .c/ .cpp 文件中 导入 log.h 头文件 #include<android/log.h> 2.在Android.mk 中 加上 LOCAL_L ...
- python实现的一个简单的网页爬虫
学习了下python,看了一个简单的网页爬虫:http://www.cnblogs.com/fnng/p/3576154.html 自己实现了一个简单的网页爬虫,获取豆瓣的最新电影信息. 爬虫主要是获 ...
- Spark Streaming性能优化系列-怎样获得和持续使用足够的集群计算资源?
一:数据峰值的巨大影响 1. 数据确实不稳定,比如晚上的时候訪问流量特别大 2. 在处理的时候比如GC的时候耽误时间会产生delay延迟 二:Backpressure:数据的反压机制 基本思想:依据上 ...
- eclipse svn org.tigris.subversion.javahl.ClientException: RA layer request failed
突然之间eclipse使用svn更新项目时报错,org.tigris.subversion.javahl.ClientException: RA layer request failed 网上搜的都是 ...
- System V 信号量使用相关函数
System V 信号量 在提到Posix 信号量时,指的是二值信号量或计数信号量,而System V信号量指的是入了计数信号量集 二值信号量:其值为0或1,类似于互斥锁,资源被锁住时为0,资源可用为 ...
- zoj1232Adventure of Super Mario(图上dp)
题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...
- scala快速学习笔记(一):变量函数,操作符,基本类型
为了用spark,先学下scala. 参考教程:http://meetfp.com/zh/scala-basic doc查询:http://docs.scala-lang.org 其它资料:http: ...
- Hihocoder #1527 : 快速乘法 DP
时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 在写代码时,我们经常要用到类似 x × a 这样的语句( a 是常数).众所周知,计算机进行乘法运算是非常慢的,所以我们需 ...