Question: Implement the function solve ( ) that attempts to solve the puzzle using the strategy described below. solve ( ) Compute the set of valid

Implement the function solve() that attempts to solve the puzzle using the strategy described below.
solve()
ComputethesetofvalidvaluesV[(r,c)]foreverycell(r,c).
known=set()
Foreverycellwhosesetofvalidvalueshaslength1,add(val,r,c)totheknownset,wherevalisthe(only)elementofsetV[(r,c)].
whileknownsetisnotempty:
(val,r,c)=known.pop()#Removeanelementfromknownset
S[r,c]=val#Setvalueinpuzzle
V[(r,c)]=set()#Makethecandidatesetforcell(r,c)empty,sinceithasalreadybeenfilled.
updatesets#Removevalfromallsetsthatareinthesamerow,columnorregionascell(r,c).Addtoknownsetallsetswhoselengthisreducedto1bythisupdate.
Ifthepuzzlehasbeensolved(thatis,allcellsinSarefilled),return1
Ifthereisacell(r,c)thathasnotbeenfilledbutwhosesetofvalidvaluesisempty,return-1
Return0-thepuzzlehasnotbeensolved,butitstillmayhaveasolution

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!