乱七八糟代码合集٩(๑◡๑)۶
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
乱七八糟代码合集٩(๑◡๑)۶相关推荐
- 一、PyTorch Cookbook(常用代码合集)
PyTorch Cookbook(常用代码合集) 原文链接:https://mp.weixin.qq.com/s/7at6y2NcYaxGGN8syxlccA 谢谢作者的付出.
- GitHub上7000+ Star的Python常用代码合集
作者 | 二胖并不胖 来源 | 大数据前沿(ID:bigdataqianyan) 今天二胖给大家介绍一个由一个国外小哥用好几年时间维护的Python代码合集.简单来说就是,这个程序员小哥在几年前开始保 ...
- apicloud ajax html,基于apicloudAJAX请求代码合集
get请求代码: api.ajax({ url:'http://m.weather.com.cn/data/101010100.html' //天气预报网站的WebService接口 },functi ...
- 收藏 | 因果推断书籍代码合集
来源:计量经济学服务中心本文约2700字,建议阅读8分钟 本文为你介绍了因果推断书籍的代码合集. 1.Causal Inference: The Mixtape 来源: https://mixtape ...
- 2013计算机视觉代码合集
注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-9-7 一.特征提取Feature Extraction: ...
- 【CV】YOLOv4最全复现代码合集(含PyTorch/TF/Keras和Caffe等)
前言 2020年4月24日,CVer第一时间推文:大神接棒,YOLOv4来了! 2020年6月28日,CVer第一时间推文:YOLOv4-Tiny来了!371 FPS! 距离YOLOv4正式推出,已经 ...
- YOLOv3最全复现代码合集(含PyTorch/TensorFlow和Keras等)
点击上方"CVer",选择"置顶公众号" 重磅干货,第一时间送达 前戏 2018年3月26日,CVer第一时间推文:YOLOv3:你一定不能错过 2019年3月 ...
- 预编码 matlab,无线通信-预编码-MATLAB代码合集
无线通信-预编码-MATLAB代码合集 c 2021-2-18 下载地址 https://www.codedown123.com/72422.html 多用户MIMO预编码之类的代码,帮助初学者进行编 ...
- js时间戳(代码合集)获取(年月日,秒戳,毫秒戳,) - 综合篇
js时间戳(代码合集)获取(年月日,秒戳,毫秒戳,) - 综合篇 官方解说:JavaScript Date 对象 W3CSchool教程: JavaScript Date 对象 一.js获取北京时间 ...
最新文章
- 函数小知识点(文档字符串,闭包等)
- 宏观经济学思维导图_巧用思维导图,提升初三化学专题复习课实效
- 使用hexo搭建个人博客
- RTSP播放器网页web无插件直播流媒体音视频播放器EasyPlayer-RTSP实现支持H265编码格式过程介绍
- Linux Crontab 安装使用详细说明
- esxi root 密码规则_陌陌风控系统静态规则引擎aswan
- RAC 安装完成后 节点间通信不依赖于SSH
- 查询商品信息报错FreeMark template error
- html+分割字符,sql拆分字符串split
- sip网络电话 用户一直注册超时无法注册成功
- 常用希腊字母与英文字母对照表;希腊字母中文发音
- Word——如何给公式自动编号插入题注交叉引用时不出现公式本身||公式行距变大怎么办
- 简单实现antd的表单设计
- 如何创建一个最简单的Windows桌面应用程序 (C++)
- 高考志愿填报很纠结?这款志愿填报软件来解救大家!
- 手机word文档怎么恢复
- java tostring格式化日期_Java格式化日期的三种方式
- android 调用系统图库剪切图片出现黑屏闪退
- 美图公司2018年营收27.9亿元 同比下滑37.8%
- 360安全卫士、360杀毒与火绒