The Sultan's successors, the eight queens question

Source: Internet
Author: User

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 103



Question type: backtracking


Original question:

  The Sultan of Nubia has no children, so she has decided that the country will be split into up K Separate
Parts on her death and each part will be inherited by whoever performs best at some test. it is possible for any individual to inherit more than one or indeed all of the portions. to ensure that only highly intelligent people eventually become her successors,
The Sultan has devised an ingenious test. In a large hall filled with the splash of fountains and the delicate scent of incense have been placed K Chessboards.
Each chessboard has numbers in the range 1 to 99 written on each square and is supplied with 8 jewelled chess queens. the task facing each potential successor is to place the 8 queens on the chess board in such a way that no Queen threatens another one, and
So that the numbers on the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the Board contains exactly one queen, and each diagonal
Contains no more than one .)

Write a program that will read in the number and details of the chessboards and determine the highest scores possible for each board under these conditions. (You know that the Sultan is both a good chess
Player and a good mathematician and you suspect that her score is the best attainable .)

Question:

Sultan has no children, so she decided to do a test and select a heir. The test content is that there is an 8*8 chessboard, each of which has

1 ~ 99. Then, put the eight queens up so that they can not walk in the same column, with the same slash, and find the maximum sum of the digits in the eight placement locations

.

Analysis and Summary:

The naked eight queens problem was solved, but a summation step was added.

First, we can conclude that these eight queens must be on different rows, and then we can further determine that the column coordinates on each row are different, that is, exactly

Place a queen. In this way, you only need to determine whether there is a conflict on the diagonal line.

Next, we can open an array loc [9] = {,}. The array subscript corresponds to the row, and the value of the array element corresponds to the column of the row. We only need

Enumerate the full arrangement of LOC, and then write a judge function to determine whether the arrangement scheme complies with the eight queens rule. If yes, calculate the corresponding

Update and maintain a global maximum value.

0 ~ A total of 8! = 40320, and enumeration two will not exceed this.

1. Brute Force Enumeration

# Include <iostream> # include <algorithm> # include <cstring> # include <cstdio> # define mod using namespace STD; int map [8] [8]; int loc [8]; bool judge () {for (INT I = 1; I <8; ++ I) {for (Int J = 0; j <I; ++ J) {if (I-j = loc [I]-loc [J] | I-j = loc [J]-loc [I]) return false ;}} return true ;}int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int T; scanf ("% d", & T); While (t --) {for (INT I = 0; I <8; ++ I) for (Int J = 0; j <8; ++ J) scanf ("% d", & map [I] [J]); For (INT I = 0; I <8; ++ I) loc [I] = I; int maxval =-1; do {If (Judge () {int sum = 0; For (INT I = 0; I <8; ++ I) sum + = map [I] [loc [I]; If (sum> maxval) maxval = sum ;}while (next_permutation (LOC, loc + 8); printf ("% 5d \ n", maxval);} return 0 ;}

2. Backtracking

The basic idea of the backtracking method is similar to the above one. Recursive placement is performed row by row. When placement is performed, you must first determine whether there is a conflict with the previous one.

 # include 
  
    # include 
   
     # include 
    
      # include 
     
       using namespace STD; int map [8] [8], Loc [8], maxval; bool vis [8]; inline bool judge (INT cur, int col) {for (INT I = 0; I 
      
        maxval) maxval = sum;} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int t; scanf ("% d", & T); While (t --) {for (INT I = 0; I <8; ++ I) for (Int J = 0; j <8; ++ J) scanf ("% d ", & map [I] [J]); maxval =-1; memset (VIS, 0, sizeof (VIS); search (0, 0 ); printf ("% 5d \ n", maxval) ;}return 0 ;}
      
     
    
   
  

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 ,By d_double(For details, refer)

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.