N-queens N Queen Question

Source: Internet
Author: User

Title: The n-queens Puzzle is the problem of placing N Queens in an nxn chessboard such th At No, Queens attack. (Leetcode 51)

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens ' placement, where ‘Q‘ and ‘.‘ both Indic Ate a queen and an empty space respectively.

For example,
There exist-distinct solutions to the 4-queens puzzle:

[ [". Q.. ",  //solution 1  " ... Q ",  " Q ... ",".  . Q. "], ["]. Q. ",  //Solution 2  " Q ... ",  " ... Q ",  ". Q.. "]

Reference frame:

class Solution {public:    vector<vector<string>> solvenqueens (  int  N) {        //code    }};

1. The Queen can be attacked in a straight line and slash in chess. That is to ask the N queen on the Board of the NxN not appear peers, the same column, with a slash to place.

The Queen's question was extended by the eight Queen's question. The N queen problem only has a solution when n=1 or n>=4.

2.N Queen problem is a classic case of backtracking, try to solve this problem with naive's backtracking method first.

N-queens 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.