您的位置:首页 > 编程语言 > C语言/C++

poj 2965 The Pilots Brothers' refrigerator

2013-07-08 00:00 260 查看
摘要: 不看题解真做不来= =枚举tle。。竟然有O(n^2)的算法【此题有一简单方法,就是依次将所有关闭的开关所在的行和列都翻转一遍(除了开关位置),然后遍历整个矩阵,翻转奇数次的开关就是要翻转的开关,奇数的个数就是要翻转的次数。这个可以自己去证明一下。】

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>

using namespace std;

int time_[4][4] = {0};
bool color[4][4] = {0};

int main()
{
char temp[6];
for(int i = 0; i < 4; ++i)
{
scanf("%s", temp);
for(int j = 0; j < 4; ++j)
{
if(temp[j] == '-') color[i][j] = 0;
else
{
color[i][j] = 1;
time_[i][j]++;
for(int k = 0; k < 4; ++k)
{
time_[i][k]++;
time_[k][j]++;
}
}
}
}
int sum_ = 0;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
{
if(time_[i][j] % 2 != 0)
{
sum_++;
}
}
printf("%d\n", sum_);
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
{
if(time_[i][j] % 2 != 0)
{
printf("%d %d\n", i + 1, j + 1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj cpp