Thread: Eight Queen Problem code. But I cannot understand.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    13

    Eight Queen Problem code. But I cannot understand.

    Eight Queen Problem code. But I cannot understand.
    Can anybody explain it for me briefly?
    Thank you so much!

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #define N 8 
    
    // int num; 
    
    void backtrack(int* column, int* rup, int* lup, int* queen, int); 
    
    int main(void) { 
        //num = 0;
        int column[N+1]; 
        int rup[2*N+1]; 
        int lup[2*N+1]; 
        int queen[N+1] = {0}; 
    
        int i; 
        for(i = 1; i <= N; i++) 
            column[i] = 1; 
        for(i = 1; i <= 2*N; i++) 
            rup[i] = lup[i] = 1; 
    
        backtrack(column, rup, lup, queen, 1); 
    
        return 0; 
    } 
    
    void printQueen(int* queen) {
        int x, y;
        // printf("\nAnswer %d\n", ++num);
        for(y = 1; y <= N; y++) {
            for(x = 1; x <= N; x++) {
                printf(" %c", queen[y] == x ? 'Q' : '.');
            }
            printf("\n");
        }
        printf("\n");
    }
    
    void backtrack(int* column, int* rup, int* lup, int* queen, int i) { 
        if(i > N) { 
            printQueen(queen);
        } 
        else { 
            int j;
            for(j = 1; j <= N; j++) { 
                if(column[j] == 1 && 
                     rup[i+j] == 1 && lup[i-j+N] == 1) { 
                    queen[i] = j; 
                    // mark
                    column[j] = rup[i+j] = lup[i-j+N] = 0; 
                    backtrack(column, rup, lup, queen, i+1); 
                    column[j] = rup[i+j] = lup[i-j+N] = 1; 
                } 
            } 
        } 
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    The main problem for me is I can not understand the two array rup[] and lup[].

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #define N 8 
    
    // int num; 
    
    void backtrack(int* column, int* rup, int* lup, int* queen, int); 
    
    int main(void) { 
        //num = 0;
        //creates arrays
        int column[N+1]; 
        int rup[2*N+1]; 
        int lup[2*N+1]; 
        int queen[N+1] = {0}; 
    
        int i; 
        // Fills columns with 1
        for(i = 1; i <= N; i++) 
            column[i] = 1; 
       // this is really strange, I don't know if it's possible, it equals rup[i] to lup[i] and lup[i] to 1 if I'm not wrong
        for(i = 1; i <= 2*N; i++) 
            rup[i] = lup[i] = 1; 
    
        backtrack(column, rup, lup, queen, 1); 
    
        return 0; 
    } 
    
    void printQueen(int* queen) {
        int x, y;
        // printf("\nAnswer %d\n", ++num);
        for(y = 1; y <= N; y++) {
            for(x = 1; x <= N; x++) {
                printf(" %c", queen[y] == x ? 'Q' : '.'); // I think that if queen[y] equals to x "Q" is printed, else "." is printed
            }
            printf("\n");
        }
        printf("\n");
    }
    
    void backtrack(int* column, int* rup, int* lup, int* queen, int i) { 
        if(i > N) { 
            printQueen(queen);
        } 
        else { 
            int j;
            for(j = 1; j <= N; j++) { 
                if(column[j] == 1 && 
                     rup[i+j] == 1 && lup[i-j+N] == 1) { 
                    queen[i] = j; 
                    // mark
                    column[j] = rup[i+j] = lup[i-j+N] = 0; 
                    backtrack(column, rup, lup, queen, i+1); 
                    column[j] = rup[i+j] = lup[i-j+N] = 1; 
                } 
            } 
        } 
    }
    It's interacting with the int arrays and printing..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with progress code
    By JFonseka in forum C Programming
    Replies: 13
    Last Post: 04-25-2008, 05:03 PM
  2. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  3. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM