| |

VerySource

 Forgot password?
 Register
Search
View: 790|Reply: 6

Help! Eight Queens Question (VC ++), please

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-6 11:00:01
| Show all posts |Read mode
On an 8 * 8 chess board, there are eight queens, one for each queen. It is required that there should be no "attack" between queens. Neither two queens can be in the same row, same row or On the same diagonal. Ask how many different methods are available.
Claim
1) The method and steps to solve the problem, and the specific plan, require the flow chart of program execution.
2) Detailed procedure list.
3) Problems during program writing and debugging and methods to solve them.
4) The gains in this training and the shortcomings of the procedures.
Reply

Use magic Report

1

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-27 20:15:01
| Show all posts
A lot online
Reply

Use magic Report

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-28 18:15:01
| Show all posts
I found it, but didn't have what I want (no answer to the request later)
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-3-29 21:30:01
| Show all posts
Find a few more algorithms, take a moment to read all the programs, compare them with each other, and you can make a difference ^ _ ^
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-30 16:15:01
| Show all posts
/ * This program uses the "exhaustive method" to solve the eight queens problem. File name BHH.C TC2.0 debug passed * /
/ * Set an 8 * 8 chessboard as shown below: * /
/ * 1 2 3 4 5 6 7 8 * /
/ * 1: 0 0 0 0 0 0 0 0 * * /
/ * 2: 0 * 0 0 0 0 0 0 0 * /
/ * 3: 0 0 0 * 0 0 0 0 * /
/ * 4: * 0 0 0 0 0 0 0 0 * /
/ * 5: 0 0 0 0 0 0 0 * 0 * /
/ * 6: 0 0 0 0 * 0 0 0 * /
/ * 7 : 0 0 * 0 0 0 0 0 * /
/ * 8: 0 0 0 0 0 0 * 0 0 * /
/ * An 8-bit sequence can be used to indicate the position of 8 queens, such as "82417536", which means to sequentially list the sequence number of the queen's column in each row * /
/ * The question is translated into the following two points: * /
/ * 1. Generate an 8-bit sequence P and make each number in the sequence not duplicate, that is to ensure different columns (different rows can be avoided when defining the sequence). * /
/ * 2. Find a sequence in the sequence that satisfies (1), so that the absolute value of the difference between any two digits in the sequence is not equal to their sequence number * /
/ * The absolute value of the difference, that is, there are no two queens on the same diagonal of the board. * /
/ * All sequences satisfying (2) are solutions to the eight queens problem. * /
#include <stdio.h>
#include <math.h>
#define N 8 / * Define the size of the board by adjusting the value of N * /
void check (int p []) / * The function check is used to check whether the sequence P satisfies (2) * /
  {int i, j;
   for (i = 0; i <N-1; i ++)
     for (j = i + 1; j <N; j ++)
       if (abs (p [j] -p [i]) == j-i) return; / * If it is not satisfied, return to the function permute and continue to find the next P * /
   for (printf (""), i = 0; i <N; printf ("% d", p [i ++])); / * print the result of the sequence satisfying (2) * /
  }
/ * Function permute uses recursion to find the sequence P satisfying (1) * /
void permute (int n, int p []) / * n means the current line has been found (set up from line 8), use P [] to save the sequence * /
  {int i, j;
   if (n == 1) check (p);
   for (i = 0, j = n-1; i <N; i ++) / * Find the next row where the queen can be placed (P [i]! = 0) corresponding to the column i that has been dropped * /
     if (! p [i]) {p [i] = j; permute (j, p); p [i] = 0;}
  }
main ()
  {int p [N] = {0}, n = N; / * Define the array p [N] to store the result sequence, n is the line number * /
   printf ("\n");
   permute (n + 1, p); / * call permute function to search * /
  }
Reply

Use magic Report

1

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-30 17:00:01
| Show all posts
void queen8 (int depth)
{
 if (depth == 8)
 {
 for (int x = 0; x <8; x ++)
{
 for (int y = 0; y <8; y ++)
  {
    if (y == number [x])
    queen [x] [y] == 1;
    else queen [x] [y] = 0;
   }
}
}
for (x = 0; x <8; x ++)
{
 for (y = 0; y <8; y ++)
 cout << queen [x] [y] << "";
 cout << endl;
}
 
for (int i = 0; i <8; i ++)
 {
   for (int j = 0; j <i; j ++)
     if (i == number [j])
     goto next;
   if (number [depth-1] == i-1&&number [depth] == i)
    goto next;
   number [depth] = i;
   queen8 (depth + 1);
   next:
 }
}
void main ()
{
queen8 (0);
}
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-30 18:30:01
| Show all posts
Yang Leibao made it
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list