N皇后问题代码
/*.h*/
#ifndef _NQUEEN_H
#define _NQUEEN_H
#include<iostream>
#include<vector>
#include<string>
using namespace std; class Queen{
public:
Queen();
Queen(int num);
//~Queen();
void show(const vector<vector<string>>&input);
void solve();
private:
bool is_valid(const int row, const int col);
//void init(const int num);
void TraceBack(const int curRow);
private:
int m_num;
vector<int>pos;
vector<vector<string>>res;
}; #endif
/*cpp*/
#include"NQueen.h" Queen::Queen(int num) :m_num(num), pos(, -){ }
void Queen::TraceBack(int curRow){
//cout << m_num << endl;
if (curRow == m_num)
{
vector<string>out(m_num, string(m_num, '*'));
for (int i = ; i < m_num; i++){
out[i][pos[i]] = 'Q';
}
res.push_back(out);
}
else{
for (int col = ; col < m_num; col++){
if (is_valid(curRow, col)){
pos[curRow] = col;
TraceBack(curRow + );
pos[curRow] = -;
}
}
}
}
bool Queen::is_valid(int row, int col){
for (int i = ; i < row; i++){
if (pos[i] == col || abs(i - row) == abs(pos[i] - col))
return false;
}
return true;
}
void Queen::solve(){
TraceBack();
//cout << res.size() << endl;
show(res);
}
void Queen::show(const vector<vector<string>>&input){
//cout << input.size() << endl;
//cout << input[0].size() << endl;
for (int i = ; i < input.size(); i++){
for (int j = ; j < input[].size(); j++){
cout << input[i][j] << endl;
//if ((j + 1) % input.size() == 0)cout << input[i][j]<<e
}
cout << endl;
}
}
N皇后问题代码的更多相关文章
- 对八皇后的补充以及自己解决2n皇后问题代码
有了上次的八皇后的基础.这次准备解决2n皇后的问题,: //问题描述// 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行./ ...
- 八皇后--python代码
迭代和递归方法的运用 import random def prettyprint(solution): #图形化处理数据 def line(pos,length=len(solution)): #单行 ...
- C#中八皇后问题的递归解法——N皇后
百度测试部2015年10月份的面试题之——八皇后. 八皇后问题的介绍在此.以下是用递归思想实现八皇后-N皇后. 代码如下: using System;using System.Collections. ...
- [CODEVS1295]N皇后(位运算+搜索)
题目描述 Description 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个皇后,任 ...
- 算法回顾--N皇后问题简单回顾
前言 最近学习的过程中,不知道哪门子的思维发散,突然又遇见皇后问题了,于是乎老调重弹,心里琢磨,虽然思路大家都容易懂,哪怕是最简单的野蛮回溯法,说着简单,但是如果非得编码实现?我可以一次性写出来OK的 ...
- [题解]N 皇后问题总结
N 皇后问题(queen.cpp) [题目描述] 在 N*N 的棋盘上放置 N 个皇后(n<=10)而彼此不受攻击(即在棋盘的任一行,任一列和任一对角线上不能放置 2 个皇后) ,编程求解所有的 ...
- Python 八皇后问题
八皇后问题描述:在一个8✖️8的棋盘上,任意摆放8个棋子,要求任意两个棋子不能在同一行,同一列,同一斜线上,问有多少种解法. 规则分析: 任意两个棋子不能在同一行比较好办,设置一个队列,队列里的每个元 ...
- 【搜索】还是N皇后
先看题才是最重要的: 这道题有点难理解,毕竟Code speaks louder than words,所以先亮代码后说话: #include<iostream> using namesp ...
- 回溯法——求解N皇后问题
问题描写叙述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后.使其不能互相攻击,即随意的两个皇后不能处在允许行.同一列,或允许斜线上. 能够把八皇后问题拓展 ...
随机推荐
- A+B for Polynomials
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- python is 和 == 区别(8)
在python中is和==都说常用的运算符之一,主要用于检测两个变量是否相等,返回True或者False,具体区别在哪呢? 一.前言 在讲解is和==区别直接先讲解一下内置函数id(),其实在文章 p ...
- tcpdump网络调试
简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...
- (idea maven)mybatis-generator步骤
1.新建一个maven项目,选择maven-archetype-webapp 点击next 2.项目名称,点击next 3.选择项目存放路径,然后点击finish 4.在main包下 添加包java和 ...
- day32——进程、操作系统
day32 进程的基础 程序 一堆静态的代码文件 进程 一个正在运行的程序进程.抽象的概念 被谁运行? 由操作系统操控调用交于CPU运行 操作系统 管理控制协调计算机中硬件与软件的关系 操作系统的 ...
- linux centos7开机自动启动程序实现
1存放脚本位置 /etc/init.d/ServerManagerCLI.sh 该脚本是自己新建的内容参看2 增加执行权限 chmod +x /etc/rc.d/init.d/ServerManage ...
- machine learning相关会议
1. ICML(International Conference on Machine Learning) 链接:https://en.wikipedia.org/wiki/Internation ...
- Wireless support
Wireless support 参考: https://www.rhyous.com/2010/12/03/freebsd-wireless-configuring-a-wireless-inter ...
- SAP云平台上的SSO Principal Propagation设置
我今天试图使用SAP云平台的SAP WebIDE Fullstack时,发现打不开, 遇到如下错误信息: You are not authorized to work with SAP Web IDE ...
- python学习之操作redis
一.Redis安装网上很多可以查找 二.redis-py的安装,使用命令pip install redis. 安装过程中如果产生连接超时的错误,可以使用国内镜像参考如下 豆瓣:pip install ...