mazeForWXJ.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # coding=utf-8
  2. '''
  3. Created on 2016年4月6日
  4. @author: ChenHao
  5. '''
  6. import random
  7. # 生成数据
  8. M = 4
  9. N = 8
  10. m = M
  11. n = N
  12. map = list()
  13. for i in range(1, m + 1):
  14. for j in range(1, n + 1):
  15. location = dict()
  16. location["x"] = i
  17. location["y"] = j
  18. ran = random.random()
  19. if ran> 0.5:
  20. location["value"] = 1
  21. else:
  22. location["value"] = 0
  23. map.append(location)
  24. # 路径方案
  25. path_result = list()
  26. # 添加路径方案的起点
  27. def _init_path_result():
  28. d = dict()
  29. d["path"] = [{"x": 1, "y": 1}]
  30. d["direction_now"] = "right"
  31. path_result.append(d)
  32. # 取出一条任务(判断是否有任务)
  33. def _get_one_task():
  34. for r in path_result:
  35. if len(r["path"]) < (m+n-1):
  36. return r
  37. return False
  38. # 得到下一步的坐标,并得到可能的Block的坐标
  39. def _get_next_step_and_block(x_now, y_now, direction_now):
  40. next_step_result = list()
  41. if x_now < m:
  42. next_step_right = dict()
  43. next_step_right["x"] = x_now + 1
  44. next_step_right["y"] = y_now
  45. next_step_right["direction"] = "right"
  46. if direction_now is "right":
  47. next_step_right["block"] = None
  48. else:
  49. if y_now < n:
  50. d_block = dict()
  51. d_block["x"] = x_now
  52. d_block["y"] = y_now + 1
  53. next_step_right["block"] = d_block
  54. else:
  55. next_step_right["block"] = None
  56. next_step_result.append(next_step_right)
  57. if y_now < n:
  58. next_step_down = dict()
  59. next_step_down["x"] = x_now
  60. next_step_down["y"] = y_now + 1
  61. next_step_down["direction"] = "down"
  62. if direction_now is "left":
  63. next_step_down["block"] = None
  64. else:
  65. if x_now < m:
  66. d_block = dict()
  67. d_block["x"] = x_now + 1
  68. d_block["y"] = y_now
  69. next_step_down["block"] = d_block
  70. else:
  71. next_step_down["block"] = None
  72. next_step_result.append(next_step_down)
  73. return next_step_result
  74. # 比较路径地图和初始化地图的数量差异
  75. def _compare_get_min_munber(rs):
  76. # 行走路径的比较
  77. pass
  78. # block的比较
  79. '''
  80. 执行函数
  81. '''
  82. # 初始化起点数据
  83. _init_path_result()
  84. # 生成路径地图
  85. while(_get_one_task()):
  86. rs = _get_one_task()
  87. size = len(rs["path"])
  88. now_site = rs["path"][size - 1]
  89. next_step_result = _get_next_step_and_block(now_site["x"], now_site["y"], rs["direction_now"])
  90. if len(next_step_result) == 2:
  91. pass
  92. else:
  93. pass
  94. # 将所有路径地图进行对比得到最小差异
  95. min_number = m*n
  96. for rs in path_result:
  97. tmp_number = _compare_get_min_munber(rs)
  98. if tmp_number < min_number:
  99. min_number = tmp_number
  100. print (min_number)