The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15136   Accepted: 5660   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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; char str[][]; int change[]={ , , ,, ,,,
,, ,,,, , , }; int vis[];
int path[]; struct node{
int status;
int step;
}; void BFS(int curstate){
queue<node> q;
while(!q.empty())
q.pop();
node cur,next;
cur.status=curstate, cur.step=;
vis[cur.status]=;
q.push(cur);
while(!q.empty()){
cur=q.front();
q.pop();
for(int i=;i<;i++){
next.status=cur.status^change[i];
next.step=cur.step+;
if(!vis[next.status]){
vis[next.status]=;
path[next.status]=i;
q.push(next);
}
if(next.status==){
printf("%d\n",next.step);
int k=next.status;
while(k!=curstate){
printf("%d %d\n",path[k]/+,path[k]%+);
k=k^change[path[k]];
}
return ;
}
}
}
}
/*
int num[16]; //即求change[]的方法 void init(){
for(int i=0;i<16;i++){
num[i]=0;
for(int j=0;j<16;j++)
if((i/4==j/4) || (i%4==j%4))
num[i]=(num[i]<<1)+1;
else
num[i]<<=1;
}
}
*/
int main(){ //freopen("input.txt","r",stdin);
/*
init();
for(int i=0;i<16;i++)
printf("%d ",num[i]);
printf("\n");
*/
while(~scanf("%s",str[]+)){
for(int i=;i<;i++)
scanf("%s",str[i]+);
int status=;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
status<<=;
if(str[i][j]=='-')
status |= ;
}
if(status==){
printf("0\n");
continue;
}
memset(vis,,sizeof(vis));
BFS(status);
}
return ;
}
4 4

Source

Northeastern Europe 2004, Western Subregion
 
 
 

POJ 2965 The Pilots Brothers' refrigerator (DFS)的更多相关文章

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

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

  2. POJ 2965 The Pilots Brothers' refrigerator (暴力枚举)

    https://vjudge.net/problem/POJ-2965 与poj-1753相似,只不过这个要记录路径.poj-1753:https://www.cnblogs.com/fht-lito ...

  3. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

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

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

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

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

  6. The Pilots Brothers' refrigerator(dfs)

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

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

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

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

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

  9. POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)

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

随机推荐

  1. js读取json方法

    json也是对象,可以直接使用对象调用 var json = {contry:{area:{man:"12万",women:"10万"}}}; json.con ...

  2. Guava Files 源码分析(一)

    Files中的工厂 Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupp ...

  3. 【BZOJ】【1014】【JLOI2008】火星人prefix

    Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...

  4. HttpMessageConverter

    HttpMessageConverter<T>是Spring3的一个重要接口,它负责将请求信息转换为一个对象(类型为T),将对象(类型为T)输出为响应信息. DispatcherServl ...

  5. Best Time to Buy and Sell Stock II leetcode java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. Debug时检测到Loaderlock的解决办法

    昨天遇到了Loaderlock的问题. 出错信息为:检测到LoaderLock,正试图在OS加载程序锁内执行托管代码,不要尝试在DllMain或映像初始化函数内运行托管代码,这样会导致应用程序挂起. ...

  7. linux CentOS7 安装scala

    1.打开terminal ,进入当前用户路径: cd /home/sks 2.下载Scala2.11 wget https://downloads.lightbend.com/scala/2.11.8 ...

  8. Android4.2.2 Gallery2源码分析(4)——GLCanvas.java

    首先申明,找到这个类是在GLRootView.java中发现的线索.这是一个接口,源码中对该接口作了详细的说明: // // GLCanvas gives a convenient interface ...

  9. Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...

  10. cognos report同比环比以及默认为当前月分析

    现在的需求是按月份分析不同时期的余额数据,.(报表工具:cognos report:建模工具:FM) ------------------------------------------------- ...