zoukankan      html  css  js  c++  java
  • 枚举(+-)

    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
  • 相关阅读:
    nyoj 202红黑树 (搜索)
    POJ 3281 Dining(最大流)
    nyoj-488 素数环 +nyoj -32 组合数 (搜索)
    LeetCode100:Same Tree
    LeetCode283:Move Zeros
    Leetcode226:Invert Binary Tree
    LeetCode258:Add Digits
    Leetcode237:Delete Node in a Linked List
    LeetCode7:Reverse Integer
    LeetCode292:Nim Game
  • 原文地址:https://www.cnblogs.com/baoluqi/p/3745352.html
Copyright © 2011-2022 走看看