# coding=utf-8 ''' Created on 2016年4月6日 @author: ChenHao ''' import random # 生成数据 M = 4 N = 8 m = M n = N map = list() for i in range(1, m + 1): for j in range(1, n + 1): location = dict() location["x"] = i location["y"] = j ran = random.random() if ran> 0.5: location["value"] = 1 else: location["value"] = 0 map.append(location) # 路径方案 path_result = list() # 添加路径方案的起点 def _init_path_result(): d = dict() d["path"] = [{"x": 1, "y": 1}] d["direction_now"] = "right" path_result.append(d) # 取出一条任务(判断是否有任务) def _get_one_task(): for r in path_result: if len(r["path"]) < (m+n-1): return r return False # 得到下一步的坐标,并得到可能的Block的坐标 def _get_next_step_and_block(x_now, y_now, direction_now): next_step_result = list() if x_now < m: next_step_right = dict() next_step_right["x"] = x_now + 1 next_step_right["y"] = y_now next_step_right["direction"] = "right" if direction_now is "right": next_step_right["block"] = None else: if y_now < n: d_block = dict() d_block["x"] = x_now d_block["y"] = y_now + 1 next_step_right["block"] = d_block else: next_step_right["block"] = None next_step_result.append(next_step_right) if y_now < n: next_step_down = dict() next_step_down["x"] = x_now next_step_down["y"] = y_now + 1 next_step_down["direction"] = "down" if direction_now is "left": next_step_down["block"] = None else: if x_now < m: d_block = dict() d_block["x"] = x_now + 1 d_block["y"] = y_now next_step_down["block"] = d_block else: next_step_down["block"] = None next_step_result.append(next_step_down) return next_step_result # 比较路径地图和初始化地图的数量差异 def _compare_get_min_munber(rs): # 行走路径的比较 pass # block的比较 ''' 执行函数 ''' # 初始化起点数据 _init_path_result() # 生成路径地图 while(_get_one_task()): rs = _get_one_task() size = len(rs["path"]) now_site = rs["path"][size - 1] next_step_result = _get_next_step_and_block(now_site["x"], now_site["y"], rs["direction_now"]) if len(next_step_result) == 2: pass else: pass # 将所有路径地图进行对比得到最小差异 min_number = m*n for rs in path_result: tmp_number = _compare_get_min_munber(rs) if tmp_number < min_number: min_number = tmp_number print (min_number)