码迷,mamicode.com
首页 > 其他好文 > 详细

枚举(+-)

时间:2014-05-26 22:16:23      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

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

具体代码:

bubuko.com,布布扣
#include <stdio.h>
#include <stdlib.h>
int a[4][4],number,re[16][2],temp[16][2],step ;
void change (int i,int j) ;
int judge() ;
int binary (int k , int l , int n) ;
int main ()
{
 int i,j,k ;
 char s[6] ;
 number=17 ;
 for (i=0 ; i<4 ; i++){
  scanf("%s",s) ;
  for (j=0 ; j<4 ; j++){
   if (s[j]==-) 
    a[i][j]=1 ;
   else  
    a[i][j]=0 ;
  }
 }
 step=0 ;
  binary(0,0,0) ;
  printf("%d\n",number) ;
  for (i=0 ; i<number ; i++){
   printf("%d %d\n",re[i][0],re[i][1]) ;
  }
 return 1 ;
}
void change (int i,int j)
{
 
 int  k,l ;
 a[i][j] = 1-a[i][j] ;
 for (k=0 ; k<4 ; k++)
  a[k][j] = 1-a[k][j] ;
 for (l=0 ; l<4 ; l++)
  a[i][l]= 1-a[i][l] ;
}

int judge()
{
 int i,j ;
 for (i=0 ; i<4 ; i++){
  for (j=0 ; j<4 ; j++){
   if (a[i][j]!=1)
    return 0 ;
  }
 }
 return 1 ;
}
 
int binary (int k , int l , int n)
{
  int m ;
  if (judge()){
   if (number>n){
    number=n ;
    for (m=0 ; m<n ; m++){
     re[m][0] = temp[m][0] ;
     re[m][1] = temp[m][1] ;
    }
   }
   return 1 ;
 }
 if (k<4 && l<3){
  binary (k,l+1,n) ;
  change(k,l) ;
  temp[step][0] = k+1 ;
  temp[step][1] = l+1 ;
  step++ ;
  binary(k,l+1,n+1) ;
  change(k,l) ;
  step-- ;
 }
 else if (k<4 && l==3){
  binary(k+1,0,n) ;
  change(k,l) ;
  temp[step][0] = k+1 ;
  temp[step][1] = l+1 ;
  step++ ; 
  binary(k+1,0,n+1) ;
  change(k,l) ;
  step-- ;
 }
 return 0 ;
}
View Code

 

枚举(+-),布布扣,bubuko.com

枚举(+-)

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/baoluqi/p/3745352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!