POJ 2965 The Pilots Brothers' refrigerator (DFS)

The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23895 Accepted: 9205 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

题意:给你4*4个开关,当你点击一个开关时,这个开关所处的行和列的开关都会被点击,问你,需要点击几次开关可以使得所有开关都为-。

思路:直接枚举状态就好了,枚举复杂度为 O(216) ,然后就一直搜就好了,搜到了就停止,注意中间开关的变化,当一个开关变化次数为偶数次时,相当于没有变化.

ac代码:

/* ***********************************************
 Author       : AnICoo1
 Created Time : 2016-07-27-14.42 Wednesday
 File Name    : D:\MyCode\2016-7月\2016-7-27-3.cpp
 LANGUAGE     : C++
 Copyright  2016 clh All Rights Reserved
 ************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
char s[5][5];
int ans;
int num[20];
int v[MAXN],vis[30];
int A[20];
int bz;
void DFS(int id,int cnt)
{
    if(bz)
        return ;
    if(id==17)
    {
        int k=0,kk=1;
        for(int i=16;i>=1;i--)
        {
            int c=num[i];
            if(vis[i]%2)
                c=num[i]?0:1;
            k+=c*kk;kk*=2;
        }
        if(k==0)
        {
            if(ans>cnt)
            {
                ans=cnt;
                for(int i=1;i<=16;i++) A[i]=v[i];
            }
            bz=1;
        }
        return ;
    }
    int k=id%4?id/4+1:id/4;
    DFS(id+1,cnt);
    v[id]=1;vis[id]++;for(int i=id+4;i<=16;i+=4) vis[i]++;for(int i=id-4;i>=1;i-=4) vis[i]++;for(int i=k*4;i>=k*4-3;i--) if(i!=id) vis[i]++;
    DFS(id+1,cnt+1);
    v[id]=0;vis[id]--;for(int i=id+4;i<=16;i+=4) vis[i]--;for(int i=id-4;i>=1;i-=4) vis[i]--;for(int i=k*4;i>=k*4-3;i--) if(i!=id) vis[i]--;

}
int main()
{
    while(scanf("%s",s[0])!=EOF)
    {
        for(int i=1;i<4;i++)
            scanf("%s",s[i]);
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(s[i][j]=='+')
                    num[i*4+j+1]=1;
                else
                    num[i*4+j+1]=0;
            }
        }        mem(A);mem(vis);mem(v);ans=INF;bz=0;
        DFS(1,0);
        printf("%d\n",ans);
        for(int i=1;i<=16;i++)
        {
            if(A[i])
            {
                int x=i%4?i/4+1:i/4;
                int y=i%4?i%4:i/x;
                printf("%d %d\n",x,y);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值