数字建模 模拟一数独陈丽娟蒋鑫陈德进.docx
《数字建模 模拟一数独陈丽娟蒋鑫陈德进.docx》由会员分享,可在线阅读,更多相关《数字建模 模拟一数独陈丽娟蒋鑫陈德进.docx(16页珍藏版)》请在冰豆网上搜索。
数字建模模拟一数独陈丽娟蒋鑫陈德进
CreatingSudokuPuzzles
Summary
Sudokupuzzleisanattractivegamewhichisverypopularintheworldnow.Atcurrent,creatingandevaluatingthedifficultylevelofSudokupuzzlesisasignificantproblem.InourpaperwedesignanalgorithmthatbasedonimprovementbackdatingmethodandcanproducedifferentdifficultySudokuwithauniquesolution,thenevaluatethecomplexityofit.
First,wedefinethebasicoperationoffindingthesmallestcandidatenumberandjudgingwhetherthelayoutisfinallayoutordeathlayout.Onthisbasis,usingthesearchalgorithmminimumcandidatenumberandbacktrackingexhaustivelystatespacetominimizealgorithmcomplexity.
Then,constructadecisiontreeandusebackdatingmethodtodefinedifficultyevaluationfromthreeaspects.Thethreeaspectsarethenumberofknowngridsinoriginallayout,thesmallestknownnumberofeachrowandeachline,thesearchnumberinsolvingthefinallayout.Throughalotofexperimentsweendowthemwithcorrespondingweights,andusetheweightstocalculatethedifficultygradeofeachoriginallayout.
Finally,designanalgorithmtoproduceanoriginallayoutwhichsatisfiestherulesofSudoku.Solveitandgetallthefinallayoutsofit,thenusebacktrackingtoseekanoriginallayoutwithuniquesolutionmeetingthechosendifficultylevel.
keywords:
thesearchalgorithmminimumcandidate;decisiontree;weights;backtracking
Content
1.Introduction
SudokuwasdevisedbymathematicianOlah,andnowitispopularathomeandabroad.Itisanattractivegamethatlotsofpeopleliketostudyitthoughbuyingbooks.Thereare93×3unitgridsintheoriginalSudoku,eachofwhichisaunit3×3grid.Eachgridcanhaveanumberof1to9togiveitasimpleconstraint,ormaybeempty.Therulesofthegameareasfollows[3]:
1.Eachrowofcellscontainstheintegers1throughto9exactlyonce;
2.Eachcolumnofcellscontainstheintegers1throughto9exactlyonce;
3.Each3×3squarecontainstheintegers1throughto9exactlyonce.
Inordertounderstandclearlywegiveasimpleexampleaspicture1shows:
5
2
8
1
6
7
3
9
4
6
4
9
3
2
8
7
1
5
1
7
3
4
9
5
2
6
8
3
9
4
5
8
6
1
7
2
8
1
7
2
3
4
9
5
6
2
5
6
7
1
9
4
8
3
4
3
5
8
7
1
6
2
9
7
6
2
9
5
3
8
4
1
9
8
1
6
4
2
5
3
7
2
8
1
6
7
3
9
4
6
4
3
2
8
7
1
5
1
7
3
4
9
2
6
8
3
4
5
8
6
1
7
2
8
1
7
3
4
9
5
6
2
5
6
7
1
9
4
3
4
3
5
8
1
6
2
9
7
6
2
9
5
3
4
1
9
8
1
6
4
2
5
3
Theoriginallayoutthefinallayout
Figure1ThesimpleexampleofSudoku
2.DescriptionandAnalysis
WeshoulddevisealgorithmandmetricsofconstructingSudokupuzzleswhichcanbeextensibletoavaryingnumberofdifficultylevels,what’smoreensuretheuniquesolutionforeachSudoku.Themethodsofsolvingthegridsaregenerallyclassifiedintothevisual-fitmethodandcandidatenumbermethod.
Thevisual-fitmethodiscombiningthedigitalavailablewiththerows,columnsandpalacetheyarrayedin,andthenfindtheblanksthatcandecideauniquenumber.Thecandidatenumbermethodiscalculatingthenumberofcandidatesinsomeblank.Becausethevisual-fitmethodcanonlyagainstpartofthesimpleSudokusolution,inthepaperweusecandidatenumbermethod.
Ourprimaryaimismakingthesmallestalgorithmcomplexitywhiletherequirementsabovearesatisfied.Inordertoguaranteethealgorithmcomplexityminimum,weusethesmallestcandidatenumbermethod.What’smore,todescribelevelsofSudokupuzzles,wehavedifficultycriteriondefinition.
Wedescribethesolvingprocessasadecisiontreeasfigure2shows.Thenumbers(m,n)inthetreenodes,amongwhichmmeanstheblanksofcorrespondinglayoutandnmeansthenumberofthesmallestcandidates.Ifm>0andn=0,thatmeansthecorrespondinglayoutisadeathlayout;ifm=0andn=0,thatmeansthecorrespondinglayoutisafinallayout.ThisdecisiontreeindicatesthattheoriginallayoutS0hasS1andS2twosolutions,amongwhichfromS1.1toS1.5representthemiddlelayoutsofS1andfromS2.1toS2.5representthemiddlelayoutofS2,WhileS1.1isthesameasS2.1andS1.2isthesameasS2.2.
Figure2DecisiontreeofsolvingSudokuwith6spaces
3.Assumptions
1.Inthispaper,alllayoutsisa9×9grid,don'tinvolveothersizes.
2.Allstatisticsaboutrunningtimehavenothingtodowithcomputer,thatistosay,programs’runningtimeshouldbesameindifferentcomputers.
3.Therunningtimeisembodiedbythenumberofmiddlelayoutsbetweeneachoriginallayoutanditsallfinallayouts.
4.SymbolsandTerms
4.1Symbols
S:
thelayoutofSudoku;
i:
therownumberofunitgrid;
j:
thelinenumberofunitgrid;
m:
thenumberofblanksincorrespondinglayout;
n:
thenumberofthesmallestcandidates;
k:
thenumberoflayout.
4.2Terms[1]
OriginalLayout:
theunitgridstateatthebeginningofthegame,includeseveraldigitalandseveralspaces;
FinalLayout:
theunitgridstateattheendofthegame,includeseveraldigitalandnospacesexist;
MiddleLayout:
theunitgridstateintheprocessoffindingfinallayoutfromtheoriginallayout;
DeathLayout:
therearespacesinthelayout,butnomatterwhatdigitalfrom1to9fallinginthespacesallwillconflictwiththerulesofthegame;
Unitgrid:
thegridthatmakeupbya
gridwhichcontainthenumberfrom1to9onlyonce;
Candidates:
lotsofnumberthatcanbefilledintheunitgridthatmeansthenumbermustbedifferentfromthedigitalsinthesamerow,samelineandthesameunitgrid;
Solution:
anoriginallayoutwhichcanfinditsfinallayout
5.ModelDesignandSolve
5.1BasicOperation[1]
Obviously,thelayoutofSudokushouldbeexpressedbymatrixstructure.Todescribesubsequentalgorithmnowwedevisebasicoperationasfollows:
FunctionCandidates=Check(S,i,j).
FunctionCheckusetocalculatethecandidatenumbervectoroftheunitrowilinejinSlayout.Candidates:
Function[i,j]=FindCandidate(S).
FunctionFindCandidatesearchtheunit[i,j]whichhasthesmallestcandidatenumber.SupposetherearekspacesinlayoutSthatintheperformingoffunctionFindCandidate,thereneedtoinvokektimesfunctionCheck:
Functionflag=Success(S).
FunctionSuccessisusedtoestimatewhetherSlayoutisfinallayout:
Functionflag=Failed(S).
FunctionFailedisusedtoestimatewhethertheSlayoutisdeathlayout.
5.2Thesearchalgorithmbasedontheminimumnumberofcandidates
TheCandidatesEliminationTechniquesiswritingallthedigitalsthatmayappearintheblanksfirst,andthenthroughsomecommonlyusedalgorithmtocutcandidatenumberuntilgainthecertainuniquecandidatenumber.Itiswidelyusedincomputergeneratingpuzzleandproblemsolvinginpractice.Thisnotonlybecauseitrelativelyeasyprogramming,anditsalgorithmarealsogrowingwhichmakeitssolvingefficiencyandabilityraise.
InordertounderstandtheCandidatesEliminationmoreclearly,wegiveanexampleoffindingcandidatesinSudokuasfollow:
Figure3theexampleoffindingcandidates
Thebasicmethodtosolveoriginallayoutisusingbackdatingmethodtoillustratestatespaceexhaustively.Butwhichspaceshouldbefilledinwhichdigitalineachstepwillinfluencethestatespacegreatly,andtheninfluencetheefficiencyofthealgorithm.Generally,wethinkthestatespacetosolveoriginallayoutisa9forktreewhosedepthislessthan81[1].
Usingmoreforktreetraversalalgorithmframeandcombinethecalculationofthesmallestcandidatesnumber,wedevisealgorithmasfollows[1].
WriteoriginallayoutasS0,writethefirstKalayoutofthesolvingprocessasSk,andstorethedatastructureofmiddlelayoutasstackSS.TherecursivealgorithmofsolvingallthefinallayoutofSkdescribesasfollows:
1.If
thatistosaySkisthefinallayout,thenfromS1toSkwhichkeptinstackSSaresolvingprocessesfromoriginallayouttoanfinallayoutandreturntotheupperfunctioncalls;
2.If
thatistosaySkisastalemate,soreturntotheupperfunctioncalls;
3.Findthesmallestcandidatesunitgrid’spositioninSkthatis
;
4.Calculatethecandidatesvectorofposition[i,j]thatisCandidates=Check(Sk,i,j);
Fillingeachcandidateintheemptygrids[i,j]ofSkinturntogainanewlayout
Sk+1,thenputSk+1instackSSandinvokeSolve(Sk+1)recursively,afterthatexecuteastackoperation.
Theflowchartoffunctionsolveisasfollow:
Figure4theprogramflowchartoffunctionSolve
5.3TheMeasurementforDifficultyGrades[2]
WeevaluatethedifficultyofaSudokufromthelogicinferenceofhumanandtheexhaustivesearchofcomputer.Duetotheplayer’sreasoningtotheunknowngridsbasedontheinformationprovidedbytheknowngrids,whiletheknowngrids’quantityanddistributiondecidedhowmuchinformationtheycanprovided.Thelessknowngrids,anditsdistributionismoreuneven,moreasymmetrythatthelogicinferenceoftheSudokushouldbemoredifficult.
ThustoaSudokutopic,wegivethreeevaluationprojects(aschart1),evaluateitbymarkingitfrom0to4,andtogivethethreeprojectscorrespondingweights,thengetthesummationscoringofthetopic.Usetheintegerpartofthescoretoweighthegamers’difficultylevelneedasfollows:
Grade1:
thescorefrom0to1;
Grade2:
thescorefrom1to2;
Grade3:
thescorefrom2to3;
Grade4:
thescorefrom3to4.
Chart1evaluationofSudoku
score
Projects
of
Evaluation
1
2
3
4
Thenumberofknowngridsinoriginallayout:
k
(weights:
0.4)
From38to45
From31to37