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的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. [LeetCode] All solution

    比较全的leetcode答案集合: kamyu104/LeetCode grandyang

  3. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  4. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. 【leetcode】solution in java——Easy1

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...

  6. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...

  7. 【leetcode】solution in java——Easy4

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...

  8. 【leetcode】solution in java——Easy3

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...

  9. 【leetcode】solution in java——Easy2

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...

随机推荐

  1. js实现查找字符串中最多的字符的个数

    用hash table实现.key是字符,value是字符个数. var hashTable={}; var str="fjsdeiuwidshjfhjsksghfjhsjjskalsk&q ...

  2. JavaScript 中对变量和函数声明的“提前(hoist)”

    hoist vt.升起,提起; vi.被举起或抬高; n.起重机,升降机; 升起; <俚>推,托,举; 这篇文章不讲英语,但是对于某些英语单词找不到很好的翻译,一上来就列出“hoist”这 ...

  3. Ajax comet XMLHttpRequest 异步

    function createXHR() { if (typeof XMLHttpRequest != “undefi ned”){      return new XMLHttpRequest(); ...

  4. WebGL学习(2) - 3D场景

    原文地址:WebGL学习(2) - 3D场景 经过前面WebGL学习(1) - 三角形的学习,我们已经掌握了webGL的基础知识,也已经能够画出最基本的图形,比如点,线,三角形,矩形等.有了2D绘图的 ...

  5. OGEngine_2.x中BitmapFont加载后黑屏问题的解决办法

    在我使用OGEngine_2.x进行消灭圈圈(星星)游戏的实践的时候,使用BitmapFont对自定义字体进行调用. 原文字体教程如下:http://blog.csdn.net/OrangeGame/ ...

  6. Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 Tools Update

    ASP.NET MVC 3 can be installed side by side with ASP.NET MVC 2 on the same computer, which gives you ...

  7. 解决:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'requestMap.maintenancename != null and requestMap.maintenance

    异常如下:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Builde ...

  8. js 资料

    http://javascript.ruanyifeng.com/#introduction 2.MDN 火狐中文社区  https://developer.mozilla.org/zh-CN/doc ...

  9. C语言之计算log2

    #include<stdio.h>int main(){int num,count=0,i=0,ret=0;scanf("%d",&num);count=num ...

  10. 读Kafka Consumer源码

    最近一直在关注阿里的一个开源项目:OpenMessaging OpenMessaging, which includes the establishment of industry guideline ...