The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20899 Accepted: 8070 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
题目大意 有一个冰箱,有16个门(TM的冰箱要那么多门干嘛),每个门有两种状态,打开关闭。你的目标就是关闭所有的冰箱门,每次对于一个门进行处理的时候必须同时改变与这个门同行同列的门的状态。
思路 枚举 通过位运算进行时间压缩
#include<iostream>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
int po[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int ch[16] = {4383,8751,17487,34959,4593,8946,17652,35064,7953,12066,20292,36744,61713,61986,62532,63624};
int T1,a[17][2],b[17][2];
int main()
{
int dis = 0,vis;
char c;
for(int i = 0; i < 16; i++){
c = getchar();
if(c == '+') dis += po[i];
if(i%4 == 3) getchar();
}
T1 = inf;
for(int i = 0; i < 65536; i++)
{
int T2 = 0;
vis = dis;
for(int j = 0; j < 16; j++)
if(i & po[j]){
b[T2][0] = j/4 + 1;
b[T2++][1] = j%4 + 1;
vis ^= ch[j];
}
if(vis == 0 && T2 < T1){
T1 = T2;
for(int i = 0; i < T1; i++) {
a[i][0] = b[i][0];
a[i][1] = b[i][1];
}
}
}
cout<<T1<<endl;
for(int i = 0; i < T1; i++)
cout<<a[i][0]<<" "<<a[i][1]<<endl;
return 0;
}
如果没看懂po数组的可以运行一下下面的代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int po[4][4] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
void fun(int n)
{
cout<<n<<endl;
int x[100],Top;
Top = 0;
memset(x,0,sizeof(x));
while(n){
x[Top++] = n%2;
n /= 2;
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++)
cout<<x[i*4+j];
cout<<endl;
}
cout<<endl;
}
int main()
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++){
int sum = 0;
for(int k = 0; k < 4; k++)
sum += po[i][k] + po[k][j];
sum -= po[i][j];
fun(sum);
}
return 0;
}