POJ:2695-The Pilots Brothers' refrigerator
题目链接:http://poj.org/problem?id=2965
The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
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
题意就是给你一个4*4的图,要通过翻转将图全改为‘-’,并打印出翻转过程,每次翻转一个点,这个点所在的行和列也要同时翻转。
刚看到的时候就以为是POJ1753题的升级版 ,其实这也是一个思维题,要翻转一个点并且除了这个点外其他点不变,就只能将这个点所在的行列上所有的点都给翻转一下。然而一个点如果翻转偶数次就和没翻转的效果一样。这样在实现的时候就可以开一个4*4的int数组,每个是‘+’的点它所在的行列上的点全加一,最后将这个int数组上的所有的数全mod2,如果int数组中1的个数就是需要翻转的次数,1所在的点就是需要翻转的点。
然而自己在写这个题的时候用poj1753的思路写了个bfs超时了,看到网上都是用dfs过的好像没有bfs。
丑代码:
#include<stdio.h>
using namespace std;
const int maxn = 10;
char s[maxn][maxn];
int maps[maxn][maxn];
void deal(int x,int y)
{
int x2,y2;
maps[x][y]++;
//上下作业操作
x2 = x-1;
while(x2>=0)
{
maps[x2][y]++;
x2--;
}
y2 = y-1;
while(y2>=0)
{
maps[x][y2]++;
y2--;
}
x2 = x+1;
while(x2<4)
{
maps[x2][y]++;
x2++;
}
y2 = y+1;
while(y2<4)
{
maps[x][y2]++;
y2++;
}
}
void solve()
{
for(int i=0;i<4;i++)
scanf("%s",s[i]);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(s[i][j] == '+')
deal(i,j);
}
int sum = 0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
maps[i][j] = maps[i][j]%2;
if(maps[i][j])
sum++;
}
printf("%d\n",sum);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(maps[i][j])
printf("%d %d\n",i+1,j+1);
}
return ;
}
int main()
{
solve();
}
POJ:2695-The Pilots Brothers' refrigerator的更多相关文章
- POJ 2695 The Pilots Brothers' refrigerator(神奇的规律)
转载请注明出处:http://blog.csdn.net/a1dark 分析:如果想要将一个“+”翻转成“-”,那么必然会把对应的行和列上的所有点翻转一次.由于一个点翻偶数次就相当于不翻转.所以我需要 ...
- 【POJ 2965】 The Pilots Brothers' refrigerator
[题目链接] http://poj.org/problem?id=2965 [算法] 位运算 [代码] #include <algorithm> #include <bitset&g ...
- The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20304 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
随机推荐
- replcation set (复制集)配置过程 --mongodb
一,配置规划 复制集原理(基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB) Paxos(mysql MGR 用的是变种))如果发生主库宕机,复制集内部会进行投票选举,选择一 ...
- spring和springmvc是单例还是多例
这么说其实不规范 spring的bean 默认是单例 springmvc的controller 默认是单例 所以最好不要在controller里定义成员变量 都可通过注解 @scope=p ...
- java内存分配(堆、栈、常量池)
Java内存分配: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,以及成员方法中的局部变量 ◆堆:存放对象本身(成员变量+成员方法的引用) ◆静态域:存放在对象中用static ...
- 传入泛型类型(T.class)的方法
java中当我们需要T.class时会报错,这是我们只需定义一个Class<T>类作为参数传入即可,具体如下: public List<T> findStuByQuery(De ...
- iOS - KVO 简单应用
KVO(键值监听)全称 Key Value Observing.使用KVO可以实现视图组件和数据模型的分离,视图作为监听器,当模型的属性值发生变化后,监听器可以做相应的处理.KVO的方法由NSKeyV ...
- centos 安装 rtmp nginx 视频流服务器
---恢复内容开始--- 1.使用yum安装git yum -y install git 2.下载nginx-rtmp-module,官方github地址 // 通过git clone 的方式下载到服 ...
- 【转】Java Cipher类 DES算法(加密与解密)
Java Cipher类 DES算法(加密与解密) 1.加密解密类 import java.security.*; import javax.crypto.*; import java.io.*; / ...
- django之母版的继承
模板继承示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- codeforce Gym 100500E IBM Chill Zone (SG函数)
关于sg函数这篇blog讲得很详细http://blog.csdn.net/logic_nut/article/details/4711489. sg函数的价值在于把复杂的游戏拆分成简单的游戏,然后通 ...
- IOS CoreData 多表查询(下)
http://blog.csdn.net/fengsh998/article/details/8123392 在iOS CoreData中,多表查询上相对来说,没有SQL直观,但COREDATA的功能 ...