Java and Algorithm (6)-eight Queen's question

Source: Internet
Author: User

Put eight queens on the 8x8 chess, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum.

(text and pictures from Baidu Encyclopedia)

If you put the Queen, you can use a way of thinking: Drop a Queen in the leftmost column, and then in the right column from top to bottom to find the first place with the left Queen does not conflict with the second Queen, and then to yo one column, from top to bottom to find the first two queen in the position of the third Queen, and so on, Until the eighth Queen is placed in the last column.

Serious thinking, you can find that this is still the depth of the first search idea, that is, stepping forward, the next thing to do is the same as at present. Code:

[Java]View PlainCopyprint?
  1. Public class Dfseightqueens {
  2. int[] Queens = new int[8]; //Record the position of the Queen in each column
  3. int count = 0; //Pendulum total
  4. public void Dfs (int column) {
  5. if (column = = 8) { //8 queens have been placed
  6. count++;
  7. System.out.println ("n" + Count + "method:");
  8. Print ();
  9. return;
  10. }
  11. For (int i = 0; i < 8; i++) {
  12. Queens[column] = i; //Place the Queen on line I of the column
  13. if (isValid (column)) //Check whether the position is in conflict with the Queen of the former column-1 column
  14. DFS (column + 1); //No conflict start the next 8-position attempt
  15. }
  16. }
  17. Private Boolean isValid (int column) {
  18. for (int i = 0; i < column; i++) { ///column above the Queen compared to the front column-1 Queen
  19. if (queens[i] = = Queens[column]) //Two queens on the same line
  20. return false;
  21. if (Math.Abs (Queens[i]-queens[column]) = = (column-i)) //Two queens on the same diagonal
  22. return false;
  23. }
  24. return true;
  25. }
  26. private void print () {
  27. For (int i = 0; i < 8; i++) {
  28. For (int j = 0; j < 8; j + +) {
  29. if (queens[i] = = j)
  30. System.out.print ("*");
  31. Else
  32. System.out.print ("_");
  33. }
  34. System.out.println ();
  35. }
  36. }
  37. public static void Main (string[] args) {
  38. Dfseightqueens q = new Dfseightqueens ();
  39. Q.dfs (0);
  40. System.out.println ("Total" + Q.count + "method of placement");
  41. }
  42. }
Output:

[Java]View PlainCopyprint?
    1. A total of 92 kinds of placement methods

Java and Algorithm (6)-eight Queen's question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.