def mrv_forward(table,variables,hints): # printTable(table) iteration=0 newTable=deepcopy(table) path=[] unsigned=deepcopy(variables) stack=[] newNode,result=find_min(newTable,hints,unsigned) if result==-1: stack.append([newNode,0,-1]) stack.append([newNode,1,0]) else: #print(newNode) stack.append([newNode,result,-1]) while stack: current=stack.pop() iteration+=1 path.append(current) # print(current) unsigned.remove(current[0]) test=forward_checking(newTable,current,hints,unsigned,path) # if current[0]==[3,5]: # print(test) if(test==False): while path: delete=path.pop() unsigned.append(delete[0]) mrv_recover(newTable,delete,hints) if delete[2]==0: break continue else: if len(unsigned)==0: test=finalTest(newTable,table,hints) if(test==False): while path: delete=path.pop() unsigned.append(delete[0]) mrv_recover(newTable,delete,hints) if delete[2]==0: break continue else: break else: newNode,result=find_min(newTable,hints,unsigned) if result==-1: stack.append([newNode,0,-1]) stack.append([newNode,1,0]) else: stack.append([newNode,result,-1]) for hint in hints: newTable[hint[0]][hint[1]]=table[hint[0]][hint[1]] # print(stack) printTable(newTable,iteration) print(iteration)
def degree_BTS(table,variables,hints): # printTable(table) iteration=0 newTable=deepcopy(table) path=[] unsigned=deepcopy(variables) stack=[] degree=degree_init(newTable,hints,variables) newNode=find_max(newTable,degree,unsigned,variables) stack.append([newNode,0,-1]) stack.append([newNode,1,0]) while stack: current=stack.pop() iteration+=1 path.append(current) unsigned.remove(current[0]) # print(1) test=easy_addTest(newTable,current,hints) # test=forward_checking(newTable,current,hints,unsigned,path) # if current[0]==[3,5]: # print(test) if(test==False): while path: delete=path.pop() unsigned.append(delete[0]) newTable[delete[0][0]][delete[0][1]]=-1 if delete[2]==0: break continue else: if len(unsigned)==0: test=finalTest(newTable,table,hints) if(test==False): while path: delete=path.pop() unsigned.append(delete[0]) newTable[delete[0][0]][delete[0][1]]=-1 if delete[2]==0: break continue else: break else: newNode=find_max(newTable,degree,unsigned,variables) stack.append([newNode,0,-1]) stack.append([newNode,1,0]) for hint in hints: newTable[hint[0]][hint[1]]=table[hint[0]][hint[1]] # print(stack) printTable(newTable,iteration) print(iteration)
def easy_BTS(table,variables,hints): iteration=0 stack=[[variables[0],0],[variables[0],1]] newTable=deepcopy(table) unsignVariables=0 path=[] while stack: current=stack.pop() iteration+=1 path.append(current) unsignVariables+=1 test=easy_addTest(newTable,current,hints) if(test==False): while path: delete=path.pop() unsignVariables-=1 newTable[delete[0][0]][delete[0][1]]=-1 if delete[1]==1: break continue else: if(unsignVariables==len(variables)): # end of finding mines test=finalTest(newTable,table,hints) # check ans correct if(test==False): while path: delete=path.pop() unsignVariables-=1 newTable[delete[0][0]][delete[0][1]]=-1 if delete[1]==1: break continue else: break else: stack.append([variables[unsignVariables],0]) stack.append([variables[unsignVariables],1]) for hint in hints: newTable[hint[0]][hint[1]]=table[hint[0]][hint[1]] printTable(newTable,iteration) print(iteration)
def all_BTS2(table, variables, hints): # printTable(table) iteration = 0 newTable = deepcopy(table) path = [] unsigned = deepcopy(variables) stack = [] degree = degree_init(newTable, hints, variables) #newNode,result=find_min(newTable,hints,unsigned) newNode = find_max(newTable, degree, unsigned, variables) newNode, result = find_min(newTable, hints, unsigned) if result == -1: stack.append([newNode, 0, -1]) stack.append([newNode, 1, 0]) # newNode=find_max(newTable,degree,unsigned,variables) ''' if newNode!=-1: stack.append([newNode,0,-1]) stack.append([newNode,1,0]) else: newNode,result=new_node(newTable,hints,unsigned[0]) if result==-1: stack.append([newNode,0,-1]) stack.append([newNode,1,0]) else: #print(newNode) stack.append([newNode,1,-1]) stack.append([newNode,0,0]) ''' else: #print(newNode) stack.append([newNode, 1, -1]) stack.append([newNode, 0, 0]) #stack.append([newNode,result,-1]) while stack: current = stack.pop() iteration += 1 path.append(current) # print(current) unsigned.remove(current[0]) # test=forward_checking(newTable,current,hints,unsigned,path) test = mrv_addTest(newTable, current, hints) #foward checking or not # if current[0]==[3,5]: # print(test) if (test == False): while path: delete = path.pop() unsigned.append(delete[0]) mrv_recover(newTable, delete, hints) if delete[2] == 0: break continue else: if len(unsigned) == 0: test = finalTest(newTable, table, hints) if (test == False): while path: delete = path.pop() unsigned.append(delete[0]) mrv_recover(newTable, delete, hints) if delete[2] == 0: break continue else: break else: newNode, result = find_min(newTable, hints, unsigned) if result == -1: newNode = find_max(newTable, degree, unsigned, variables) if newNode != -1: stack.append([newNode, 0, -1]) stack.append([newNode, 1, 0]) else: newNode, result = new_node(newTable, hints, unsigned[0]) if result == -1: stack.append([newNode, 0, -1]) stack.append([newNode, 1, 0]) else: #print(newNode) stack.append([newNode, 1, -1]) stack.append([newNode, 0, 0]) else: #print(newNode) stack.append([newNode, result, -1]) for hint in hints: newTable[hint[0]][hint[1]] = table[hint[0]][hint[1]] # print(stack) printTable(newTable, iteration) print(iteration)