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 ...
随机推荐
- Spring、Spring MVC、MyBatis整合文件配置详解2
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- 题目1539:师弟 ——最短路+DFS
题意::从起点到终点的所有的最短路中,找出离终点有X个路口的城市一共有几个 开始我用最短路+DFS从起点开始搜,超时了 换了一种方法,从终点开始搜,AC #include<stdio.h> ...
- HTMLTestRunner 汉化版---来源一个大神的源码(加了失败截图,用例失败重新执行 功能)
HTMLTestRunner 汉化版 20170925 测试报告完全汉化,包括错误日志的中文处理 针对selenium UI测试增加失败自动截图功能 增加失败自动重试功能 增加饼图统计 同时兼容pyt ...
- WebSocket :Nginx+WebSocket内部路由策略推送服务器的实现(附可生产环境应用代码)
1.项目背景 前几天写了一篇WebSocket推送的博客:WebSocket :用WebSocket实现推送你必须考虑的几个问题 支持的连接数大概几千个,具体数量依赖于tomcat能并发的线程数,但很 ...
- AngularJS:template2
ylbtech-AngularJS: 1.返回顶部 1. 2. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. 2. 6.返回顶部 作者:ylbtech出处:h ...
- 严谨的程序案例Api
文档 功能 同步推荐关系 接口方法 syncRelation 参数描述 OriginalUsername 查询的用户用户名 RecommandUsername 推荐人用户名 返回值 status 1成 ...
- 【转载】解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法
1.开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: exec sp_configure 'show advanced options',1 recon ...
- PL/SQL 训练04--事务
--pl/sql通过SQL和ORACLE数据库紧密的整合在一起--在pl/sql中可以执行任何操作语句(DML语句),包括INSERT,UPDATE,DELETE,MERGE,也包括查询语句--可否执 ...
- java NIO(转载)
(原文地址:https://zhuanlan.zhihu.com/p/23488863) NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型 ...
- spring+hibernate ---含AOP--事务--laobai
biz包: package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support. ...