Anyone knows the shortest solution of 8 queens problem?



The Problem is:


Place 8 queens in a 8x8 chessboard and no queen is

attacking others.


Thanks a lot.


Laybe.


Comments

  • :

    : The Problem is:


    : Place 8 queens in a 8x8 chessboard and no queen is

    : attacking others.


    : Thanks a lot.


    : Laybe.




    Make sense. Does anything you say mean anything.......

    NOPE




  • Yup. I could solve the problem, probably with a recursive placement with backtracking and some heuristics to prune the decision tree a little. I could also write a brute force mechanism to place them all.


    However, I don't post code. Either do it yourself, or ask a question about advice on getting started or on some technical point about the language.




  • Hey,

    Well, here's the recursive code, called exhaustive

    search that uses backtracking. I had it in Java

    and "translated" it into C++ just now without

    testing. Small (or large) bugs might exist:


    class Queens

    {

    private:

    bool SolutionFound;

    bool column[9];

    bool upleft[15];

    bool upright[17];

    int places[9];


    public:

    Queens();

    bool try(int row);

    bool safe(int r, int c);

    void PrintSolution();

    }


    Queens::Queens()

    {

    int i;

    for (i = 1; i < 7; i++)

    column[i] = true;

    for (i = 0; i < 14; i++)

    upleft[i] = true;

    for (i = 2; i < 16; i++)

    upright[i] = true;

    }

    bool Queens::try(int row)

    {

    bool answer;

    int col = 0;

    do {

    col++;

    if (safe(row, col))

    {

    places[row] = col;

    column[col] = false;

    upleft[row-col+7] = false;

    upright[row+col] = false;

    if (row < 8)

    {

    answer = try(row+1);

    if (!answer)

    {

    column[col] = true;

    upleft[row-col+7] = true;

    upright[row+col] = true;

    }

    else

    return answer;

    else

    return true;

    }

    }

    while(col < 8);

    return answer;

    }


    bool Queens::safe(int r, int c)

    {

    return (column[r] && upleft[r-c+7] && upright[r+c])

    }


    void Queens::PrintSolution()

    {

    for (int i = 1; i < 8; i++)

    cout << "Row " << i << " Column " <<<br>
    places[i] << endl;<br>
    }


    int main()

    {

    Queens q();

    bool SolutionFound = q.try(1);

    if (SolutionFound)

    q.PrintSolution()

    else

    { /* No Solution */ }

    }




    :

    : The Problem is:


    : Place 8 queens in a 8x8 chessboard and no queen is

    : attacking others.


    : Thanks a lot.


    : Laybe.





  • : :

    : : The Problem is:


    : : Place 8 queens in a 8x8 chessboard and no queen is

    : : attacking others.


    : : Thanks a lot.


    : : Laybe.


    :

    : Make sense. Does anything you say mean anything.......

    : NOPE


    It makes perfect sense. Ever hear of CHESS? Is it above your level of understanding?


    Here's his message in a form you may be able to understand:


    Put 8 QUEEN CHESS PIECE on an 8x8 CHESS BOARD grid in such a way that none of the queens, if they were on opposing teams, would kill each other on the first move.


    NOW do you understand?


    -Xotor-


  • All you've likely accomplished is to give him something to just hand in.


    Sigh.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories