题目链接: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();
}

最新文章

  1. openvpn 启动
  2. Texstudio中文乱码问题
  3. VS2012 编译带有c/c++代码的python模块失败解决方案
  4. Lintcode: Topological Sorting
  5. 转: 在.NET中操作数字证书
  6. Unix时间戳 和 NSDate 的转换
  7. 新手学习.net编程计划-1
  8. javascript的getter和setter(转)
  9. nginx解决方案
  10. Spring-shiro源码陶冶-DelegatingFilterProxy和ShiroFilterFactoryBean
  11. 在Windows上安装Nexus 3.2.0-01
  12. [转帖]nginx配置ssl加密(单/双向认证、部分https)
  13. Jupyter Notebook 编辑器美化
  14. 9.Django Admin编写
  15. JDK7和JDK8concurrentHashmap区别
  16. Docker 容器管理:rancher
  17. 关于HttpServletRequest报红叉的解决办法
  18. 中断、轮询、事件驱动、消息驱动、数据流驱动(Flow-Driven)?
  19. SuperSlide——再次接触
  20. 二进制求和(LintCode)

热门文章

  1. 055 Jump Game 跳跃游戏
  2. 过流监测芯片ADS720/723
  3. (转)启动网卡报错(Failed to start LSB: Bring up/down networking )解决办法总结
  4. 使用MRUnit对MapReduce进行单元测试
  5. Jquery会死吗?我为什么不用vue写富文本!
  6. Zookeeper+websocket实现对分布式服务器的实时监控(附源码下载)
  7. SpringMVC对HTTP报文体的处理
  8. Java函数的传参机制
  9. Git、Github和GitLab的区别及与SVN的比较
  10. jsp使用中$的符号使用失效