Algorithm a little bit every day (4) - recursive algorithm: sub-issue book

A seemingly simple question no clue -_- ||  

Title: There are number 1 ... n of n book, ready to give n individuals, each person reading interests be described by a two-dimensional array,

    1: 0 like this book: not like the book

    like [i] [j] = 1, i j books like

    like [i] [j] = 0, i do not like books j

    How to solve the sub-books satisfy everyone

n n individual book. . . .

 

Analysis: The answer according to the book, can be understood:

 

Such as:

6

100 000 (like the first book, did not like the first book 2,3,4,5,6)

0 1 1 1 1 1

1 1 0 0 0 0

0 0 0 0 1 0

1 1 0 0 1 1 

1 0 1 0 0 0

 

 

  • The first person: I chose the first book, the rest of your own distribution, it can be a staff? (Recursive layer 1)
  • Second man: I chose the second book the rest of your own distribution, it can be a staff? (Recursive layer 2)
  • Third person: No! I just like the first and second present present, recursion fails, returning the recursive layer 2 (layer 3 recursive)
  • The second person: that I chose this third, and the rest of your own distribution, it can be a staff? (Recursive layer 2)
  • Third person: I chose the second book the rest of your own distribution, it can be a staff? (Recursive layer 3)
  • The fourth person: I chose the fifth book, and the rest of your own distribution, it can be a staff? (Recursive layer 4)
  • A fifth person: I chose the sixth book, the rest of your own distribution, it can be a staff? (Recursive layer 5)
  • Sixth man: No, I did not like the book (recursive layer 6)
  • Fifth man: No, this can not be assigned (recursive layer 5)
  • Fourth Man: No, this can not be assigned (recursive layer 4)
  • Third man: No, this can not be assigned (recursive layer 3)
  • Second man: I choose the fourth book, the rest of your own distribution, it can be a staff? (Recursive layer 2)
  • Third person: I chose the second book the rest of your own distribution, it can be a staff? (Recursive layer 3)
  • The fourth person: I chose the fifth book, and the rest of your own distribution, it can be a staff? (Recursive layer 4)
  • A fifth person: I chose the sixth book, the rest of your own distribution, it can be a staff? (Recursive layer 5)
  • Sixth man: I chose the third book, so you can hand a recursive end (recursive layer 6)

 

 

Although understanding has not been difficult, but started writing code that is killing me ...... because I do not really strong sense of logic.

Code barely finished, barely understand, also we need to look back to see more ah. . Really worried about my future. .

:( wrote the following code as a record, taking the time to find a big brother to give me a stroke stroke ...)

 

#include <the iostream>
 the using  namespace STD; 

#define N 550
 int like [N] [N];              //   like [i] [j] =. 1 i-j-book personal favorite (0 dislike) 
int answer [ N], n-;             //   answer [m] m = i records the selected i-th individual book 
BOOL selected [N];            //   selected [i] = i-book. 1 has been selected (0 = not selected ) 

BOOL Search ( int m) {
     IF (m == n-) {
         return  to true ; 
    } 
    for ( int I = 0 ; I <n-; I ++ ) {
         IF ! (Selected [I] &&like [m] [i]) { 
            answer [m] = i + . 1 ;             // m-i-book selected individuals; 
            Selected [i] = . 1 ;
             IF (Search (m + . 1 ))
                 return  to true ;             // first m + 1 individual does not like a book, the book returned i-th 
            selected [i] = 0 ; 
        } 
    } 
    return  to false ; 
} 

int main () { 
    COUT << " ----- ---- start selected from the book " << endl; 
    cout << " Please enter this number of people altogether: "<< endl;
     the while (Scanf ( " % D " !, & N-) = the EOF) { 
        Memset (Selected, 0 , the sizeof (Selected)); 
        Memset (answer, 0 , the sizeof (answer));
         for ( int I = 0 ; I <n-; I ++ ) {
             for ( int J = 0 ; J <n-; J ++ ) { 
                the printf ( " % d of whether the individual likes of% d book, like 1, dislike 0: " , I + 1 , J + . 1 ); 
                Scanf ( "% d " , & like [I] [J]);
                 IF (Search ( 0 )) { 
                    the printf ( " % d of individual choice of the book% d \ n- " , . 1 , answer [ 0 ]);
                     for ( int I = . 1 ; I <n-; I ++ ) { 
                        the printf ( " % d of individual choice of% d of the book " , I + . 1 , answer [I]); 
                        the printf ( " \ n- " ); 
                    } 
                } 
            } 
        }
        cout < <" ----- round ended ----- " << endl; 
    } 
    return  0 ; 
}

Guess you like

Origin www.cnblogs.com/yinniora/p/12109287.html