Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 24669 | Accepted: 9510 | 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
题意: 有一个冰箱,门上有4*4,16个把手,'+'代表关闭,'-'代表打开.现在给出这个4*4的把手矩阵,表示把手的状态,问至少需要几步可以把冰箱打开(把手状态全部变为'-'),求step?
题解思路:
奇变偶不变,把手状态变化偶数倍和不变化是一样的,用一个表变化次数的矩阵mp标记变化次数,最后根据是否变化奇数次来判断输出.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int mp[4][4];
int main()
{
int step=0;
char a;
memset(mp, 0, sizeof(mp));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
scanf("%c", &a);
if(a == '+')
{
mp[i][j]++;
for(int k = 0; k < 4; k++)
{
mp[i][k]++;
mp[k][j]++;
}
}
}
getchar(); //*****注意点*****
}
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(mp[i][j]%2 == 1)
step++;
}
}
printf("%d\n", step);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(mp[i][j]%2 == 1)
printf("%d %d\n", i+1, j+1);
}
}
return 0;
}