leetcode N-Queens I && N-Queens II
第一个的代码:
#include<iostream>
#include<vector> using namespace std; bool isLegal(int i, int j, vector<string> ¤t)
{
int size = current.size();
int x = i-, y = j;
while (x >= )
{
if (current[x][y] == 'Q')
return false;
x--;
}
x = i-;
y = j - ;
while (x >= && y >= )
{
if (current[x][y] == 'Q')
return false;
x--;
y--;
}
x = i - ;
y = j + ;
while (x >= && y < size)
{
if (current[x][y] == 'Q')
return false;
x--;
y++;
}
return true;
} void getResult(int row, int index, vector<vector<string>> &result, vector<string> ¤t, int size)
{
if (row == size)
result.push_back(current);
else
{
while (index < size)
{
current[row][index] = 'Q';
if (isLegal(row, index, current))
getResult(row + , , result, current, size);
current[row][index] = '.';
index++;
}
}
} vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> result;
string s = "";
for (int i = ; i < n; i++)
s.push_back('.');
vector<string> current(n, s);
getResult(, , result, current, n);
return result;
} int main()
{
vector<vector<string>> result = solveNQueens();
for (int i = ; i < result.size(); i++)
{
for (int j = ; j < result[i].size(); j++)
cout << result[i][j].c_str() << endl;
cout << "-_________________________________________-" << endl;
}
return ;
}
第二个的代码:
得,忘了保存了,算了,不贴了,这题也就做到这份上了,讨论区里那个用位的我真是佩服死你啦。。。。。!!!!!!!!!
leetcode N-Queens I && N-Queens II的更多相关文章
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 【python】Leetcode每日一题-反转链表 II
[python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- leetcode面试准备:Contains Duplicate I && II
1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
随机推荐
- STM32从boot跳转到app失败
现象:在每次boot执行完跳转到APP时,都会跑飞 原因:在boot中使用到了USART和TIM,boot执行完没有关闭总中断 方法:在boot执行完跳转之前关闭中断,__disable_irq() ...
- Array数组(PHP学习)
什么是数组? 答:就是一组数. 数组的创建: <?php $Arr = array('姓名'=>'张三','身高'=>'174','家乡'=>'上海'); print_r($A ...
- Redis官方文档》持久化
原文链接 译者:Alexandar Mahone 这篇文章从技术层面描述了Redis持久化,建议所有读者阅读.如果希望更多了解Redis持久化和持久性保障,建议阅读Redis持久化揭秘. Redis ...
- mysql实战优化之四:mysql索引优化
0. 使用SQL提示 用户可以使用use index.ignore index.force index等SQL提示来进行选择SQL的执行计划. 1.支持多种过滤条件 2.避免多个范围条件 应尽量避免在 ...
- 混搭下的C与C++内存操作
源自最近遇到一个的问题,先介绍一下背景.项目中混用了C与C++编程范式,鉴于项目成员背景不一,每个模块的负责人可以自行2选1.同时为了提高效率,C范式的模块被允许使用STL库的部分容器(其实也就仅仅大 ...
- python's ninth day for me
函数 函数的定义与调用: #def 关键字 定义一个函数. # my_len 函数名, 函数名的书写规则与变量的命名一致. # def 与函数名中间一个空格. # 函数名() : 加上冒号. ...
- 初学Python--列表(List)
1.索引 列表中的元素类型未必统一,如: listExample=[1,2,'a','b'] 元素下标索引以0开始 firstEle=listExample[0] 不能进行越界索引,但可以倒序索引 l ...
- Android P2P语音通话实现
1.http://www.cnblogs.com/milospooner/archive/2012/07/13/2590950.html 2.http://my.oschina.net/sanshan ...
- NetworkView
[游戏Server中Server的类别] There are two common and proven approaches to structuring a network game which ...
- 粗粒度(Coarse-grained)vs细粒度(fine-grained)
在读的一篇文献中关于RDF的描述: As we know, RDF data is a set of triples with the form (subject, property, object) ...