洛谷 - 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记录一下每一个点的方案数,分几种情况讨论一下转移, ...
随机推荐
- BUPT复试专题—网络传输(2014网研)
题目描述 网络的高效互联与智能传输是提升海量用户服务请求映射效率的重要措施.在这个任务中,你需耍在最小的传输时间内,将数据源传输到指定的网络节点中.我们给定的网络一共包含N个节点,其中节点1为数据源. ...
- 【转】Code Your Own PHP MVC Framework in 1 Hour
原文: https://www.codeproject.com/Articles/1080626/Code-Your-Own-PHP-MVC-Framework-in-Hour --------- ...
- CCPhysicsSprite
#ifndef __PHYSICSNODES_CCPHYSICSSPRITE_H__ #define __PHYSICSNODES_CCPHYSICSSPRITE_H__ #include " ...
- mybatis 动态curd
xml <select id="selectByCondition" parameterType="com.oracle.pojo.Student" re ...
- 基于bootstrap_网站汇总页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 查看windows下port占用
有时假tomcat启动时提示port被占用.能够用netstat -aon|findstr "8080" 命令查找到,然后 用 taskkill /f /pid pid号 强制ki ...
- Jquery-easyui的默认图标的使用,以及如何添加自己想要的图标
easyui的默认图标有以下这些: .icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background: ...
- Python开发【第3节】【Python分支结构与循环结构】
1.流程控制 流程: 计算机执行代码的顺序就是流程 流程控制: 对计算机代码执行顺序的管理就是流程控制 流程分类: 流程控制共分为3类: 顺序结构 分支结构/选择结构 循环结构 2.分支结构(if. ...
- C++的string连接(a = a + b 与 a += b)
大一学习C语言的时候,书上就写着a = a + b与 a += b等价,但是提倡用后者. 在CSDN上也看到一个关于a+=b和a=a+b的区别的帖子,大概内容如下:------------------ ...
- iOS之Prefix.pch
本文转载至 http://blog.csdn.net/lvxiangan/article/details/21325093 Prefix.pch的作用和用法 Hello World_Prefix. ...