def minConflict(filename): board=() checks=0 board,board_size,small_row,small_col=func.convertBoard(filename) board_copy=board #func.printBoard(board_copy,board_size) board_closed=[] board_dict={} board_dict=func.updateDict(board,board_size,small_row,small_col,{}) #find the board_closed cells for i in range(0,board_size): for j in range(0,board_size): if board[i][j]!=0: board_closed.append(board_size*i+j) #initialize board open cells with random values for i in range (0,board_size): for j in range (0,board_size): if board[i][j]==0: board[i][j]=random.randrange(1,board_size+1) bool_val,checks=minConflictRec(board,board_size,small_row,small_col,board_copy,board_dict,board_closed)# call the recursive function, get the final solution and #checks if (bool_val): print("\n successfully completed sudoku. ") # success else: print("\n not completed board. ",bool_val) # failure return(board,checks) return (board,checks)
def backtrackingRecursiveMRVcp(board,board_size,small_row,small_col): global checks #base case if func.boardFull(board,board_size) : return True,checks minPos,board_dict_list=func.findNextPosMRV(board,board_size,small_row,small_col) # find the next MRV pos and list of values unassigned_row=int(minPos/board_size) unassigned_col=minPos%board_size #update list for LCV sorted board_dict_list=func.updateList(board,board_size,unassigned_row,unassigned_col,small_row,small_col,board_dict_list) if (board_dict_list==[]): return False,checks #for every fwd consistent values for digit in board_dict_list: checks=checks+1 board[unassigned_row][unassigned_col]=digit board_dict=func.updateDict(board,board_size,small_row,small_col,board_dict={}) if(backtrackingRecursiveMRVcp(board,board_size,small_row,small_col)[0]==True): # recursive calls return True,checks board[unassigned_row][unassigned_col]=0 # backtracking and reverting last move made #check for backward consistency if func.backwardConsistent(board_dict)==False: return False,checks return False,checks