def sudo_solve_iter(self): # 排除法解题 self.sudo_exclude() # logger.debug(f'excluded, current result:\n{self.value}')if self.verify_value(): if self.get_num_count() == 81: # solve successreturnelse: logger.info(f'current no. of fixed answers: {self.get_num_count()}') point = self.get_best_point() index = 0 items = self.add_to_queue(point, index) logger.info(f'add to LIFO queue and guessing {items[index]}/{items}: ' f'{[x.point for x in self.record_queue.queue]}') self.guess_times += 1 return self.sudo_solve_iter() while True: if self.record_queue.empty(): # raise Exception('Sudo is wrong, no answer!') logger.error(f'Guessed {self.guess_times} times. Sudo is wrong, no answer!') exit() # check value ERROR, need to try next index or rollback record = self.record_queue.get() point = record.point index = record.point_index + 1 items = record.value[point] self.value = record.value logger.info(f'Recall! Pop previous point, {items} @{point}') # 判断索引是否超出范围# if not exceed,则再回溯一次if index < len(items): items = self.add_to_queue(point, index) logger.info(f'guessing next index: answer={items[index]}/{items} @{point}') self.guess_times += 1 return self.sudo_solve_iter() 复制代码
实战
最难数独,需要猜测109次?!确实很变态啊!不过就算i3级别的旧电脑,也只要0.3s左右。
/home/kevin/git/sudo-py3/venv/bin/python /home/kevin/git/sudo-py3/sudo-recur.py DEBUG:__main__:current no. of fixed answers: 21 DEBUG:__main__:add to LIFO queue and guessing 3/[3, 9]: [(7, 6)] DEBUG:__main__:current no. of fixed answers: 22 DEBUG:__main__:add to LIFO queue and guessing 5/[5, 9]: [(7, 6), (6, 6)] DEBUG:__main__:current no. of fixed answers: 25 DEBUG:__main__:add to LIFO queue and guessing 6/[6, 8, 9]: [(7, 6), (6, 6), (5, 6)] DEBUG:__main__:verify failed. dup in col 0 DEBUG:__main__:Recall! Pop previous point, [6, 8, 9] @(5, 6) DEBUG:__main__:guessing next index: answer=8/[6, 8, 9] @(5, 6) DEBUG:__main__:current no. of fixed answers: 29 DEBUG:__main__:add to LIFO queue and guessing 2/[2, 4, 6]: [(7, 6), (6, 6), (5, 6), (5, 1)] DEBUG:__main__:verify failed. dup in col 0 ... DEBUG:__main__:guessing next index: answer=3/[2, 3, 8] @(4, 3) DEBUG:__main__:current no. of fixed answers: 41 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 4]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (1, 1)] DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 4] @(1, 1) DEBUG:__main__:guessing next index: answer=4/[1, 4] @(1, 1) DEBUG:__main__:current no. of fixed answers: 42 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 6, 8]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (1, 1), (4, 1)] DEBUG:__main__:verify failed. dup in row 4 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:guessing next index: answer=6/[1, 6, 8] @(4, 1) DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:guessing next index: answer=8/[1, 6, 8] @(4, 1) DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:Recall! Pop previous point, [1, 4] @(1, 1) DEBUG:__main__:Recall! Pop previous point, [2, 3, 8] @(4, 3) DEBUG:__main__:guessing next index: answer=8/[2, 3, 8] @(4, 3) DEBUG:__main__:current no. of fixed answers: 33 DEBUG:__main__:add to LIFO queue and guessing 2/[2, 3]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3)] DEBUG:__main__:current no. of fixed answers: 42 DEBUG:__main__:add to LIFO queue and guessing 3/[3, 4]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3), (7, 1)] DEBUG:__main__:current no. of fixed answers: 45 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 6]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3), (7, 1), (4, 1)] DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6] @(4, 1) DEBUG:__main__:guessing next index: answer=6/[1, 6] @(4, 1) INFO:__main__:Done! guessed 109 times, in 0.540sec INFO:__main__:Puzzle: [[8 0 0 0 0 0 0 0 0] [0 0 3 6 0 0 0 0 0] [0 7 0 0 9 0 2 0 0] [0 5 0 0 0 7 0 0 0] [0 0 0 0 4 5 7 0 0] [0 0 0 1 0 0 0 3 0] [0 0 1 0 0 0 0 6 8] [0 0 8 5 0 0 0 1 0] [0 9 0 0 0 0 4 0 0]] INFO:__main__:Answer: [[8 1 2 7 5 3 6 4 9] [9 4 3 6 8 2 1 7 5] [6 7 5 4 9 1 2 8 3] [1 5 4 2 3 7 8 9 6] [3 6 9 8 4 5 7 2 1] [2 8 7 1 6 9 5 3 4] [5 2 1 9 7 4 3 6 8] [4 3 8 5 2 6 9 1 7] [7 9 6 3 1 8 4 5 2]] 复制代码
数学建模方法——皮尔逊相关系数及其显著性检验 (Pearson correlation coefficient)0 皮尔逊相关系数简介相关系数是衡量两个数据相关关系的指标 两个数据相关在某种程度上可以帮助人们理解事物的变化规律 例如在商品推荐中 我们已知一个用户 A 的购买喜好 同时发现另一个用户 B 的购买数据和 A 相关性很高 那么我们可以根据 A 的喜好去给 B 推荐相关的产品 等等 皮尔逊相关系数 Pearsoncorre 就是最为常用的用来衡量两个变量线性相关关系的指标 有了