# Leetcode: My solution N-queens

Source: Internet
Author: User
N-Queens total accepted: 15603 total submissions: 60198my submissions

The N-Queens puzzle is the problem of placing n queens on an n × n chessboard such that no two queens attack each other.

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 indicate a queen and an empty space respectively.

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

`[ [".Q..",  // Solution 1  "...Q",  "Q...",  "..Q."], ["..Q.",  // Solution 2  "Q...",  "...Q",  ".Q.."]]`
`public class Solution {  public List<String[]> solveNQueens(int n) {        List<String[]> result = new ArrayList<String[]>();        if ( n < 1) {            return result ;        }        List<List<Integer>> storeArray = new ArrayList<List<Integer>>();        helper(n, new ArrayList<Integer>(), storeArray);result = drawPicture(storeArray,n);        return result;    }        private List<String[]> drawPicture(List<List<Integer>> storeArray ,int n) {    List<String[]> drawedPicture = new ArrayList<String[]> ();    for (int i = 0; i < storeArray.size();i++) {    String[] str = new String[n];    for (int j = 0; j < n; j++) {        str[j] = new String("");    for (int w = 0; w < n; w++) {    if (storeArray.get(i).get(j) == w) {    str[j] +="Q";    } else {    str[j] +=".";    }    }        }    drawedPicture.add(str);    }    return drawedPicture;}boolean isValid(ArrayList<Integer> cols, int col) {       int newX = col;       int newY = cols.size();    for (int y = 0; y < cols.size(); y++) {    int x = cols.get(y);        if (newX == x) {    return false;    }    if (newX - newY == x - y) {    return false;    }    if (newX + newY == x + y){    return false;    }    }    return true;    }    public void helper(int n, ArrayList<Integer> cols,  List<List<Integer>> storeArray) {    if (cols.size() == n) {    storeArray.add(new ArrayList<Integer>(cols));    return;    }    for (int i = 0; i < n; i++) {    if (!isValid(cols, i)) {    continue;    }    cols.add(i);    helper(n, cols, storeArray);    cols.remove(cols.size() - 1);    }    }}`

Leetcode: My solution N-queens

Related Keywords:

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.