题目描述
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.

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

输出
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.
样例输入 Copy

样例输出 Copy
6
1 1
1 3
1 4
4 1
4 3
4 4

思路
因为这个思路真的很有意思,所以就好好瞅瞅嘿嘿嘿~

  • 以题目中所给例子来跟大家聊聊哈!我们可以开一个新的二维数组vis[5][5](是5而不是4的原因是我用4写的时候数据溢出),vis数组对应我们输入的char型二维数组可看成
  • 如果遍历二维的char,有“+”的位置就让它所在的行和列发生相应的改变,也就是将对应的vis数组的位置加1,记录改变次数
  • 我们可以得到如下记录
  • 我们就能发现了一个“神奇”的事情:二维数组里的奇数所在的位置,就是我们需要改变的位置。
  • 这是为什么呢?
  • 只在有“+”的位置让它对应的行和列发生相应改变是因为要先确定哪些位置会被“波及”到,同理,也只有这些非零的位置中有可能存在我们要改变的位置。
  • 明确:经过偶数次的变化不会使这个位置的+或者-号发生变化,只有奇数次能实现改变。也就是说,图中的是偶数的位置的变化并不会帮助我们达到全“-”,是无效的位置,而是奇数的位置可以,是有效的位置。
#include <bits/stdc++.h>using namespace std;char c[5][5];int step=0;int vis[5][5];int main(){memset(vis,0,sizeof(vis));for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){cin>>c[i][j];}}for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){if(c[i][j]=='+'){for(int k=1;k<=4;k++){vis[i][k]++;vis[k][j]++;  }vis[i][j]--;}}}for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){if(vis[i][j]%2){step++;}}}cout<<step<<endl;for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){if(vis[i][j]%2){cout<<i<<" "<<j<<endl;}}}return 0;}

The Pilots Brothers‘ Refrigerator(高效贪心)相关推荐

  1. POJ 2965.The Pilots Brothers‘ refrigerator

    POJ 2965.The Pilots Brothers' refrigerator Ideas 题意:给你4*4的矩阵.每个点有两种状态,+代表关,-代表开.每个点有一个操作就是该点所在行列所有状态 ...

  2. poj 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 poj 1753扩展,dfs+枚举,不过加了一个路径. The Pilots Brothers' refrigerator Time Li ...

  3. ACM POJ 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65 ...

  4. B - The Pilots Brothers' refrigerator

    B - The Pilots Brothers' refrigerator 文章目录 B - The Pilots Brothers' refrigerator 题目描述: Input: Output ...

  5. POJ2965 The Pilots Brothers‘ refrigerator

    POJ2965 The Pilots Brothers' refrigerator 题干 Description Input Output Sample Input Sample Output 题意 ...

  6. The Pilots Brothers‘ refrigerator(思维)

    题面:The Pilots Brothers'refrigerator 题目大意 "飞行员兄弟"这个游戏,需要玩家打开一个有着16把手的冰箱. 每个把手有且只有两种状态:打开(−- ...

  7. POJ-2965 The Pilots Brothers' refrigerator

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

  8. POJ-2965-The Pilots Brothers\' refrigerator

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24651 Accepted: 9504 Special Judge Descri ...

  9. The Pilots Brothers' refrigerator - poj 2965

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   Special Judge ...

最新文章

  1. 秒杀多线程第六篇 经典线程同步 事件Event
  2. sqlserv已生成用于更新的结果集。_ML.NET API 和工具八月更新
  3. 现代通用计算机的雏,1834年巴贝奇设计的( )是现代通用计算机的雏形 答案:分析机...
  4. GIL , 线程池 , 同步 , 异步 , 队列 , 事件
  5. logistic regression一点理解
  6. Spring AOP自动创建代理 和 ProxyFactoryBean创建代理
  7. JVM教程:JM内存分哪几个区,每个区的作用是什么?
  8. 数据结构题集c语言版题目与答案,数据结构题集(C语言版)答案 - 严蔚敏编著...
  9. MySQL之环境变量配置
  10. Origin图选择性粘贴到word出现问题,提示‘word出现问题’解决方法
  11. python超清壁纸_Python爬取5K分辨率超清唯美壁纸
  12. 网络设备高可用性简例
  13. ae合成设置快捷键_(精品)AE从小白到大神之路(一)-AE入门
  14. Pytorch基础操作 —— 8. 张量转置操作
  15. vue-echarts数据统计图表展示
  16. 1]解决java.util.concurrent.RejectedExecutionException
  17. AI2022:如何在 Illustrator 中创建色板?
  18. 这10本书,带你了解 ChatGPT 的底层逻辑!
  19. Linux - iptable(防火墙)
  20. Django 之ORM(一)

热门文章

  1. sql中intersect_INTERSECT –谓词中被低估的双向
  2. 只会用 xxl-job?更强大的新一代分布式任务调度框架来了!
  3. 不同网络环境下监控视频统一汇聚集中管理方案介绍
  4. 浪潮nf5220服务器做系统,【浪潮NF5220参数】浪潮NF5220系列服务器参数-ZOL中关村在线...
  5. Python快速入门(八)面向对象1:类、对象和封装
  6. 使用ethtool限制服务器网速
  7. AI绘画神器Stable Diffusion的疯狂与危险
  8. 机器学习中的优化算法介绍
  9. 计算机丢失quartz.dll什么意思,计算机中丢失quartz.dll解决方法
  10. c语言知识地图,【程序设计论文】C语言程序设计翻转课堂研究(共3546字)