题意:有个4*4的开关,里面有着16个小开关

-+--
----
---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开。现在有一种操作,只要改变某个开关,那么这个开关的行列所在开关都会被改变
-+-- 问,要打开总开关至少要改变多少次开关?并输出改变开关的位置。 思路: 由于每个开关只有两种状态,那么对于这16个小开关,我们可以用2进制来压缩下,如果开关是打开的那么为'0',如果是关着的,那么为'1',如此,我们就可以从下到上,从右到左给这16个开关
标记状态,如果以某个点为中心,那么这个点的行列状态都压缩进去,遇到这个点,取反与不取反,然后一次广搜过去,再记忆下路径,结果就出来了......
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define M 1<<16
int t[20]={
63624,62532,61986,61713,
36744,20292,12066,7953,
35064,17652,8946,4593,
34959,17487,8751,4383,
};
structnode
{
int k;
int step;
};
structnode1
{
int father;
int x,y;
}s[(1<<17)];
//bool vist[(1<<17)];
int sum;
void print(int ans)
{
if(ans==sum)
return;
print(s[ans].father);
printf("%d %d\n",s[ans].x+1,s[ans].y+1);
}
void bfs(int ans)
{
queue<node>q;
node p;
p.k=ans;
p.step=0;
s[p.k].father=-10;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
if(p.k==0)
{
printf("%d\n",p.step);
print(p.k);
return;
}
for(int i=0;i<16;i++)
{
node p1;
p1.k=p.k^t[i];
p1.step=p.step+1;
if(s[p1.k].father==-1)
{
s[p1.k].father=p.k;
s[p1.k].x=i/4;
s[p1.k].y=i%4;
q.push(p1);
}
}
}
}
int main()
{
int ans=0,cnt=15;
for(int i=0;i<4;i++)
{
char ch[100];
scanf("%s",ch);
for(int j=0;j<4;j++)
{
if(ch[j]=='+')
{
ans|=(1<<cnt);
}
cnt--;
}
}
//printf("%d\n",ans);
sum=ans;
for(int i=0;i<(M);i++)
{
s[i].father=-1;
}
bfs(ans);
return 0;
}

poj2965(位运算压缩+bfs+记忆路径)的更多相关文章

  1. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  2. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  3. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

  4. HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash

    这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...

  5. poj1077(康托展开+bfs+记忆路径)

    题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...

  6. poj1753-Flip Game 【状态压缩+bfs】

    http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  7. POJ 1753 bfs+位运算

    T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...

  8. HDU5627--Clarke and MST (bfs+位运算)

    http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...

  9. POj 1753--Flip Game(位运算+BFS)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30669   Accepted: 13345 Descr ...

随机推荐

  1. 详解PV、UV、VV、IP及其关系与计算

    一.什么是PV? PV即Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量.用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数量 ...

  2. JAVA操作mysql(如何更加面向对象的操作数据库)

    既然谈到面向对象,所以,先把连接信息给搞个对象出来: public class DBInfo { private String driver; private String host; private ...

  3. idea上activiti插件的安装及使用

    最近做的东西需要用到activiti,做个笔记 首先下载activti插件- actiBPM File - settings-plugins-Browse-Repositories 搜索 actiBP ...

  4. springboot(五):spring data jpa的使用

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  5. HDU 4301 Divide Chocolate (DP + 递推)

    Divide Chocolate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Android Studio 常见问题汇总

    一.字体大小问题 在android studio的使用过程中没有发现类似于Eclipse中的font选项,调节字体大小方法如下: 1.File---- >Settings,找到Editor 2. ...

  7. FaceBook登陆API -- Login with API calls

    Login with API calls Related Topics Understanding sessions FBSession Error handling FBError FBLoginC ...

  8. cucumber java从入门到精通(1)初体验

    cucumber java从入门到精通(1)初体验 cucumber在ruby环境下表现让人惊叹,作为BDD框架的先驱,cucumber后来被移植到了多平台,有cucumber-js以及我们今天要介绍 ...

  9. RocketMQ os.sh 系统优化(CentOS)

    贴出源码中的优化脚本先: #!/bin/sh # # Execute Only Once # echo 'vm.overcommit_memory=1' >> /etc/sysctl.co ...

  10. Ubuntu 13.04开机亮度调节

    终于把我的T430换成Ubuntu,本来还打算等几天13.10,想想反正能升级,趁着101长假就抓紧换了吧~` 总体来说遇到的问题不是很多,可能是Thinkpad在Linux或者ubuntu的方面做的 ...