Eight queen problem

Discussion in 'C Programming' started by ravi, Jul 21, 2007.

  1. ravi

    ravi Guest

    C/C++ program to find a possible solution of eight queen problem

    Can any one help me to get the solution ?
     
    ravi, Jul 21, 2007
    #1
    1. Advertising

  2. ravi

    santosh Guest

    On Jul 21, 11:27 am, ravi <> wrote:
    > C/C++ program to find a possible solution of eight queen problem
    >
    > Can any one help me to get the solution ?


    Look up these links:

    <http://en.wikipedia.org/wiki/Eight_queens_puzzle>
    <http://mathworld.wolfram.com/QueensProblem.html>
    <http://en.literateprograms.org/Eight_queens_puzzle_(C)>

    Your linked-list problems are general algorithmic ones. A search of
    this group, comp.programming, comp.algorithms or a web search should
    reveal lots of ideas.
     
    santosh, Jul 21, 2007
    #2
    1. Advertising

  3. ravi

    pete Guest

    ravi wrote:
    >
    > C/C++ program to find a possible solution of eight queen problem
    >
    > Can any one help me to get the solution ?


    /* BEGIN Q8.c */

    #include <stdio.h>
    #include <assert.h>

    #define SQUARES 8

    void column(int *row, int c, int squares);
    int ok(int *row, int c, int r);
    void solution(int *row, int squares);

    int main(void)
    {
    int row[SQUARES + 1];

    assert(sizeof row / sizeof *row - 1 != 0);
    row[0] = 0;
    column(row, 1, sizeof row / sizeof *row - 1);
    return 0;
    }

    void column(int *row, int c, int squares)
    {
    int r = squares;

    do {
    if (ok(row, c, r)) {
    row[c] = r;
    if (c == squares) {
    solution(row, squares);
    break;
    }
    column(row, c + 1, squares);
    }
    } while (--r != 0);
    }

    int ok(int *row, int c, int r)
    {
    const int p = c + r, m = c - r;

    do {
    --c;
    } while (c && r != row[c] && p != c+row[c] && m != c-row[c]);
    return c == 0;
    }

    void solution(int *row, int squares)
    {
    int c = 1;

    ++row[0];
    printf("\n\n\nSOLUTION # %d\n\n", row[0]);
    do {
    printf("COLUMN %d, ROW %d\n", c, row[c]);
    } while (c++ != squares);
    }

    /* END Q8.c */

    --
    pete
     
    pete, Jul 21, 2007
    #3
  4. ravi wrote:
    > C/C++ program to find a possible solution of eight queen problem
    >
    > Can any one help me to get the solution ?
    >


    Try drawing a "space filling" curve, or a Koch snowflake...
    much more fun. ;-)

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
     
    Charles Richmond, Jul 22, 2007
    #4
  5. ravi

    user923005 Guest

    On Jul 21, 6:56 am, pete <> wrote:
    > ravi wrote:
    >
    > > C/C++ program to find a possible solution of eight queen problem

    >
    > > Can any one help me to get the solution ?

    >
    > /* BEGIN Q8.c */
    >
    > #include <stdio.h>
    > #include <assert.h>
    >
    > #define SQUARES 8
    >
    > void column(int *row, int c, int squares);
    > int ok(int *row, int c, int r);
    > void solution(int *row, int squares);
    >
    > int main(void)
    > {
    > int row[SQUARES + 1];
    >
    > assert(sizeof row / sizeof *row - 1 != 0);
    > row[0] = 0;
    > column(row, 1, sizeof row / sizeof *row - 1);
    > return 0;
    >
    > }
    >
    > void column(int *row, int c, int squares)
    > {
    > int r = squares;
    >
    > do {
    > if (ok(row, c, r)) {
    > row[c] = r;
    > if (c == squares) {
    > solution(row, squares);
    > break;
    > }
    > column(row, c + 1, squares);
    > }
    > } while (--r != 0);
    >
    > }
    >
    > int ok(int *row, int c, int r)
    > {
    > const int p = c + r, m = c - r;
    >
    > do {
    > --c;
    > } while (c && r != row[c] && p != c+row[c] && m != c-row[c]);
    > return c == 0;
    >
    > }
    >
    > void solution(int *row, int squares)
    > {
    > int c = 1;
    >
    > ++row[0];
    > printf("\n\n\nSOLUTION # %d\n\n", row[0]);
    > do {
    > printf("COLUMN %d, ROW %d\n", c, row[c]);
    > } while (c++ != squares);
    >
    > }
    >
    > /* END Q8.c */
    >
    > --
    > pete

    /* short version: */
    #include<stdio.h>
    static int v,i,j,k,l,s,a[99];int main(void){for(s=8;*a-s;v=a[j*=v]-
    a,
    k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?
    (l^j)&1:2])
    &&++l||a<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:+
    +a)>=s*
    k&&++a[--i]);printf("\n\n");return 0;}
     
    user923005, Jul 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cfanatic
    Replies:
    3
    Views:
    561
    cfanatic
    Oct 16, 2003
  2. cj

    8-queen problem

    cj, Jun 21, 2004, in forum: C++
    Replies:
    5
    Views:
    6,702
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo
    Jun 21, 2004
  3. jblazi

    The eight queens problem

    jblazi, Aug 30, 2004, in forum: C++
    Replies:
    9
    Views:
    2,557
    Karl Heinz Buchegger
    Aug 30, 2004
  4. Jeff Epler
    Replies:
    10
    Views:
    667
    Anton Vredegoor
    Aug 20, 2003
  5. Matt

    "Eight Queens" program

    Matt, Aug 18, 2004, in forum: C Programming
    Replies:
    5
    Views:
    463
    -berlin.de
    Aug 21, 2004
Loading...

Share This Page