POJ 2965-The Pilots Brothers' refrigerator

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19794 Accepted: 7614 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
 
小结:
    怎么说好呢?说实话,感觉这道题目时间压得很近,暴力方法交几次,几次TLE,也是醉了,冥冥之中突然蹦出一个概念--剪枝!!这个我从来没有用过的方法,第一次用竟然就能达到这么有效的结果!!
    大致的做法和POJ上有一道题目Flip Game很像!所以我无论是格式还是输出基本都和那题解法雷同,总共有16个格子,每种格子对应两种情况,翻或者不翻,因此总共有2的16次方的情况,感觉是不会有问题的,暴力应该妥妥的求出来了,现在还没有想通,如果谁有想法欢迎评论,我定当感激不尽!!
    然后呢,就是怎么剪的问题了,我的想法是当判断到一行的时候,前面几行的任何一列应该都是一样的,不然不可能达成相应的目的,这点是关键!看懂这点,暴力解法就不会被无情的TLE了。
以下是AC代码(115MS):
#include<stdio.h>
#include<string.h>
struct node
{
    char map[5][5];
    int ans;
    int changex[17];
    int changey[17];
}final;
int allsame(struct node a)
{
    for(int i=1;i<=4;i++)
    for(int j=1;j<=4;j++)
    if(a.map[i][j]=='+')
    return 0;
    return 1;
}
void change(struct node *a,int row,int col)
{
    for(int i=1;i<=4;i++)
    {
        (*a).map[row][i]=(*a).map[row][i]=='-'?'+':'-';
        (*a).map[i][col]=(*a).map[i][col]=='-'?'+':'-';
    }
    (*a).map[row][col]=(*a).map[row][col]=='-'?'+':'-';
}
int check(struct node a,int row)
{
    for(int i=1;i<row;i++)
    {
        for(int j=1;j<=4;j++)
        {
            if(a.map[i][j]!=a.map[1][j])
            return 0;
        }
    }
    return 1;
}
void dfs(struct node a,int row,int col)
{
    if(allsame(a))
    {
        final=a.ans<final.ans?a:final;
        return;
    }
    if(!check(a,row)||row>4)
    return;
    //printf("a\n");
    struct node temp=a;
    dfs(a,row+col/4,col==4?1:col+1);
    temp.ans++;
    temp.changex[temp.ans]=row;
    temp.changey[temp.ans]=col;
    change(&temp,row,col);
    dfs(temp,row+col/4,col==4?1:col+1);
}
int main()
{
    struct node init;
    for(int i=1;i<=4;i++)
    {
        scanf("%s",init.map[i]+1);
    }
    init.ans=0;
    final.ans=20;
    dfs(init,1,1);
    printf("%d\n",final.ans);
    for(int i=1;i<=final.ans;i++)
    {
        printf("%d %d\n",final.changex[i],final.changey[i]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值