下载
加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 ifndef position

ifndef position.doc

ifndef position

罗密欧煮你爷很快乐
2017-09-27 0人阅读 举报 0 0 暂无简介

简介:本文档为《ifndef positiondoc》,可适用于高等教育领域

ifndefposition#ifndefPOSITION#definePOSITIONclassPosition{protected:introw,columnpublic:Position()Position(introw,intcolumn)intgetRow()constintgetColumn()constintgetnumofqueen()constvoidsetPosition(intnewRow,intnewColumn)}classPosition#endif#ifndefBACKTRACK#defineBACKTRACK#include"Applicationh"#include"Positionh"classBackTrack{public:BackTrack(constApplicationapp)booltryToSolve(Positionpos)protected:Applicationapp}classBackTrack#endif#ifndefAPPLICATION#defineAPPLICATION#include<iostream>#include"Positionh"usingnamespacestdclassApplication{friendostreamoperator<<(ostreamstream,Applicationapp)public:Postcondition:theinitialstateforthisApplicationhasbeengeneratedfrominputorassignmentsandthestartpositionhasbeenreturnedPositiongenerateInitialState()Postcondition:truehasbeenreturnedifposcanbeonthepathtoagoalOtherwise,falsehasbeenreturnedboolvalid(constPositionpos)Precondition:posrepresentsavalidpositionPostcondition:poshasbeenrecordedasavalidpositionvoidrecord(constPositionpos)Postcondition:truehasbeenreturnedifposisthefinalpositionforthisapplicationOtherwise,falsehasbeenreturnedbooldone()constPostcondition:poshasbeenmarkedasnotbeingonthepathtoagoalvoidundo(constPositionpos)classIterator{protected:void*fieldPtrpublic:Postcondition:thisIteratorhasbeeninitializedtoiteratefromposIterator(constPositionpos)Postcondition:thenextpositionforthisIteratorhasbeenreturnedPositionoperator(int)Postcondition:thisIteratorcannotiterateanyfurtherboolatEnd()}classIterator}classApplication#endif主要部分#include"Applicationh"#include"Positionh"#include<iostream>#include<string>#include<Mathh>constshortQUEEN=constshortROWS=constshortCOLUMNS=constshortTRIED=shortChessBoardshortNUMOFQUEEN=intnewRow=PositionstartusingnamespacestdstructitrFields{introw,column,directionx,directiony}itrFieldsPositionApplication::generateInitialState(){for(inti=i<i){for(intj=j<j){ChessBoardij=cout<<ChessBoardij<<""if(j==)cout<<endl}}初始化棋盘并输出整个棋盘conststringSTARTPROMPT="Pleaseenterthestartrowandstartcolumn:"introw,columncout<<"请输入第一个皇后的位置(纵坐标~横坐标~):"cin>>row>>columnstartsetPosition(row,column)cout<<endl<<endlreturnstart获取并返回第一个皇后的位置}methodgenerateInitialStateboolApplication::valid(constPositionpos){boolflag=trueif(posgetRow()>||posgetRow()<||posgetColumn()>||posgetColumn()<)flag=falseif(flag)for(inti=i<i){if(i==posgetColumn())continueif(ChessBoardposgetRow()i==){flag=falsebreak}}检查横行是否有皇后if(flag)for(intj=j<j){if(j==posgetRow())continueif(ChessBoardjposgetColumn()==){flag=falsebreak}}检查竖行是否有皇后if(flag)for(intr=r<r){for(intc=c<c){if(r==posgetRow()c==posgetColumn())continueif(abs(rposgetRow())==abs(cposgetColumn())){if(ChessBoardrc==){flag=falsebreak}}}if(!flag)break}检查左斜右斜有没有皇后if(flag)newRowreturnflag}标记皇后voidApplication::record(constPositionpos){ChessBoardposgetRow()posgetColumn()=QUEENNUMOFQUEEN}判断八个皇后是否已经都摆上boolApplication::done()const{cout<<NUMOFQUEENreturnNUMOFQUEEN==}voidApplication::undo(constPositionpos){if(ChessBoardposgetRow()posgetColumn()==)NUMOFQUEENChessBoardposgetRow()posgetColumn()=}ostreamoperator<<(ostreamstream,Applicationapp){cout<<endlfor(introw=row<ROWSrow){for(intcolumn=column<COLUMNScolumn)cout<<ChessBoardrowcolumn<<""cout<<endl}oorreturnstream}operation<<Application::Iterator::Iterator(constPositionpos){itrFields*itrPtr=newitrFieldsitrPtr>row=posgetRow()itrPtr>column=posgetColumn()itrPtr>directionx=posgetRow()itrPtr>directiony=fieldPtr=itrPtr}constructorPositionApplication::Iterator::operator(int){itrFields*itrPtr=(itrFields*)fieldPtrintnextRow=itrPtr>row,nextColumn=itrPtr>columnswitch(itrPtr>directionx){case:nextRow=breakcase:nextRow=breakcase:nextRow=breakcase:nextRow=breakcase:nextRow=breakcase:nextRow=breakcase:nextRow=breakcase:nextRow=break}switch(itrPtr>directiony){case:nextColumn=breakcase:nextColumn=breakcase:nextColumn=breakcase:nextColumn=breakcase:nextColumn=breakcase:nextColumn=breakcase:nextColumn=breakcase:nextColumn=break}cout<<nextRow<<nextColumn<<""Positionnext(nextRow,nextColumn)returnnext}operatorboolApplication::Iterator::atEnd(){if(((itrFields*)fieldPtr)>directionx>||((itrFields*)fieldPtr)>directiony>)returntruereturnfalse}#include"Positionh"Position::Position(){row=column=}defaultconstructorPosition::Position(introw,intcolumn){this>row=rowthis>column=column}constructorvoidPosition::setPosition(intnewRow,intnewColumn){this>row=newRowthis>column=newColumn}intPosition::getRow()const{returnrow}methodgetRow()intPosition::getColumn()const{returncolumn}methodgetColumn()#include<iostream>#include<string>#include"BackTrackh"#include"Applicationh"#include"Positionh"usingnamespacestdintmain(){ApplicationappBackTrackb(app)cout<<"初始化棋盘:n"Positionstart=appgenerateInitialState()if(!appvalid(start))cout<<"您的输入有误~"<<endlelse{apprecord(start)if(appdone()||btryToSolve(start))cout<<"八个皇后已经正确摆放~"<<endl<<appelse{cout<<app<<endlappundo(start)cout<<"八个皇后没有正确摆放~"<<endl}}cout<<endl<<endlsystem("pause")return}#include"BackTrackh"BackTrack::BackTrack(constApplicationapp){this>app=app}constructorboolBackTrack::tryToSolve(Positionpos){boolsuccess=falseApplication::Iteratoritr(pos)while(!success!itratEnd()){pos=itrif(appvalid(pos)){apprecord(pos)if(appdone())success=trueelse{success=tryToSolve(pos)if(!success)cout<<if(!success){appundo(pos)}}notdone}}whilereturnsuccess}methodtryToSolve

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/12

ifndef position

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利