鱼C论坛

 找回密码
 立即注册
楼主: jerryxjr1220

[技术交流] python小练习(068):回溯法(深度优先搜索)30行代码求解“马踏棋盘”问题

[复制链接]
发表于 2020-8-28 22:20:02 | 显示全部楼层
厉害了,学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 23:17:03 | 显示全部楼层
学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-8 21:21:12 | 显示全部楼层
抱着学习的态度来看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-6 18:22:03 | 显示全部楼层
学到了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-6 19:11:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-6 11:46:24 From FishC Mobile | 显示全部楼层
感谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 16:30:36 | 显示全部楼层
学习一下,这个用递归吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 20:39:10 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-6 21:38:21 | 显示全部楼层
看一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-7 03:14:06 From FishC Mobile | 显示全部楼层
先回复 再学习。噢耶。加油加油加油
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-7 16:18:57 | 显示全部楼层
我没有用递归,写了一个脚本

但是脚本的执行效率很低

8X8的,感觉算不出来啊

但是6 X 4 这种,倒是 2秒钟就出来了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-7 16:28:03 | 显示全部楼层
我的代码

  1. #!/usr/bin/python3 -B
  2. import time

  3. def next_x_y(point, order):
  4.         x_y_list = [point[0], point[1]]

  5.         if order == 1:
  6.                 x_y_list[0] = int(x_y_list[0]) + 1
  7.                 x_y_list[1] = int(x_y_list[1]) + 2
  8.                 return x_y_list

  9.         if order == 2:
  10.                 x_y_list[0] = int(x_y_list[0]) + 2
  11.                 x_y_list[1] = int(x_y_list[1]) + 1
  12.                 return x_y_list

  13.         if order == 3:
  14.                 x_y_list[0] = int(x_y_list[0]) + 2
  15.                 x_y_list[1] = int(x_y_list[1]) - 1
  16.                 return x_y_list

  17.         if order == 4:
  18.                 x_y_list[0] = int(x_y_list[0]) + 1
  19.                 x_y_list[1] = int(x_y_list[1]) - 2
  20.                 return x_y_list

  21.         if order == 5:
  22.                 x_y_list[0] = int(x_y_list[0]) - 1
  23.                 x_y_list[1] = int(x_y_list[1]) - 2
  24.                 return x_y_list

  25.         if order == 6:
  26.                 x_y_list[0] = int(x_y_list[0]) - 2
  27.                 x_y_list[1] = int(x_y_list[1]) - 1
  28.                 return x_y_list

  29.         if order == 7:
  30.                 x_y_list[0] = int(x_y_list[0]) - 2
  31.                 x_y_list[1] = int(x_y_list[1]) + 1
  32.                 return x_y_list

  33.         if order == 8:
  34.                 x_y_list[0] = int(x_y_list[0]) - 1
  35.                 x_y_list[1] = int(x_y_list[1]) + 2
  36.                 return x_y_list

  37. def check_legal(next_point):
  38.         if 1 <= next_point[0] and next_point[0] <= size_of_x and 1 <= next_point[1] and next_point[1] <= size_of_y:
  39.                 return True
  40.         else:
  41.                 return False

  42. size_of_x = int(input("Please input the size of X :"))
  43. size_of_y = int(input("Please input the size of Y :"))

  44. start_point = input("Please input start point : ")

  45. start_time = time.time()

  46. main_list = []
  47. main_order_list = []

  48. main_list.append(start_point)
  49. main_order_list.append(1)

  50. end_of_search = False

  51. while not end_of_search:

  52.         if main_order_list[-1] >= 8:
  53.                 main_list.pop()
  54.                 main_order_list.pop()
  55.                 main_order_list[-1] += 1
  56.                 continue


  57.         next_point = next_x_y(main_list[-1], main_order_list[-1])
  58.         next_point_str = "".join([str(x) for x in next_point])

  59.         if  (not next_point_str in main_list) and (check_legal(next_point)):
  60.                 main_list.append("".join([str(x) for x in next_point]))
  61.                 main_order_list.append(1)

  62.                 if len(main_list) == int(size_of_x * size_of_y):
  63.                         print ("++++++++++++++++++ Find one solution ++++++++++++++++")
  64.                         print(main_list)
  65.                         print(main_order_list)
  66.                         print ("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
  67.                         print("")


  68.         else:
  69.                 if main_order_list[-1] >= 8:
  70.                         main_list.pop()
  71.                         main_order_list.pop()
  72.                         main_order_list[-1] += 1
  73.                 else:
  74.                         main_order_list[-1] += 1

  75.         shrink_order = list(set(main_order_list))
  76.         if len(shrink_order) == 1 and shrink_order[0] == 8:
  77.                 end_of_search = True

  78. end_time = time.time()

  79. used_time = end_time - start_time

  80. print("Use %10.3f second." %used_time)
复制代码



计算 6 X 4 棋盘的结果,2秒多就出来了

但是 8 X 8 ,尝试过10分钟,都没有出结果

Please input the size of X :6
Please input the size of Y :4
Please input start point : 11
++++++++++++++++++ Find one solution ++++++++++++++++
['11', '23', '31', '12', '24', '32', '51', '63', '44', '52', '64', '43', '62', '41', '53', '61', '42', '54', '33', '14', '22', '34', '13', '21']
[1, 4, 7, 1, 4, 3, 1, 7, 4, 1, 6, 3, 6, 1, 4, 7, 1, 6, 7, 4, 1, 6, 4, 1]
+++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++ Find one solution ++++++++++++++++
['11', '32', '51', '63', '44', '23', '31', '52', '64', '43', '24', '12', '33', '14', '22', '34', '13', '21', '42', '54', '62', '41', '53', '61']
[2, 3, 1, 7, 6, 4, 2, 1, 6, 7, 5, 2, 7, 4, 1, 6, 4, 2, 1, 4, 6, 1, 4, 1]
+++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++ Find one solution ++++++++++++++++
['11', '32', '51', '63', '44', '23', '31', '12', '24', '43', '64', '52', '33', '14', '22', '34', '13', '21', '42', '54', '62', '41', '53', '61']
[2, 3, 1, 7, 6, 4, 7, 1, 3, 2, 5, 7, 7, 4, 1, 6, 4, 2, 1, 4, 6, 1, 4, 1]
+++++++++++++++++++++++++++++++++++++++++++++++++++++

Use      2.272 second.

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-9 23:11:48 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-25 15:37:07 | 显示全部楼层
你好,可以看一下代码吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-28 19:26:35 | 显示全部楼层
111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-29 11:06:43 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-29 15:42:12 | 显示全部楼层
学习一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-26 13:37:47 | 显示全部楼层
学习了,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 04:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表