HDU2553 N Queen Question

Source: Internet
Author: User

Problem Link: HDU2553 n queen problem. Basic Training questions, written in C language program.

Originally did solve the problem of the N queen of the procedure, it is easy to rewrite a bit. For procedures, see: Eight Queens (n Queens) problem algorithm program.

One submission, "Time Limit exceeded", had to hit the table first. The original code comment was left there.

This is a classic backtracking procedure, is to use the recursive return to achieve, in fact, can also be implemented with non-recursive return. Backtracking does not have to be done with recursion.

The details of the program still need to be noted, such as when playing a table, you need to declare more than one element, because the array subscript is starting from 0.

The AC program is as follows:

/* HDU2553 N Queen Problem */#include <stdio.h> #define TRUE 1#define FALSE 0#define maxqueen 10#define ABS (x) ((x>0)? (  x):-(x)//* For the absolute value of x *//* Store 8 Queens of the column position, the array is labeled as the Queen's column position */int Queen[maxqueen];int total_solution;    /* Calculates a total of several sets of solutions *//* test whether the Queen on (Row,col) is under attack and returns a value of 1 if attacked, otherwise returns 0 */int attack (int row, int col) {int i, atk=false;    int Offset_row, Offset_col;    i=0;        while (!atk && i<row) {offset_row=abs (i-row);        Offset_col=abs (Queen[i]-col); /* Determine if the two queens are in the same column, whether on the same diagonal *//* If the two queens are in the same column or diagonal, then the attack occurs, atk==true */ATK = (queen[i] = = col) | |        (Offset_row = = Offset_col);    i++; } return ATK;}    /* Recursive placement of Queen subroutine */void place (int q, int n) {int i=0;             while (I < n) {if (!attack (q, i))/*/Queen not attacked */{queen[q]=i;   /* Store Queen's column position */* To determine if a set of solutions */if (q = = n-1) total_solution++;      /* Get a solution */else place (q+1, n); /* Otherwise continue to put the next Queen */} i++;   }}int Main (void) {int n;    int ans[maxqueen+1], I;        Because "time Limit exceeded", had to first hit the table for (i=1; i<=maxqueen; i++) {//Queen traversal total_solution = 0;                Place (0, I);    /* Starting from the No. 0 queen to the board */ans[i] = total_solution;        } while (scanf ("%d", &n)! = EOF) {//Judgment end Condition if (n = = 0) break;                Queen Traversal//total_solution = 0;//Place (0, N);        /* Starting from the No. 0 queen to the board *///output//printf ("%d\n", total_solution);    printf ("%d\n", Ans[n]); } return 0;}


HDU2553 N Queen 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.