The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4
题意:有一个4*4冰箱开关,+为关,-为开,给你一个起始状态,每次可以选择一个开关,将它连同它所在的行列一起翻转状态,求最小次数,并输出操作
题解:前一道题可以爆枚,但毕竟期望标程是bfs或dfs,所以本题我来了一份bfs
首先
因为只有4*4的大小,每个坐标(x,y)上只有两种情况,所以可以考虑把十六个位置压成一个数
然后要反转哪个位置,就只需要给该位置xor上一个数就可以了
如果要反转一堆呢?
那只需要xor这一堆位置的二进制值即可
所以先预处理好改变(1,1)-(4,4)每个点要xor上的值(推荐还是用程序预处理,手算错误较高)
然后bfs状态
至于为什么要bfs呢?
因为bfs有着优越性,因为进入队列的状态按步数从小到大排,可以保证搜到的第一个解一定是最小的
终止条件为所有的开关都被翻好了,即sta为0或65535(随自己定义的关为1或开为1而定)
至于记录路径吗emmmm和一道floyd求最小环的记录路径很像
记录转移过来的状态,然后追溯回去,依次输出。
代码:
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fa puts("fuck");
using namespace std; struct node
{
int state,s;
node(int a,int b)
{
state=a;
s=b;
}
}; struct node2
{
int pa;
int i;
} path[]; int vis[],sum,map[][]; int change[]=
{
,,,,
,,,,
,,,,
,,,
}; void print(int k)
{
if(path[k].pa==sum)
{
cout<<path[k].i/+<<" "<<path[k].i%+<<endl;
return;
}
print(path[k].pa);
cout<<path[k].i/+<<" "<<path[k].i%+<<endl;
} void bfs()
{
memset(vis,,sizeof(vis));
vis[sum]=;
queue<node> q;
q.push(node(sum,));
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.state==)
{
cout<<t.s<<endl;
print(t.state);
return ;
}
for(int i=; i<; i++)
{
int tsta=t.state^change[i];
int ts=t.s+;
if(!vis[tsta])
{
q.push(node(tsta,ts));
vis[tsta]=;
path[tsta].pa=t.state;
path[tsta].i=i;
}
}
}
} int main()
{
char c;
int cnt=;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
cin>>c;
if(c=='-')
{
map[i][j]=;
sum+=(<<cnt);
}
else
{
map[i][j]=;
}
cnt--;
}
}
bfs();
return ;
}


POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)的更多相关文章

  1. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

  2. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  3. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

      The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 151 ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16868 ...

  6. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  7. POJ 2965 The Pilots Brothers' refrigerator (DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15136 ...

  8. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  9. POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】

    题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...

随机推荐

  1. 蓝桥杯 算法训练 ALGO-34 纪念品分组

    算法训练 纪念品分组   时间限制:1.0s   内存限制:256.0MB 问题描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值 相对均衡,他要把购 ...

  2. druid抛出异常:javax.management.InstanceAlreadyExistsException: com.alibaba.druid:type=DruidDataSource,id=xxx

    第一种结论 (参考: https://www.cnblogs.com/youzhibing/p/6826767.html): 问题产生的根本原因还真是:同一实例被启动了两遍,Path为/SLBAdmi ...

  3. 安装用于跨平台移动开发的 Visual C++

    安装用于跨平台移动开发的 Visual C++ Visual Studio 2015   若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 20 ...

  4. WebSocket :Nginx+WebSocket内部路由策略推送服务器的实现(附可生产环境应用代码)

    1.项目背景 前几天写了一篇WebSocket推送的博客:WebSocket :用WebSocket实现推送你必须考虑的几个问题 支持的连接数大概几千个,具体数量依赖于tomcat能并发的线程数,但很 ...

  5. Java-Maven-Runoob:Maven 快照(SNAPSHOT)

    ylbtech-Java-Maven-Runoob:Maven 快照(SNAPSHOT) 1.返回顶部 1. Maven 快照(SNAPSHOT) 一个大型的软件应用通常包含多个模块,并且通常的场景是 ...

  6. C# 通用类型转换方法

    在程序开发过程中经常需要进行数据的类型转换,而且如果一个字段的类型改成另一个类型时,所有相关的类型转换的地方都要跟着修改,不但造成了很多重复转换的代码而且修改字段类型时额外修改相关转换代码的工作量也很 ...

  7. C++加载位图跟SOCKET通信的编写

    //将屏幕信息转换为位图结构 CDC *pDeskDC=GetDesktopWindow()->GetDC();//获取桌面的额画图对象 ; ; CDC memDC;//定义一个内存画布 mem ...

  8. Visual C++ Samples-------------Code Project

    https://msdn.microsoft.com/en-us/library/hyds2fy1(v=vs.80).aspx

  9. entityframework.extended 配置mysql

    entityframework.extended 这个是个很好的扩展,不过由于默认是配置成MSSQL的,今天在github上面 看到一个解决方案,亲测可用,下面贴代码 1.在DbContext 修改默 ...

  10. asp:GridView控件的使用

    使用asp:GridView显示一个统计的表格 cs样式: <style>        table.gridview_m        {            border-colla ...