• 枚举(+-)


    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

    具体代码:

    #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
    ",number) ;
      for (i=0 ; i<number ; i++){
       printf("%d %d
    ",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
  • 相关阅读:
    用css3弹性盒子模型实现九宫格布局
    css布局之左中右结构总结
    内联块inline-block的垂直间隙问题
    Jquery选择器 讲解
    U大师装系统
    陈伟:软件趋势——云物移大智
    asp网站发布步骤总结
    MYfirst
    ASP.net 利用From实现上传文件
    【ASP.NET Core CentOS 发布】(四)在CentOS上安装.NET Core运行时、部署到CentOS
  • 原文地址:https://www.cnblogs.com/baoluqi/p/3745352.html
Copyright © 2020-2023  润新知