POJ 2965 The Pilots Brothers' refrigerator (枚举)

网友投稿 271 2022-08-31

POJ 2965 The Pilots Brothers' refrigerator (枚举)

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

61 11 31 44 14 34 4

题意

把题目中给出的状态图,全部翻转成 ​​-​​ 的状态

每次翻转一个,它所在的行和列都要进行翻转

问最小翻转次数,同时输出翻转路径。

思路

和 ​​POJ 1753​​ 一样,只不过在dfs的时候用一个栈或者双端队列存储当前的路径,找到答案之后输出。

AC 代码

#include #include#include#include#include#include#includeusing namespace std;bool bits[16];bool flag=false;dequesk;void rese(int n) //翻转{ int x=n/4,y=n%4; for(int i=0; i<4; i++) bits[x*4+i]=!bits[x*4+i]; for(int i=0; i<4; i++) if(i!=x) bits[i*4+y]=!bits[i*4+y];}bool jud() //判断是否一致{ bool ini=bits[0]; if(!ini)return false; for(int i=1; i<16; i++) if(ini!=bits[i]) return false; return true;}void solve(int maxx,int now,int step){ if(maxx==step) //翻转完最后一枚棋子 { if(jud()) //满足状态 { flag=true; printf("%d\n",maxx); for(int i=0; i>str[i]; for(int j=0; j<4; j++) bits[i*4+j]=(str[i][j]=='-')?1:0; } for(int i=0; i<=16; i++) //i 为要翻转的棋子个数 solve(i,0,0); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:玩尽营销花样后,Ulike的“居心”已无处藏身!
下一篇:POJ 2299 Ultra-QuickSort (归并排序、逆序数)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~