题意:有个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. 【Eclipse】Eclipse上如何集成SVN插件

    SVN的安装 下载svn插件site.zip工具包 解压site工具包,将内容放置到eclipse\dropins目录下 重启 eclipse 打开 SVN 窗口 提示:如果 SVN 图标出现红色方块 ...

  2. 【jsp】Servlet与jsp之间的传值

    Servlet与JSP 之间的传值有两种情况:JSP -> Servlet, Servlet -> JSP. 通过对象 request和 session (不考虑 application) ...

  3. SQL Server 2008中SQL之WaitFor

    SQL Server 2008中SQL应用系列--目录索引 在SQL Server 2005以上版本中,在一个增强的WaitFor命令,其作用可以和一个job相当.但使用更加简捷. 看MSDN: ht ...

  4. MySQL-innodb_flush_log_at_trx_commit

    有效取值为0.1.2.建议设置为1 -1:执行commit的时将重做日志缓冲区同步写到磁盘,即伴有fsync调用 -2:执行commit的时将重做日志异步写到磁盘,即先写到文件系统的缓冲中(因为文件系 ...

  5. netty服务端实现心跳超时的主动拆链

    一.服务器启动示例: public class MySocketServer { protected static Logger logger = LoggerFactory.getLogger(My ...

  6. Java 8 forEach examples遍历例子

    1. forEach and Map 1.1 Normal way to loop a Map. Map<String, Integer> items = new HashMap<& ...

  7. ReferenceError: weakly-referenced object no longer exists Python kafka

    Python存入kafka报错,ReferenceError: weakly-referenced object no longer exists. Exception in thread 14: p ...

  8. Guid ToString 格式

    转自 http://www.cnblogs.com/greenerycn/archive/2010/04/25/guid_tostring_format.html 在日常编程中,Guid是比较常用的, ...

  9. 关系型数据库性能测试参考指标----SQL Server

    注:以下指标取自SQL Server自身提供的性能计数器. [@more@] SQL Server 指标名称 指标描述 指标范围 指标单位 1.SQL Server中访问方法(Access Metho ...

  10. linux上NFS性能参数

    linux nfs客户端对于同时发起的NFS请求数量进行了控制,若该参数配置较小会导致IO性能较差,查看该参数: cat /proc/sys/sunrpc/tcp_slot_table_entries ...