1.全排列

#include<bits/stdc++.h>
using namespace std;
int n = 3;
bool hashtable[100] = {false};
int P[100] = {-1};
int count_num = 0;
void f(int index){if(index == n + 1){for (int i = 1; i <= n; i++){printf("%d ", P[i]);}count_num++;printf("\n");return;}for (int x = 1; x <= n; x++){if (hashtable[x] == false){P[index] = x;hashtable[x] = true;f(index + 1);hashtable[x] = false;}}
}int main(){f(1);printf("\ncount=%d", count_num);return 0;
}

全排列

2.八皇后-暴力

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n = 4;
 4 int P[9] = {0};
 5 int cntnum = 0;
 6 bool tablehash[9] = {false};
 7
 8 void f(int index){
 9     if (index == n + 1){
10         bool flag = true;
11         for (int i = 1; i <= n; i++){
12             for (int j = i + 1; j <= n; j++){
13                 if (abs(i - j) == abs(P[i] - P[j])){
14                     flag = false;
15                 }
16             }
17         }
18         if (flag){
19             for (int k= 1; k <= n; k++){
20                 printf("%d ", P[k]);
21             }
22             printf("\n");
23             cntnum++;
24         }
25         return;
26     }
27     for (int i = 1; i <= n; i++){
28         if (tablehash[i] == false){
29             P[index] = i;
30             tablehash[i] = true;
31             f(index + 1);
32             tablehash[i] = false;
33         }
34     }
35 }
36 int main(){
37     f(1);
38     printf("%d", cntnum);
39     return 0;
40 }

八皇后-暴力

3.八皇后-回溯

#include<bits/stdc++.h>
using namespace std;
int n = 9;
int P[30] = {0};
int cntnum = 0;
bool tablehash[30] = {false};void f(int index){if (index == n + 1){cntnum++;for (int k = 1; k <= n; k++){printf("%d ", P[k]);}printf("\n");return;}for (int i = 1; i <= n; i++){bool flag = true;if (tablehash[i] == false){//假如第i行没有皇后, 即放在第 i 行第 index 列for (int j = 1; j < index; j++){//遍历和以前的皇后是否合法if (abs(index - j) == abs(P[j] - i)){flag = false;break;}}if (flag){//目前可以在第i行第index列放入皇后P[index] = i;tablehash[i] = true;f(index + 1);tablehash[i] = false;}}}}int main(){f(1);//printf("%d", cntnum);printf("%d", cntnum);return 0;
}

八皇后-回溯

(未完待续~)

转载于:https://www.cnblogs.com/yellowzunzhi/p/11123045.html

乱七八糟代码合集٩(๑◡๑)۶相关推荐

  1. 一、PyTorch Cookbook(常用代码合集)

    PyTorch Cookbook(常用代码合集) 原文链接:https://mp.weixin.qq.com/s/7at6y2NcYaxGGN8syxlccA 谢谢作者的付出.

  2. GitHub上7000+ Star的Python常用代码合集

    作者 | 二胖并不胖 来源 | 大数据前沿(ID:bigdataqianyan) 今天二胖给大家介绍一个由一个国外小哥用好几年时间维护的Python代码合集.简单来说就是,这个程序员小哥在几年前开始保 ...

  3. apicloud ajax html,基于apicloudAJAX请求代码合集

    get请求代码: api.ajax({ url:'http://m.weather.com.cn/data/101010100.html' //天气预报网站的WebService接口 },functi ...

  4. 收藏 | 因果推断书籍代码合集

    来源:计量经济学服务中心本文约2700字,建议阅读8分钟 本文为你介绍了因果推断书籍的代码合集. 1.Causal Inference: The Mixtape 来源: https://mixtape ...

  5. 2013计算机视觉代码合集

    注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-9-7 一.特征提取Feature Extraction: ...

  6. 【CV】YOLOv4最全复现代码合集(含PyTorch/TF/Keras和Caffe等)

    前言 2020年4月24日,CVer第一时间推文:大神接棒,YOLOv4来了! 2020年6月28日,CVer第一时间推文:YOLOv4-Tiny来了!371 FPS! 距离YOLOv4正式推出,已经 ...

  7. YOLOv3最全复现代码合集(含PyTorch/TensorFlow和Keras等)

    点击上方"CVer",选择"置顶公众号" 重磅干货,第一时间送达 前戏 2018年3月26日,CVer第一时间推文:YOLOv3:你一定不能错过 2019年3月 ...

  8. 预编码 matlab,无线通信-预编码-MATLAB代码合集

    无线通信-预编码-MATLAB代码合集 c 2021-2-18 下载地址 https://www.codedown123.com/72422.html 多用户MIMO预编码之类的代码,帮助初学者进行编 ...

  9. js时间戳(代码合集)获取(年月日,秒戳,毫秒戳,) - 综合篇

    js时间戳(代码合集)获取(年月日,秒戳,毫秒戳,) - 综合篇 官方解说:JavaScript Date 对象 W3CSchool教程: JavaScript Date 对象 一.js获取北京时间 ...

最新文章

  1. 为什么整数集合使用Z来表示?
  2. IntelliJ IDEA 2019从入门到癫狂 图文教程
  3. linux四剑客-grep/find/sed/awk/详解-技术流ken
  4. 【TensorFlow】TensorFlow从浅入深系列之八 -- 教你学会变量管理
  5. JavaScript获取当前月的第一天和最后一天日期
  6. shell学习笔记(五)
  7. 普惠科技助力智能升级 天猫精灵新品直指家庭IoT生态
  8. ThreadPoolExecutor运转机制详解
  9. 如何建立地球上任何一个区域的地形3d模型,并添加卫星或地貌贴图
  10. unity 控制对象移动、旋转
  11. dm服务器未能启动,DM 达梦数据库 创建服务 无法创建目录_REPLACE_SELF_DM_HOME 错误解决方法...
  12. 解决Linux无法连接外网的问题
  13. android开发关机代码,android代码实现关机
  14. 纯CSS3制作逼真的iphone 6手机模型
  15. 十一.再函数进阶+requests网络爬虫
  16. 论文笔记:Planning and Decision-Making for Autonomous Vehicles
  17. 5G新基建虚拟仿真技术助力大中专专业VR实训室建设
  18. SaaS公司融资的「22条军规 」
  19. Visual Studio运行项目报错:The Debugger Resource DLL is out of date
  20. 什么是xhr?XMLHttpRequest的基本使用及xhr Level2的新特性详解及案例

热门文章

  1. c++由动态库dll文件生成lib文件的方法
  2. PAT Basic(乙级)---1009 (20 分)说反话
  3. BA_重投影误差e对于相机的位姿ξ和对空间点的坐标P的雅可比矩阵的推导
  4. SpringBoot整合redis使用setnx完成分布式锁
  5. 软件测试工程师书籍介绍(精华)
  6. 惠普刀片服务器c7000硬件配置手册_HP C7000刀片机配置
  7. SAP顾问生涯闲记:2016年越南鞋厂项目回忆
  8. opencv-python识别魔方特定颜色方块,并输出各方块中心坐标
  9. OKR 结果思维:为什么要以结果为导向?(第一部分)
  10. SQL语句的基础教程(二)