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 ...
随机推荐
- linux(centos7) 安装nginx
linux(centos7) 安装nginx 1.14(stable) 版本 Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内, ...
- FPGA论剑(续)
25年之后,第二次华山论剑之时,天下第一的王重阳已然仙逝,郭靖少年英杰刚过二十岁,接东邪黄药师.北丐洪七公300招不败,二人默认郭靖天下第一.南帝段智兴因为出家,法号“一灯”,早已看破名利,故没有参加 ...
- Does Windows have a limit of 2000 threads per process?
http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx Often I see people asking why the ...
- OBS第三方推流直播教程
第三方推流使用场景 1.当使用YY客户端进行直播遇到问题,暂无解决方法的时候,可以使用第三方直播软件OBS进行推流. 2.对OBS情有独钟的主播. OBS简介: OBS是一款比较好用的开源直播软件,目 ...
- 【转】使用JMeter 完成常用的压力测试(二)
使用JMeter 完成常用的压力测试 Login.jsp 和welcome.jsp.其中 login.jsp 负责生成 User 对象,并调用 User 的login.当 login 返回为 true ...
- 用一两句话说一下你对“盒模型”这个概念的理解,和它都涉及到哪些css属性
网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS盒子模式都具备这些属性. 这些属性我们可以用日常生活中的常见事物——盒子作一 ...
- hbase集群配置
说明 安装 配置 启动 网页效果 一点废话 本文介绍hbase集群配置 说明 hbase想正确配置成功的前提是,你必须知道hadoop集群和zookeeper集群是如何配置的 安装 下载地址 http ...
- apache 不解析 php
apache 不解析php 1.找到: AddType application/x-gzip .gz .tgz在其下面添加: AddType application/x-httpd-php .php ...
- mssql server修改数据库文件位置 此种方法暂未测试成功
--查看当前的存放位置 select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.master ...
- Android 4 学习(12):Linkify & Broadcast event
参考<Professional Android 4 Development> Linkify Linkfy类可以在Text View中创建超链接.匹配LInkify中正则表达式的文本将被L ...