1. The n-queens puzzle is the problem of placing n queens on an nn 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:

  1. [
  2. [".Q..", // Solution 1
  3. "...Q",
  4. "Q...",
  5. "..Q."],
  6.  
  7. ["..Q.", // Solution 2
  8. "Q...",
  9. "...Q",
  10. ".Q.."]
  11. ]

 分析: The classic recursive problem.
1. Use a int vector to store the current state,  A[i]=j refers that the ith row and jth column is placed a queen.
2. Valid state:  not in the same column, which is A[i]!=A[current], not in the same diagonal direction: abs(A[i]-A[current]) != r-i

3. Recursion: 
       Start:   placeQueen(0,n)
        if current ==n then print result
        else
            for each place less than n,
                 place queen
                if current state is valid, then place next queen   place Queen(cur+1,n)
           end for
        end if

  1. class Solution {
  2. public:
  3. void record(vector<int> row)
  4. {
  5. vector<string> temp;
  6. for(int i = ; i< n ; i++)
  7. {
  8. string str(n,'.');
  9. str[row[i]] = 'Q';
  10. temp.push_back(str);
  11. }
  12. res.push_back(temp) ;
  13. }
  14. bool isValid(vector<int> row, int curRow)
  15. {
  16. for(int i = ; i< curRow; i++)
  17. if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
  18. return false;
  19.  
  20. return true;
  21. }
  22. void nqueue(vector<int> row,int curRow)
  23. {
  24. if(curRow == n)
  25. {
  26. record(row);
  27. return ;
  28. }
  29. for(int i = ; i< n ;i++)
  30. {
  31. row[curRow] = i;
  32. if(isValid(row,curRow))
  33. nqueue(row,curRow+);
  34. }
  35. }
  36. vector<vector<string> > solveNQueens(int n) {
  37. // Start typing your C/C++ solution below
  38. // DO NOT write int main() function
  39. res.clear();
  40. if( n < ) return res;
  41. this->n = n;
  42. vector<int> row(n,-);
  43. nqueue(row, );
  44. return res;
  45. }
  46. private:
  47. int n;
  48. vector<vector<string> > res;
  49. };

http://yucoding.blogspot.com/2013/01/leetcode-question-59-n-queens.html

LeetCode_N-Queens的更多相关文章

  1. Jeff Somers's N Queens Solutions 最快的n皇后算法

    /* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...

  2. [CareerCup] 9.9 Eight Queens 八皇后问题

    9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...

  3. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  4. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  5. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  6. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  7. Pat1128:N Queens Puzzle

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  8. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

  9. kolla queens on centos7.5 -all in one

    目录 环境准备 开始配置 快照,快照,快照 pull镜像并部署 登录配置OpenStack 环境准备 我这里用workstation创建了一个虚拟机,安装centos7.5 mini系统,这台虚拟机上 ...

  10. openstack系列文章(1)devstack安装测试Queens

    1.在OpenStack 圈子中,有这么一句名言:”不要让朋友在生产环境中运行DevStack.但是初学者在没有掌握OpenStack CLI的情况下用devstack安装测试环境还是不错的.本系列文 ...

随机推荐

  1. LeetCode_Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. Unit Test相关问题汇总

    1.测试私有方法(1)使用反射 public class Calcutate { public int test() { return add(2, 3); } private int add(int ...

  3. Linux下cut命令用法

    1 一两句话描述一下cut命令吧! 正如其名,cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的. cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期 ...

  4. BZOJ2751: [HAOI2012]容易题(easy)

    2751: [HAOI2012]容易题(easy) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 872  Solved: 377[Submit][S ...

  5. cf492E Vanya and Field

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. cf443B Kolya and Tandem Repeat

    B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. poj1220:高精度进制转换模板题

    今天撸3708  一直奇怪的re 就先放下了,写这个题的过程中学习了一个高精度进制转换,用这个模板写了1220 记录一下: #include <iostream> #include < ...

  8. <php>PDO链接方法

    <?php //定义数据源 $dsn = "mysql:dbname=mydb;host=localhost"; //$dsn = "sqlsrv:dbname=m ...

  9. java之File

    1.文件创建.重命名.删除 code: package com.test; import java.io.File; import java.io.IOException; public class ...

  10. MySql 查询表字段数

    MySql 查询表字段数 SELECT COUNT(*) FROM information_schema.columns WHERE table_schema='test_cases' AND tab ...