POJ 2965

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

翻冰箱门 翻到全部为-为止
和1753一样BFS&DFS&数学方法
数学方法不会

BFS

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>

using namespace std;

int Map[4][4];

struct node
{
    int New[17];//新的地图
    int x;//当前位置
    int ans;//步数
    int vis[17];
} status[1000001];

int Final(node a)//判断是否为结束
{
    int sum=0;
    for(int i=0; i<16; i++)
        sum+=a.New[i];
    return sum;
}


void bfs()
{
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            status[0].New[i*4+j]=Map[i][j];
    memset(status[0].vis,0,17);
    status[0].x=0;
    status[0].ans=0;
    int s=1,e=0;
    while(s>e)
    {
        node f1,f2;
        f1=status[e++];
        int flag=Final(f1);
        if(flag==0)
        {
            cout<<f1.ans<<endl;
            for(int i=0; i<16; i++)
                if(f1.vis[i]==1)
                    cout<<i/4+1<<" "<<i%4+1<<endl;;
            return ;
        }
        for(int i=0; i<16; i++)
        {
            if(f1.x<=i)
            {
                f2=f1;

                for(int j=i-i%4; j<i+4-i%4; j++)
                    f2.New[j]=1-f1.New[j];
                for(int j=i%4; j<16; j+=4)
                    f2.New[j]=1-f1.New[j];
                f2.New[i]=1-f1.New[i];//反转当前棋子
                f2.vis[i]=1;
                f2.ans=f1.ans+1;
                f2.x=i+1;
                status[s++]=f2;
            }
        }
    }
    return ;
}

int main()
{
    char a[4];
    for(int i=0; i<4; i++)
    {
        cin>>a;
        for(int j=0; j<4; j++)
        {
            if(a[j]=='-')
                Map[i][j]=0;
            else
                Map[i][j]=1;
        }
    }
    bfs();
    return 0;
}

DFS

#include <iostream>
#include <stdio.h>

using namespace std;

int Map[5][5],ans=33,ansx[33],ansy[33],x[33],y[33];

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

void fan(int a)
{
    int i=a/4;
    int j=a%4;
    int k;
    Map[i][j]=!Map[i][j];
    for(k=0; k<4; k++)
    {
        Map[i][k]=!Map[i][k];
        Map[k][j]=!Map[k][j];
    }
}

void dfs(int a,int b)
{
    if(flag())
    {
        if(ans>b)
        {
            ans=b;
            for(int i=1; i<=ans; i++)
            {
                ansx[i]=x[i];
                ansy[i]=y[i];
            }
        }
        return;
    }
    if(a>=16)
        return;
    dfs(a+1,b);
    fan(a);
    x[b+1]=a/4+1;
    y[b+1]=a%4+1;
    dfs(a+1,b+1);
    fan(a);
}

int main()
{
    int i,j;
    char c;
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            scanf("%c",&c);
            if(c=='+')
                Map[i][j]=0;
            else
                Map[i][j]=1;
        }
        getchar();
    }
    dfs(0,0);
    printf("%d\n",ans);
    for(i=1; i<=ans; i++)
        printf("%d %d\n",ansx[i],ansy[i]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值