解题报告:POJ 2965 The Pilots Brothers' refrigerator 两种做法

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


题意:

这题和poj1753很类似啊,有个4*4的棋盘,棋盘上每个棋子有close 和open两个状态,输入会给你,然后你同样是每次选一个棋子,然后将同行同列(包括该棋子)全部进行翻转操作。问多少次操作后可以达到全部open的状态,然后输出需要操作的点的位置(注意坐标是从1,1开始到4,4)

 

思路:

这题想到了两种做法,两份代码都会贴上:

第一种是和之前写POJ1753的题解做法类似,按操作数进行1到16进行DFS深搜获取答案。具体就是先枚举操作的次数,然后通过DFS选择操作的棋子。提交后发现惊险的1000ms过了。。。。于是我开始思考更高效地做法。

 

第二种做法32ms过,非常高效,但是稍微难以理解。

我们知道当一个棋子被翻转偶数次,那么它的状态不改变,如果被翻转奇数次,那么状态改变,也就是说初始状态为闭合的棋子一定是经过了奇数次操作,而open的棋子一定是经过了偶数次操作。那么每次碰到状态为close的棋子就将它同行同列(包括棋子本身)的操作数加一,最后操作数为奇数个的点就是答案咯。

 

第一种方法1000ms:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

using namespace std;

int k=0;
int Map[5][5];

struct
{
    int x,y;
}step[20];

bool check()
{
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(Map[i][j]==0)
                return 0;
    return 1;
}

void oper(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        Map[i][y]=!Map[i][y];
        Map[x][i]=!Map[x][i];
    }
    Map[x][y]=!Map[x][y];
    /*
    for(int i=0;i<4;i++,printf("\n"))
        for(int j=0;j<4;j++)
            printf("%3d",Map[i][j]);
    system("pause");
    */
}

void dfs(int x,int y,int m,int n)
{
    if(k)
        return ;
    if(m==n)
    {
        if(check())
            k=1;
    }
    else
    {
        if(y==4)
            y=0,x++;
        if(x==4)
            return ;
        for(int i=x;i<4&&!k;i++)
            for(int j=0;j<4&&!k;j++)
            {
                if(i==x&&j==0)j=y;
                step[m].x=i+1;step[m].y=j+1;
                oper(i,j);
                dfs(i,j+1,m+1,n);
                oper(i,j);
            }
    }
}

int main()
{
    char str[10];
    for(int i=0;i<4;i++)
    {
        scanf("%s",str);
        for(int j=0;j<4;j++)
        {
            if(str[j]=='+')
                Map[i][j]=0;
            else
                Map[i][j]=1;
        }
    }
    for(int i=1;i<=16;i++)
    {
        dfs(0,0,0,i);
        if(k)
        {
            printf("%d\n",i);
            for(int j=0;j<i;j++)
                printf("%d %d\n",step[j].x,step[j].y);
            break;
        }
    }
    return 0;
}

第二种方法32ms:


#include<cstdio>
#include<cstring>
#include<algorithm>

int A[5][5];

void oper(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        A[i][y]++;
        A[x][i]++;
    }
    A[x][y]--;
}

int get()
{
    int ans=0;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(A[i][j]%2)
                ans++;
    return ans;
}

int main()
{
    char str[10];
    for(int i=0;i<4;i++)
    {
        scanf("%s",str);
        for(int j=0;j<4;j++)
            if(str[j]=='+')
                oper(i,j);
    }
    printf("%d\n",get());
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(A[i][j]%2)
                printf("%d %d\n",i+1,j+1);
    return 0;
}


你可能感兴趣的:(枚举,C语言,ACM,poj)