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
def backtrackingRecursiveMRVfwd(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 next pos and set of valid moves unassigned_row=int(minPos/board_size) unassigned_col=minPos%board_size #update the list with checks done to see if forward consistency is valid 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 digit in board_dict_list: checks=checks+1 board[unassigned_row][unassigned_col]=digit # assignment of value if(backtrackingRecursiveMRVfwd(board,board_size,small_row,small_col)[0]==True): # recursive call return True,checks board[unassigned_row][unassigned_col]=0 # backtracking and reverting return False,checks
def backtrackingRecursiveMRV(board,board_size,small_row,small_col): global checks #base case if func.boardFull(board,board_size) : # terminating state return True,checks minPos,board_dict_list=func.findNextPosMRV(board,board_size,small_row,small_col) # find the minimum remaining value pos and list of values unassigned_row=int(minPos/board_size) # convert to unassigned row and col value unassigned_col=minPos%board_size if (board_dict_list==[]): # if list is empty return False,checks #board_dict_list=func.updateList(board,board_size,unassigned_row,unassigned_col,small_row,small_col,board_dict_list) for digit in board_dict_list: checks=checks+1 board[unassigned_row][unassigned_col]=digit # value assignment if(backtrackingRecursiveMRV(board,board_size,small_row,small_col)[0]==True): # recursive call return True,checks board[unassigned_row][unassigned_col]=0 # revert the previous made move return False,checks