题目传送门: POJ 2965

给一个矩阵,矩阵中只有“+”和“-”,“+”代表关闭的开关,“-”代表打开的开关,现在需要把所有的开关打开。

每次改变一个开关的状态,就同时会改变同一行或者同一列的开关的状态。

使用枚举就可以了,运用二进制来表示开关的状态= w=

###Code

2965
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

char map[6][6], op[6][6];
int x[16], y[16], total;

int reset()
{
for(int i = 0; i <= 3; i++)
for(int j = 0; j <= 3; j++)
op[i][j] = map[i][j];
return 0;
}

int make(int a, int b)
{
for(int i = 0; i <= 3; i++)
{
if(op[a][i] == '-')
op[a][i] = '+';
else
op[a][i] = '-';
}
for(int i = 0; i <= 3; i++)
{
if(op[i][b] == '-')
op[i][b] = '+';
else
op[i][b] = '-';
}
if(op[a][b] == '-')
op[a][b] = '+';
else
op[a][b] = '-';
return 0;
}

int main()
{
int rec, temp, row, col, t;
bool flag1 = false, flag2;
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j <= 3; j++)
map[i][j] = getchar();
getchar();
}
for(int i = 0; i <= 1 << 16; i++)
{
flag2 = true;
reset();
temp = i;
for(int j = 0; j < 16; j++)
{
if((temp & 1) == 1)
{
col = j / 4;
row = j % 4;
make(row, col);
}
temp = temp >> 1;
}
for(int j = 0; j <= 3; j++)
for(int k = 0; k <= 3; k++)
{
if(op[j][k] == '+')
{
flag2 = false;
break;
}
}
if(flag2)
{
flag1 = true;
rec = i;
break;
}
}
if(flag1)
{
t = 0;
temp = rec;
for(int i = 0; i < 16; i++)
{
if((temp & 1) == 1)
{
col = i / 4;
row = i % 4;
x[t] = row;
y[t ++] = col;
}
temp = temp >> 1;
}
printf("%d\n", t);
for(int i = 0; i < t; i++)
printf("%d %d\n", x[i] + 1, y[i] + 1);
}
return 0;
}