The Pilots Brothers' refrigerator(POJ_2965)

来源:互联网 发布:黑马炒股软件 编辑:程序博客网 时间:2024/06/08 12:38

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

代码

额,递归暴力枚举了一遍。。。然后发现之前的代码是个现在又看不懂了的参考的别人的思路。。。果然还是要自己想出来才能记住。。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool str[5][5];int ans[20][2];bool flag = 0;void flip(int r, int c){    for(int i = 0; i < 4; i++)    {        str[r][i] = !str[r][i];        str[i][c] = !str[i][c];    }    str[r][c] = !str[r][c];}bool all(){    for(int i = 0; i < 4; i++)    {        for(int j = 0; j < 4; j++)        {            if(str[i][j] != 1)                return 0;        }    }    return 1;}void Solve(int r, int c, int k, int f, int cnt){    if(f == k)    {        if(all())        {            printf("%d\n", cnt);            for(int i = 0; i < cnt; i++)                printf("%d %d\n", ans[i][0], ans[i][1]);            flag = 1;        }        return ;    }    if(c >= 4)    {        if(r >= 3)            return ;        else        {            r++;            c = 0;        }    }    if(!flag)    {        Solve(r, c+1, k, f, cnt);        flip(r, c);        ans[cnt][0] = r+1;        ans[cnt][1] = c+1;        Solve(r, c+1, k, f+1, cnt+1);        flip(r, c);    }}int main(){    char c[4];    for(int i = 0; i < 4; i++)    {        scanf("%s", c);        for(int j = 0; j < 4; j++)        {            if(c[j] == '+')                str[i][j] = 0;            else                str[i][j] = 1;        }    }    for(int i = 0; i <= 16; i++)    {        int f = 0, cnt = 0;        Solve(0, 0, i, f, cnt);    }    return 0;}

->简便算法链接

0 0
原创粉丝点击