




已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2 20 2020 数据结构与程序设计 1 数据结构与程序设计 12 王丽苹lipingwang 2 20 2020 数据结构与程序设计 2 Eight QueensPuzzle BookP183 184 2 20 2020 数据结构与程序设计 3 Four Queenssolution 2 20 2020 数据结构与程序设计 4 Four Queenssolution 2 20 2020 数据结构与程序设计 5 ProgramOutline 2 20 2020 数据结构与程序设计 6 Eight QueensPuzzle 目录EightQueens下例程 2 20 2020 数据结构与程序设计 7 Eight QueensPuzzle constintmax board 30 classQueens public Queens intsize boolis solved const voidprint const boolunguarded intcol const voidinsert intcol voidremove intcol intboard size dimensionofboard maximumnumberofqueensprivate intcount currentnumberofqueens firstunoccupiedrowboolqueen square max board max board 2 20 2020 数据结构与程序设计 8 Eight QueensPuzzle boolQueens is solved const return count board size 2 20 2020 数据结构与程序设计 9 Eight QueensPuzzle voidQueens print const inti j for i 0 i board size i for j 0 j board size j cout queen square i j flush cout endl cout endl endl 2 20 2020 数据结构与程序设计 10 Eight QueensPuzzle voidQueens remove intcol queen square count col false 2 20 2020 数据结构与程序设计 11 Eight QueensPuzzle voidQueens insert intcol Pre Thesquareinthefirstunoccupiedrow rowcount andcolumncolisnotguardedbyanyqueen Post Aqueenhasbeeninsertedintothesquareatrowcountandcolumncol counthasbeenincrementedby1 queen square count col true 2 20 2020 数据结构与程序设计 12 Eight QueensPuzzle Queens Queens intsize Post TheQueensobjectissetupasanemptyconfigurationonachessboardwithsizesquaresineachrowandcolumn board size size count 0 for introw 0 row board size row for intcol 0 col board size col queen square row col false 2 20 2020 数据结构与程序设计 13 Eight QueensPuzzlep191 boolQueens unguarded intcol const Post Returnstrueorfalseaccordingasthesquareinthefirstunoccupiedrow rowcount andcolumncolisnotguardedbyanyqueen inti boolok true turnsfalseifwefindaqueenincolumnordiagonalfor i 0 ok 2 20 2020 数据结构与程序设计 14 2 20 2020 数据结构与程序设计 15 Eight QueensPuzzle voidprint information cout ThisistheQueensgame endl 2 20 2020 数据结构与程序设计 16 Eight QueensPuzzlep188 voidsolve from Queens 2 20 2020 数据结构与程序设计 17 Eight QueensPuzzlep186 voidmain Pre Theuserentersavalidboardsize Post Allsolutionstothen queenspuzzlefortheselectedboardsizeareprinted Uses TheclassQueensandtherecursivefunctionsolve from intboard size voidsolve from Queens Findallsolutionsextendingconfiguration 2 20 2020 数据结构与程序设计 18 Eight QueensPuzzle P191Refinement 代码优化P190Figure5 14 d e 目录EightQueens2下例程 2 20 2020 数据结构与程序设计 19 优化思路 1 递归调用花费较多的时间和空间 但是优化的可能性不大 该问题有大量的解 需要多次递归 2 unguarded 函数的处理由三个循环组成 花费了较多时间 可以考虑是否进行优化 基本思路是 优化数据存储结果 从而优化unguarded函数 2 20 2020 数据结构与程序设计 20 2 20 2020 数据结构与程序设计 21 Eight QueensPuzzlep193 constintmax board 30 classQueens public Queens intsize boolis solved const voidprint const boolunguarded intcol const voidinsert intcol voidremove intcol intboard size dimensionofboard maximumnumberofqueensprivate intcount currentnumberofqueens firstunoccupiedrowboolcol free max board boolupward free 2 max board 1 booldownward free 2 max board 1 intqueen in row max board columnnumberofqueenineachrow 2 20 2020 数据结构与程序设计 22 Eight QueensPuzzle Queens Queens intsize Post TheQueensobjectissetupasanemptyconfigurationonachessboardwithsizesquaresineachrowandcolumn board size size count 0 for inti 0 i board size i col free i true for intj 0 j 2 board size 1 j upward free j true for intk 0 k 2 board size 1 k downward free k true 2 20 2020 数据结构与程序设计 23 Eight QueensPuzzle boolQueens unguarded intcol const Post Returnstrueorfalseaccordingasthesquareinthefirstunoccupiedrow rowcount andcolumncolisnotguardedbyanyqueen returncol free col 2 20 2020 数据结构与程序设计 24 Eight QueensPuzzle voidQueens insert intcol Pre Thesquareinthefirstunoccupiedrow rowcount andcolumncolisnotguardedbyanyqueen Post Aqueenhasbeeninsertedintothesquareatrowcountandcolumncol counthasbeenincrementedby1 queen in row count col col free col false upward free count col false downward free count col board size 1 false count 2 20 2020 数据结构与程序设计 25 Eight QueensPuzzle voidQueens remove intcol count col free col true upward free count col true downward free count col board size 1 true 2 20 2020 数据结构与程序设计 26 Eight QueensPuzzle voidQueens print const inti j for i 0 i board size i for j 0 j board size j if j queen in row i cout 1 flush elsecout 0 flush cout endl cout endl endl 2 20 2020 数据结构与程序设计 27 Eight QueensPuzzle boolQueens is solved const return count board size 2 20 2020 数据结构与程序设计 28 Eight QueensPuzzle include time h cout board size doublestart time NULL if board sizemax board cout Thenumbermustbebetween0and max board endl else Queensconfiguration board size Initializeemptyconfiguration solve from configuration Findallsolutionsextendingconfiguration doubleend time NULL cout Ittakestime end start s endl 以上代码只能精确到秒 没有办法打印出秒以后的差异 2 20 2020 数据结构与程序设计 29 include time h cout board size doublestart clock if board sizemax board cout Thenumbermustbebetween0and max board endl else Queensconfiguration board size Initializeemptyconfiguration solve from configuration doubleend clock cout Ittakestime end start CLOCKS PER SEC s endl 以上代码只能精确到毫秒 2 20 2020 数据结构与程序设计 30 Eight QueensPuzzle 比较EightQueens和EightQueens2的运行时间 注意注释print后比较 BOOKP194 195分析回溯对工作量的减少 2 20 2020 数据结构与程序设计 31 E
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年事业单位考试公共基础知识试题库及答案
- 2025湖南湘潭湘乡市教育局公开招聘公益性岗位人员2人模拟试卷及答案详解(名师系列)
- 2025内蒙古土地资源收储投资(集团)有限公司常态化招聘急需紧缺专业人员50人考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025广东省第二中医院招聘皮肤科医师2人模拟试卷及答案详解(全优)
- 2025江苏省规划设计院社招模拟试卷附答案详解
- 2025年职业健康培训题库试题附答案
- 2025福建厦门市教育局所属事业单位厦门市音乐学校招聘专业技术岗位教师1人(2025年4月)模拟试卷及答案详解(典优)
- 2025江苏鑫氟天科技有限公司招聘1人考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025年大学生百科知识竞赛题库及答案
- 2025年宁夏回族自治区农业农村厅自主公开招聘事业单位急需紧缺高层次人才模拟试卷及答案详解参考
- 2025年大连理工大学专职辅导员招聘考试参考题库及答案解析
- 人教版(2024)八年级上册英语Unit 4 Amazing Plants and Animals 教案
- 高盛-大中华区科技行业:台湾ODM评级调整;首次覆盖AI交换机神达锐捷评为买入紫光评为中性;下调和硕至卖出(摘要)
- 全科医师外科规培体系
- 留置导尿考试试题及答案
- 静脉高营养治疗
- 肉毒素知识培训课件
- 最终版附件1:“跨学科主题学习”教学设计(2025年版)
- 艾梅乙防治知识培训课件
- 2025年春新北师大版数学七年级下册课件 第四章 三角形 问题解决策略:特殊化
- 2024年1月版安全环境职业健康法律法规标准文件清单
评论
0/150
提交评论