隐藏页面特效

POJ 2965 The Pilots Brothers' refrigerator

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30872   Accepted: 11961   Special Judge

Description

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的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的同行同列的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子朝上都是白的。

输出最少数,然后依次输出要翻转的棋子的位置(任意一种可行方案皆可)

 

【分析】

除了压缩的状态不同,其余做法与POJ 1753 Flip Game(点击查看思路)完全相同。

 

【代码】

#include<cstdio> #include<iostream> #define debug(x) cerr<<#x<<" "<<x<<'\n'; using namespace std; const int N=105; char s[N];int now,state[]={63624,62532,61986,61713,36744,20292,12066,7953,35064,17652,8946,4593,34959,17487,8751,4383}; int top,st[N]; inline void Init(){ for(int i=0;i<4;i++){ scanf("%s",s); for(int j=0;j<4;j++){ now<<=1; now|=s[j]=='+'; } } } inline bool check(){ return !now; } inline void flip(int t){ now^=state[t]; } bool find(int n,int i){ if(!n) return check(); for(;i<16;i++){ if(16-i<=n-1) break; flip(i);st[++top]=i; if(find(n-1,i+1)) return 1; flip(i);top--; } return 0; } inline void Solve(){ for(int i=0;i<=16;i++){ top=0; if(find(i,0)){ printf("%d\n",i); for(int j=1,x,y;j<=top;j++){ x=st[j]/4+1,y=st[j]%4+1; printf("%d %d\n",x,y); } return ; } } puts("Impossible"); } int main(){ Init(); Solve(); return 0; }
 


 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/10367214.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(147)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2017-02-12 LA 3882 And Then There Was One[约瑟夫问题的变形]
2017-02-12 cf-341C Iahub and Permutations
2017-02-12 Times[2017-01-25at JiNan]
2017-02-12 2017-01-20_dp测试
2017-02-12 4514: [Sdoi2016]数字配对
点击右上角即可分享
微信分享提示