The Sultan's successors ultraviolet (eight queens Problem)

Source: Internet
Author: User

Let's talk about:

In fact, this question is essentially an eight-queen question. The only difference is that each checkboard corresponds to a number. Finally, the output is required, and the corresponding solution occupies the maximum value of the sum of the lattice. You only need to calculate the result when the final solution is obtained. The following is a simple introduction to the eight queens issue. In fact, the solution is not difficult. Because each column in each row must have a pawn. Therefore, you only need to determine the number of pieces corresponding to each row. For the position where each piece is placed, there cannot be other pieces on the same column and diagonal lines. You only need to set an access array to save it. (Remember to trace back ). As for the expression of diagonal lines, for example, if the position is (x, y), one diagonal line can be represented by X + Y, and the other diagonal line can be represented by x-y, however, because the lower mark of the array is not negative, you can set the value of the second diagonal line to x-y + 7, which can be expressed by a two-dimensional array, you cannot place a piece in a corresponding position. For detailed analysis, see Liu rujia's algorithm competition getting started classic p123 eight queens question.

Source code:

# Include <stdio. h> # include <string. h> char vis [3] [17]; int C [9]; // The number of columns where the pawns of each row are stored. Int Val [9] [9]; int Max, num = 0; void search (INT); int main () {int K, I, j; // freopen ("data", "r", stdin ); scanf ("% d", & K); While (k --) {for (I = 1; I <= 8; I ++) for (j = 1; j <= 8; j ++) scanf ("% d", & Val [I] [J]); memset (VIS, 0, sizeof (VIS )); max = 0; search (1); printf ("% 5d \ n", max);} return 0;} void search (INT cur) {int I, sum; if (cur> 8) {sum = 0; for (I = 1; I <= 8; I ++) sum + = Val [I] [C [I]; num ++; max = sum> Max? Sum: Max;} else for (I = 1; I <= 8; I ++) if (! Vis [0] [I] &! Vis [1] [cur + I] &! Vis [2] [cur-I + 7]) {// same column, no other pawns on the same diagonal line are used. vis [0] [I] = vis [1] [cur + I] = vis [2] [cur-I + 7] = 1; c [cur] = I; search (cur + 1 ); vis [0] [I] = vis [1] [cur + I] = vis [2] [cur-I + 7] = 0; // pay attention to backtracking} return ;}


The Sultan's successors ultraviolet (eight queens Problem)

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.