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 ...
随机推荐
- xe 最大连接数限制、记录客户连接、心跳
xe 最大连接数限制.记录客户连接.心跳 //author: cxg unit DSServerContainer; interface uses SysUtils, Classes, IniF ...
- 蓝桥杯 算法训练 ALGO-57 删除多余括号
算法训练 删除多余括号 时间限制:1.0s 内存限制:512.0MB 问题描述 从键盘输入一个含有括号的四则运算表达式,要求去掉可能含有的多余的括号,结果要保持原表达式中变量和运算符的相对位置 ...
- nodejs 中的 NODE_PATH
在使用 nodejs 开发中我们都免不了要去安装一些第三方模块. 那么你或多或少的遇到过以下一些问题 再继续阅读之前,我们先来弄清楚一个概念. npm install --global xxx 属于全 ...
- Kaggle 比赛项目总结(项目流程)
一.EDA(Exploratory Data Analysis) EDA:也就是探索性的分析数据 目的: 理解每个特征的意义: 知道哪些特征是有用的,这些特征哪些是直接可以用的,哪些需要经过变换才能用 ...
- java ------------用Array.sort()输出数组元素的最大值,最小值,平均值~~~~
总结:输出最大值,最小值,可以先排序,再输出就容易点了.不用循环判断.我还没学.但是觉得很好用 package com.aini; import java.util.Arrays; import ja ...
- Java-Maven-Runoob:Maven IntelliJ
ylbtech-Java-Maven-Runoob:Maven IntelliJ 1.返回顶部 1. Maven IntelliJ IntelliJ IDEA 已经内建了对 Maven 的支持.我们在 ...
- PyCharm中按住Alt键,可以选择一个指定列表,然后对这个数列进行操作,比如删除,增加等等
- ARM-Linux内核移植之(一)——内核启动流程分析
内核版本:2.6.22 为什么要采用这样一个较低的版本进行移植了,因为韦东山大牛说了,低版本的才能学到东西,越是高版本需要移植时做的工作量越少,学的东西越少. 内核启动分为三个阶段,第一是运行hea ...
- C Primer Plus学习笔记(十三)- 结构和其他数据形式
建立结构声明 结构声明(structure declaration)描述了一个结构的组织布局 struct book { char title[MAXTITL]; char author[MAXAUT ...
- $http questions
//////1 $http.get("/api/task/165/").then( function(a){ console.log(a); }, function(b){ con ...