18124 N Queen Question

Source: Internet
Author: User

18124 N Queen Question

Time limit: 2000MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0

Question types: programming language: g++; GCC; Vc

There are n*n chess board, asked to put N queen, asked any two queen will not kill each other, how many different ways?

Input format
Each number is T, which represents the number of case, t<=13 thereafter, one number per line N (13>=n>0)

Output format
Each case, the output corresponds to an answer

Input sample

Output sample



Scau-n Queen question-backtracking. Enter N to find out how many solutions are available to place n Queens on the N*n board, which is a eight queen problem, and the topic n is also very small, time back to 2000ms, even if the complexity of the violent backtracking O (n^2) can be too.    But here's an optimization tip.  The Queen can attack the same column and two pieces on the diagonal, then it is definitely a row of rows, and a row and a column can only be placed in one piece.  When judging the same column, open an array of row[maxn+5] to mark which columns are placed in the Queen.   For two diagonal lines, to determine if a pawn has been placed, just open another two-dimensional array diagonal[2][maxn*2+5]; Where Y-x is the main diagonal, y+x is the diagonal. But y-x may be negative, so write y-x+n; When judging the position J on the current cur line can be placed, the diagonal[0][cur-j+n] and Diagonal[1][cur+j] are marked as 1 to indicate that a piece has been placed on the diagonal of the bar.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cctype>6#include <cmath>7#include <algorithm>8#include <Set>9#include <map>Ten#include <queue> One#include <stack> A#include <utility> -#include <vector> - #definell Long Long the #defineINF 0x3f3f3f3f - using namespacestd; -  - introw[ -],diagonal[2][ -];//an array of marker columns and two diagonal lines + intCnt,n; - voidDfsintcur) + { A     if(cur==n+1)//To reach the recursive boundary at     { -cnt++; -         return; -     } -      for(intI=1; i<=n;i++) -{//determine if a pawn is already on the column or two diagonal lines in         if(!row[i]&&!diagonal[0][cur+i]&&!diagonal[1][cur-i+N]) -         { torow[i]=diagonal[0][cur+i]=diagonal[1][cur-i+n]=1; +DFS (cur+1); -row[i]=diagonal[0][cur+i]=diagonal[1][cur-i+n]=0; the         } *     } $ }Panax Notoginseng intMain () - { the     //freopen ("Input.txt", "R", stdin); +memset (Row,0,sizeof(Row)); Amemset (Diagonal,0,sizeof(diagonal)); the     intT; +scanf"%d",&t); -      while(t--) $     { $scanf"%d",&n); -Cnt=0; -Dfs1); theprintf"%d\n", CNT); -     }Wuyi     return 0; the}

18124 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.