poj 2965 The Pilots Brothers' refrigerator【枚举】

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



题意:

要想打开冰箱必须是使网格中所有的符号变成 ‘-’,不存在‘+’,每改变一个符号则它所在的行和列都要变换,因此操作偶数次相当于没有变换。。

思路:

要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;可以证明,如果'+'位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一'+'位置的符号改变,其余都不会改变.  所以把4*4网格的初始值都设为0即操作数,进行操作后,网格中1的个数即为操作数,1的位置即为操作位置输出行列~


ACcode:


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool map[5][5];
int main()
{
	char c;
	memset(map, 0, sizeof(map));
	int row[16],col[16];
	int i, j, k;
	for(i = 0; i < 4; i++)
	{
		for(j = 0; j < 4; j++)
		{
			scanf("%c", &c);
			if(c == '+')
			{
				map[i][j] = !map[i][j];
				for(k = 0; k < 4; k++)
				{
					map[i][k] = !map[i][k];
					map[k][j] = !map[k][j];
				}	
			}
				
		}
		getchar();
	}
	int ans = 0;
	for(i = 0; i < 4; i++)
	{
		for(j = 0; j < 4; j++)
		{
			if(map[i][j]==1)
			{
				row[ans] = i + 1;
				col[ans] = j + 1;
				ans++;
			}
		}
	}
	printf("%d\n", ans);
	for(i = 0; i < ans; i++)
		printf("%d %d\n",row[i], col[i]);
	return 0;
} 


你可能感兴趣的:(poj 2965 The Pilots Brothers' refrigerator【枚举】)