def SudoSolveIt1(A, C, n): Arec = deepcopy(A) Crec = deepcopy(C) if n == 1: Crec = SV.ConstructC(Arec) return SudoSolveIt1(Arec, Crec, 2) else: if (n == 2): Arec, Crec, DidIn = SV.SudoInput1(Arec, Crec) return SudoSolveIt1(Arec, Crec, 3) if np.min(Arec) > 0: return Arec if (SC.IsDeadEnd(Crec)): return np.array([0, 0]) Arec, Crec, DidIn = SV.SudoInput1(Arec, Crec) if (DidIn): return SudoSolveIt1(Arec, Crec, 3) else: r = SV.FindMinRow(Crec) rC = Crec[r, :] for i in range(9): if (rC[i] > 0): A2 = deepcopy(Arec) C2 = deepcopy(Crec) A2[rC[9], rC[10]] = rC[i] C2 = SV.clearC(C2, rC[9], rC[10], rC[11], rC[i]) C2 = np.delete(C2, r, 0) A2 = SudoSolveIt1(A2, C2, 3) if np.min(A2) > 0: return A2 return A2
def SudoBruteSolve(A, n): #Brute Force solver if (SC.IsSudoRight(A)): return A Arec = deepcopy(A) for i in range(9): for j in range(9): if (Arec[i, j] == 0): posnum = SV.FindPosNums(Arec, i, j) if (posnum.size > 0): for k in range(posnum.size): Arec[i, j] = posnum[k] A2 = SudoBruteSolve(Arec, n) if (SC.IsSudoRight(A2)): return A2 return A2 else: return Arec
def CountSolutions(A, C, numSol, n): Arec = deepcopy(A) Crec = deepcopy(C) if n == 1: Crec = SV.ConstructC(Arec) return CountSolutions(Arec, Crec, numSol, n + 1) else: if (n == 2): Arec, Crec, DidIn = SV.SudoInput1(Arec, Crec) Arec, Crec, DidIn = SV.SudoInput2(Arec, Crec) return CountSolutions(Arec, Crec, numSol, n + 1) if np.min(Arec) > 0: numSol = numSol + 1 return numSol if (SC.IsDeadEnd(Crec)): return numSol Arec, Crec, DidIn = SV.SudoInput1(Arec, Crec) if (DidIn): if (SC.IsDeadEnd(Crec)): return numSol else: Arec, Crec, DidIn = SV.SudoInput2(Arec, Crec) return CountSolutions(Arec, Crec, numSol, n) else: Arec, Crec, DidIn = SV.SudoInput2(Arec, Crec) if (DidIn): return CountSolutions(Arec, Crec, numSol, n) r = SV.FindMinRow(Crec) rC = Crec[r, :] for i in range(9): if (rC[i] > 0): A2 = deepcopy(Arec) C2 = deepcopy(Crec) A2[rC[9], rC[10]] = rC[i] C2 = SV.clearC(C2, rC[9], rC[10], rC[11], rC[i]) C2 = np.delete(C2, r, 0) numSol = CountSolutions(A2, C2, numSol, n + 1) if numSol == 2: return numSol return numSol
def onPressSolve(A): if (np.min(A) >= 0 and np.max(A) <= 9): if (SC.IsSudoRight(A)): print("Solver", "Already Solved!") else: Adef = A.copy() if (GU.CountSolutions(Adef, [], 0, 1) == 1): sol_A = SL.SudoSolveIt2(A, [], 1) print(A) else: print("Solver", "There isn't unique solution!") else: print("Solver", "Wrong inputs!")
def onPressRate(A): if (np.min(A) >= 0 and np.max(A) <= 9): if (SC.IsSudoRight(A)): print("Rating", "Already Solved!") else: Adef = A.copy() if (GU.CountSolutions(Adef, [], 0, 1) == 1): difficulty = RT.RateProb(A) # print("Rating", "Your Problem is " + BS.getStrDiff(difficulty)) return difficulty else: print("Rating", "There isn't unique solution!") print(A) exit(0) else: print("Rating", "Wrong inputs!")
def onPressSolve(): A=np.zeros((9,9), dtype=int) i=0 for row in rows: j=0 for col in row: A[i,j]=int(col.get()) j=j+1 i=i+1 if (np.min(A)>=0 and np.max(A)<=9): if (SC.IsSudoRight(A)): printMes("Solver","Already Solved!") else: Adef=deepcopy(A) if (GU.CountSolutions(Adef,[],0,1)==1): A=SL.SudoSolveIt2(A,[],1) print(A) else: printMes("Solver","There isn't unique solution!") else: printMes("Solver","Wrong inputs!") mw.destroy()
def onPressRate(): A=np.zeros((9,9), dtype=int) i=0 for row in rows: j=0 for col in row: A[i,j]=int(col.get()) j=j+1 i=i+1 if (np.min(A)>=0 and np.max(A)<=9): if (SC.IsSudoRight(A)): printMes("Rating","Already Solved!") else: Adef=deepcopy(A) if (GU.CountSolutions(Adef,[],0,1)==1): diff=RT.RateProb(A) printMes("Rating","Your Problem is "+BS.getStrDiff(difficulty)) else: printMes("Rating","There isn't unique solution!") else: printMes("Rating","Wrong inputs!") mw.destroy()