The Pilots Brothers' refrigerator Poj2965

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20326 Accepted: 7831 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
 
 
第一次写博客  :)(纪念一下)。
此题为BFS纯收索。用的一位数组,关键是将对一维数组的操作间接地对二维数组进行变动。
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[20];
struct node
{
    int x,ans,a[20],vis[20];//有输出进行改动的坐标,故将他们分别的标记。
} q[100010];
void bfs()
{
    int i,j,k,s=0,e=0;
    node f1,f2;
    for(i=0; i<16; i++)
    {
        f1.a[i]=map[i];
        f1.vis[i]=0;
    }
    f1.x=f1.ans=0;
    q[s++]=f1;
    while(s>e)
    {
        f1=q[e++];
        int ss=0;
        for(i=0; i<16; i++)
            ss+=f1.a[i];
        if(ss==0)
        {
            cout<<f1.ans<<endl;
            for(i=0; i<16; i++)
                if(f1.vis[i])
                    cout<<i/4+1<<" "<<i%4+1<<endl;//    /4是对行,%4对列
            return ;
        }
        for(i=0; i<16; i++)
        {
            if(f1.x<=i)//一定不要少了,此为对移动次数的控制即走过一次便少遍历一次
            {
                f2=f1;
                for(j=i-i%4; j<i+4-i%4; j++)//行		i-i%4即以i=》4--7来说j都是从4--7实现的对同行的改变
                    f2.a[j]=1-f2.a[j];
                for(j=i%4; j<16; j=j+4)
                    f2.a[j]=1-f2.a[j];
                f2.a[i]=1-f2.a[i];///注意下标是i不是j
                f2.x=i+1;
                f2.ans=f1.ans+1;
                f2.vis[i]=1;
                q[s++]=f2;
            }
        }
    }
    puts("Impossible");
    return ;
}
char s[4];
int main()
{
    int n,m,i,j,k;
    for(i=0; i<4; i++)
    {
        cin>>s;
        for(j=0; j<4; j++)
        {
            if(s[j]=='-')
            {
                map[i*4+j]=0;
            }
            else
                map[4*i+j ]=1;
        }
    }
    bfs();
    return 0;
}
 
 
后来再做的时候还是有些不足
1:在限制放16个棋子的时候居然写成了f2.tmp++
2:要使用手动模拟队列,快!
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#define L1 long long
#define L2 __int64
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

//const int m1=1001000;
//const int m2=1010;
//int head[m1],vex[m1],arr[m1],cnt;
bool vis[17];
int cnt;
char  s1[5][5];

struct node
{
    int tmp,li;
    bool vis[20];
    char s[17];
}q[100010];

void bfs()
{
    int i,j,s,e;
    s=e=0;
    node  f1,f2;
    f1.tmp=f1.li=0;
    cnt=0;
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
        {
            f1.vis[cnt]=false;
            f1.s[cnt++]=s1[i][j];
        }

    f1.s[cnt]='\0';
    q[s++]=f1;
    //cout<<f1.s<<endl;

    int t;
    while(s>e)
    {
        f1=q[e++];
        bool bj=false;
        for(i=1;i<16;i++)
        {
            if(f1.s[i]==f1.s[i-1]&&f1.s[i]=='-')
            {
            }
            else
            {
                bj=true;
                break;
            }
        }
        //cout<<f1.s<<endl;
        if(!bj)
        {
            printf("%d\n",f1.tmp);
            for(i=0;i<16;i++)
            {
                if(f1.vis[i])
                {
                    printf("%d %d\n",i/4+1,i%4+1);
                }
            }
            return ;
        }

        for(i=0; i<16; i++)
        {
            if(f1.li<=i)
            {
                f2=f1;
                t=i/4;
                for(j=t*4;j<t*4+4;j++)
                {
                    if(f2.s[j]=='+')
                        f2.s[j]='-';

                    else
                        f2.s[j]='+';
                }
                t=i%4;
                for( j=t;j<16;j+=4)
                {
                    if(f2.s[j]=='+')
                        f2.s[j]='-';
                    else
                        f2.s[j]='+';
                }

                if(f2.s[i]=='+')
                    f2.s[i]='-';
                else
                    f2.s[i]='+';
                f2.li=i+1;
                f2.tmp++;
                f2.vis[i]=true;
                q[s++]=f2;
            }
        }
    }
    return ;
}

int main()
{
    int n,m,i,j,k;
    for(i=0; i<4; i++)
        scanf("%s",s1[i]);
    bfs();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值