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

Source

Northeastern Europe 2004, Western Subregion
 
 
题意:
  给你4*4的矩阵。每个点有两种状态,+代表关,-代表开。每个点有一个操作就是该点所在行列所有状态翻转。问最少多少次可以全部打开,并且输出最少个数情况下翻转的点。
 
解题思路:
  这道题跟1753 -- Flip Game很像,可以用1753的解题思路。
  这道题用了更简单的方法。
  遍历矩阵中每一个关闭的点,按照规则将其进行翻转,将他的反转次数++。重复上述操作,直到所有的点都是打开状态。
 #include<iostream>
#include<stdio.h>
using namespace std;
char m[][];
int Count[][];
//判断状态
bool Check()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
if(m[i][j] == '+') return ;//没有被全部打开
}
return ;
}
int getAns()
{//计算结果
int ans = ;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(Count[i][j]% == )
{
Count[i][j] = ;
}else{
Count[i][j] = ;
ans++;
}
}
}
return ans;
}
int changeState()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
if(m[i][j] == '+')//开关为关闭状态
{
m[i][j] = '-';
Count[i][j] += ;
for(int k = ;k<=;k++)
{
if(m[i][k] == '+') {m[i][k] = '-';}
else{m[i][k] = '+';}
}
for(int k=;k<=;k++)
{
if(m[k][j] == '+') {m[k][j] = '-';}
else{m[k][j] = '+';}
}
if(Check())
{//进行结果输出
return getAns();
}
}
}
if(Check() == ) return changeState();
} int main()
{
char c;
//初始化
while(true)
{
for(int i = ;i<=;i++)
for(int j=;j<=;j++)
Count[i][j] = ;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if((c = getchar()) == EOF) return ;
m[i][j] = c;
}
c = getchar();
} cout<<changeState()<<endl;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(Count[i][j] == ) cout<<i<<" "<<j<<endl;
}
}
} return ;
}

  对于一个关闭的点,想要把他打开,首先翻转一下他自己,为了不影响其他的点他所在行列都得翻转一次。这样最后是奇数次的即为翻转点。
 #include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char m[][];
int Count[][];
//判断状态
bool Check()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
if(m[i][j] == '+') return ;//没有被全部打开
}
return ;
}
int getAns()
{//计算结果
int ans = ;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(Count[i][j]% == )
{
Count[i][j] = ;
}else{
Count[i][j] = ;
ans++;
}
}
}
return ans;
}
int main()
{
char c;
while(true)
{
memset(Count,,sizeof(Count));//初始化
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if((c = getchar()) == EOF) return ;
m[i][j] = c;
if(c == '+')
{
for(int k=;k<=;k++)
{
Count[k][j]++;
Count[i][k]++;
}
Count[i][j]--;
}
}
c = getchar();
}
///打印结果
cout<<getAns()<<endl;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(Count[i][j] == ) cout<<i<<" "<<j<<endl;
}
}
} return ;
}

 
 
 

2965 -- The Pilots Brothers' refrigerator的更多相关文章

  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 暴力 难度:1

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

  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 (dfs)

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

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

  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+位压缩运算)

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

随机推荐

  1. 安卓开发之sql语句增删改查

    package com.lidaochen.phonecall; import android.content.Context; import android.database.sqlite.SQLi ...

  2. appium-清空输入框的内容后,再次输入内容会回退最后两个字符串

    问题描述 有两个输入框,用户名和密码输入框 调用set_text方法,输入用户名 再次调用set_text方法,输入密码 清空用户名输入框的内容后,再次输入内容会回退最后两个字符串 出问题的代码 de ...

  3. Image Processing and Analysis_8_Edge Detection: Optimal edge detection in two-dimensional images ——1996

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  4. 【Day4】2.详解Http请求协议

    Http请求协议

  5. printf固定一行打印倒计时的实现

    @2019-07-15 [小记] #include<stdlib.h> #include <stdio.h> #include <time.h> #include ...

  6. linux——在windows上搭建linux练习环境

    程序员自己研究——java-linux-php——环境搭建 需要首选准备一个linux环境. 1,可用安装一个虚拟机:VMware虚拟机 2,安装一个VMware大约5分钟左右. 3,截止目前2019 ...

  7. 关于join() 是否会释放锁的一些思考

    # 首先从一个很有意思的问题开始: - 问 : Thread 的join() 方法是否会释放锁? - 答: 会! # 如果一切到这里就结束了,那可能也就没有这篇小记了,但是我的脑子却冒出了一些奇怪的想 ...

  8. Codeforces 920E-Connected Components? (set,补图,连通块)

    Connected Components? CodeForces - 920E You are given an undirected graph consisting of n vertices a ...

  9. ServiceLoader在SPI中的重要作用分析

    对于线程上下文类加载器在之前已经对它进行了详细的理论化的学习,其中对于这个类加载器应用最多的也就是在SPI场合下用来打破双亲委托机制,如之前所描述的: 这次举一个具体的例子来进一步的加深对线程上下文类 ...

  10. Training #2 cell battle (BFS)

    Constraints: 1 <= R, C <= 500 1 <= T <= 5 Sample Input: 5 3 5 ##### a...b ##### 3 4 #### ...