(枚举)The Pilots Brothers' refrigerator

来源:互联网 发布:厨余垃圾处理器 知乎 编辑:程序博客网 时间:2024/06/05 06:22

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 rowi and all handles in columnj.

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

61 11 31 44 14 34 4

Source

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;bool mp[5][5];int flag=0,step,a[20],b[20];void judge(){   int i,j,sum=0;   for(i=1;i<=4;i++)    for(j=1;j<=4;j++)   {       if(mp[i][j]==1)        sum++;   }   if(sum==16)    flag=1;   else flag=0;}void turn(int x,int y){    int i,j;    for(i=1;i<=4;i++)    {        mp[i][y]=!mp[i][y];    }    for(j=1;j<=4;j++)    {        if(j!=y)            mp[x][j]=!mp[x][j];    }}void dfs(int x,int y,int dp){    if(dp==step)//dp初值是0,step最小是一,dp至少由0到1执行一步    {       judge();       return;    }    if(flag==1||x==5)        return;//这里有一个return返回上一级所以a,b数组最大保存step-1    a[dp]=x;    b[dp]=y;    turn(x,y);    if(y<4)        dfs(x,y+1,dp+1);    else dfs(x+1,1,dp+1);//返回时从后往前一步步返回到第一次进函数时得点    turn(x,y);    if(y<4)        dfs(x,y+1,dp);    else dfs(x+1,1,dp);    return ;}int main(){    char s;    int i,j;    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    for(i=1;i<=4;i++)    {       for(j=1;j<=4;j++)       {         scanf("%c",&s);          if(s=='+')            mp[i][j]=0;           else mp[i][j]=1;       }       getchar(); }    for(step=1;step<=16;step++)    {        dfs(1,1,0);//至少改变一处        if(flag==1)            break;    }   printf("%d\n",step);    for(i=0;i<step;i++)    {       printf("%d %d\n",a[i],b[i]);    }    return 0;}

0 0