B

来源:互联网 发布:java架构设计 编辑:程序博客网 时间:2024/05/01 22:12
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

61 11 31 44 14 34 4

卡着时间过的枚举代码主要是枚举做的太少了,发现连二进制枚举都要想半天。

#include <iostream>#include <cstdio>#include <algorithm>#include <queue>using namespace std;int bits[20],original[5],cur[5];char str[10][10];int main(){    int i,j,k;    for(i = 0;i < 17;++i){        bits[i] = (1 << i);    }    while(~scanf("%s",str[0])){        for(i = 1;i < 4;++i){            scanf("%s",str[i]);        }        for(i = 0;i < 4;++i){            for(j = 0;j < 4;++j){                if(str[i][j] == '+'){                    original[i] |= bits[j];                 }            }        }        int ans = 0x3f3f3f3f,t = 0x3f3f3f3f;        for(i = 0;i < bits[16];++i){            int c = 0;            if(i == 53261){                c = 0;            }            for(j = 0;j < 4;++j){                cur[j] = original[j];            }            for(j = 0;j < 16;++j){                if(i & bits[j]){                    c++;                    int x = j / 4;                    int y = j % 4;                    for(k = 0;k < 4;++k){                        cur[x] ^= bits[k];                    }                    for(k = 0;k < 4;++k){                        if(k != x)                            cur[k] ^= bits[y];                    }                }            }            bool flag = true;            for(j = 0;j < 4;++j){                if(cur[j] != 0){                    flag = false;                    break;                }            }            if(flag && c < ans){                ans = c;                t = i;            }        }        cout << ans << endl;        for(i = 0;i < 16;++i){            if(t & bits[i]){                int x = i / 4;                int y = i % 4;                cout << x + 1 << " " << y + 1 << endl;            }        }    }    return 0;}