UVa 167:the Sultan ' s successors, eight queens problem

Source: Internet
Author: User

Topic Link:

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

Topic Type: Backtracking

Original title:

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

Write a program that would read in the number and details of the chessboards and determine the highest scores possible for Each board under these conditions. (You know this Sultan is both a good chess player and a good mathematician and your suspect that her Attainable.)

The main effect of the topic:

Sultan had no children, so she decided to do a test and choose an heir. The test content is, there is a 8*8 board, on each of the squares on the board has

A 1~99 number. Then, put 8 queens up, so that they can not walk, the same column, the same slash, and find 8 places in the number of positions and the largest

The value.

Analysis and Summary:

The naked eight queen question, just added a summation step.

First of all, it can be concluded that these 8 queens must be on different lines, and then, you can further determine that the column coordinates on each row are different, that is, exactly every row per column

Place a queen. This way, you just need to judge whether there is a conflict on the slash.

Then it's okay, we can open an array loc[9] = {0,1,2,3,4,5,6,7}, the array subscript corresponds to the row, and the value of the array element corresponds to the row's column. All we need

Enumerate the LOC, and then write a judge function to determine if the arrangement meets the eight-queen rule and, if so, to compute the corresponding

Value and the update maintains a global maximum value.

A total of 8 0~7! = 40,320, and enum two does not exceed this.

1. Violence Enumeration Act

#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; }

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.