The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16868   Accepted: 6393   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

本来用位运算+bfs,但是tle,再进行剪枝太麻烦,结果看到如此简单的算法,果然应该三思而后行。

//poj2965

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int mem[4][4];//用于储存翻转次数,如果是偶数,则相当于没有翻转

int main(){
    char ch;
    for(int x=0;x<4;x++){//按sample的顺序,x代表纵行,y是横列
        for(int y=0;y<=4;y++){
            ch=getchar();
            if(ch=='+'){//如果想只翻转这一个点而不改变其他,需要将该行该列以及该点本身各自翻转一次.,有点像魔方
                for(int i=0;i<4;i++){//翻转该列
                    mem[i][y]++;//翻转一次相当于自增1
                }
                for(int j=0;j<4;j++){//翻转该行
                    mem[x][j]++;
                }
            mem[x][y]++;
            }
        }
    }
    int ans=0;
    string str;
    for(int x=0;x<4;x++){
        for(int y=0;y<4;y++){
            int index=mem[x][y];
            if((index/2)*2!=index){
                    ans++;//先统计需翻转的个数
                    str+=('1'+x);//避免再统计一次,使用了c++字符串容器
                    str+=32;//空格
                    str+='1'+y;
                    str+='\n';
            }
        }
    }
    printf("%d\n",ans);
    cout<<str;
    return 0;
}

POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1的更多相关文章

  1. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

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

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

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

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

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

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

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

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

  6. 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 ...

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

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

  8. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

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

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

随机推荐

  1. oracle 索引失效原因及解决方法

    oracle 索引失效原因及解决方法 2010年11月26日 星期五 17:10 一.以下的方法会引起索引失效 ‍1,<>2,单独的>,<,(有时会用到,有时不会)3,like ...

  2. Android生命周期和Service生命周期

    android生命周期 运行:oncreate → onstart → onresume暂停:onresume → onpause:再次运行:onresume停止:onpause → onstop → ...

  3. linux清空文件等有用的指令

    1).    > filename 2).    :> filename 3).   echo "" > filename  (文件大小被截为1字节) 4).   ...

  4. hdu 1115 Lifting the Stone

    题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...

  5. MFC编程入门之五(MFC消息映射机制概述)

    在MFC软件开发中,界面操作或者线程之间通信都会经常用到消息,通过对消息的处理实现相应的操作.比较典型的过程是,用户操作窗口,然后有消息产生,送给窗口的消息处理函数处理,对用户的操作做出响应. 一.什 ...

  6. 基于dubbo源码包通过Maven构建dubbo的详细步骤

    通过Maven构建dubbo 既然可以下载得到源码以及发布包,那么为什么要去构建dubbo呢?,我们先来看下dubbo的主要模块: 我们不仅要使用dubbo的核心框架,还要使用它的一些服务,比如管理控 ...

  7. 转!!java中的内部类总结

    java内部类 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液 ...

  8. android中textview设置为多行文本时,如何让文字从最顶开始显示

    <span style="white-space:pre"> </span><EditText android:layout_width=" ...

  9. testng标签运行顺序

    testng的annotations运行顺序为: @BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @AfterMethod @AfterClas ...

  10. python 练习 30

    Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过面向对象的编程语言,那你 ...