The Pilots Brothers' refrigerator

2965

he Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19995   Accepted: 7695   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
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 using namespace std;
  5 
  6 int map[6][6],ak[10005],AK[10005],flg,n,step;
  7 
  8 int judge(int map[6][6])
  9 {
 10     for(int i=1;i<=4;i++)
 11         for(int j=1;j<=4;j++)
 12         {
 13             if(map[i][j]!=1)
 14                 return 0;
 15         }
 16     return 1;
 17 }
 18 
 19 void flip(int r,int c)
 20 {
 21     int i,j;
 22     for(i=1;i<r;i++)
 23     {
 24         if(map[i][c]==0)
 25             map[i][c]=1;
 26         else
 27             map[i][c]=0;
 28     }
 29     for(i=r+1;i<=4;i++)
 30     {
 31         if(map[i][c]==0)
 32             map[i][c]=1;
 33         else
 34             map[i][c]=0;
 35     }
 36     for(j=1;j<c;j++)
 37     {
 38         if(map[r][j]==0)
 39             map[r][j]=1;
 40         else
 41             map[r][j]=0;
 42     }
 43     for(j=c+1;j<=4;j++)
 44     {
 45         if(map[r][j]==0)
 46             map[r][j]=1;
 47         else
 48             map[r][j]=0;
 49     }
 50     if(map[r][c]==0)
 51         map[r][c]=1;
 52     else
 53         map[r][c]=0;
 54     return;
 55 }
 56 
 57 void dfs(int r,int c,int deep)
 58 {
 59     int i,j;
 60     if(deep==step)
 61     {
 62         flg=judge(map);
 63         return;
 64     }
 65     if(flg==1 || r>4)
 66     {
 67         return;
 68     }
 69     flip(r,c);
 70     ak[n]=r,AK[n]=c;
 71     n++;
 72     if(c<4)
 73         dfs(r,c+1,deep+1);
 74     else
 75         dfs(r+1,1,deep+1);
 76 
 77     if(flg!=1)
 78     {
 79         flip(r,c);
 80         n--;
 81     if(c<4)
 82         dfs(r,c+1,deep);
 83     else
 84         dfs(r+1,1,deep);
 85     }
 86     return;
 87 }
 88 
 89 int main()
 90 {
 91     int i,j;
 92     char x;
 93     memset(map,0,sizeof(map));
 94     for(i=1;i<=4;i++)
 95     {
 96         for(j=1;j<=4;j++)
 97         {
 98             scanf("%c",&x);
 99             if(x=='-')
100                 map[i][j]=1;
101         }
102         getchar();
103     }
104     for(step=0;step<=16;step++)
105     {
106         n=1;
107         dfs(1,1,0);
108         if(flg==1)
109             break;
110     }
111     printf("%d
",step);
112     for(i=1;i<=n-1;i++)
113     {
114         printf("%d %d
",ak[i],AK[i]);
115     }
116     return 0;
117 }
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4444466.html