美文网首页
商人渡河

商人渡河

作者: Thinkando | 来源:发表于2018-09-09 18:32 被阅读63次

三名商人各带一个随从乘船渡河。现此岸有一小船只能容纳两人,由他们自己划行。若在河的任一岸随从人数比商人多,他们就可能抢劫财物。不过如何乘船渡河的大权由商人们掌握。商人们怎样才能安全过河呢?

  • 思路
  1. 找到每一次小船可能的集合
  2. 统计岸边,商人数量和随从可能的集合
  3. 每移动一次,重新统计岸边人数,判断是否在集合2中,如果是,纪录路线,如果不是pass.
import os
import sys
import copy

N = 3               # Total people number
END = [0, 0]        # destation point

# goble decison
src_decision = [[-2,0], [-1,0], [-1,-1], [0,-1], [0,-2]]
dst_decision = [[1,0], [2,0], [1,1], [0,1],[0,2]]

# global AVAIABLE point
restrict_point_list = []

# global list to record suceess path
result_path = []

def init_restrict_point():
    for y in range(0,N+1):
        a = [0,y]
        restrict_point_list.append(a)
    for x in range(1,N):
        a = [x,x]
        restrict_point_list.append(a)
    for y in range(0,N+1):
        a = [N,y]
        restrict_point_list.append(a)
init_restrict_point()
print(restrict_point_list)

def SearchCrossRiver(start_p, derict, src_reached, dst_reached):
    # 深拷贝,子改父不改
    s_reached = copy.deepcopy(src_reached)
    d_reached = copy.deepcopy(dst_reached)
    if derict == 1:
        # 把开始的坐标加入list
        if start_p not in s_reached:
            s_reached.append(start_p)
        for decison in src_decision:
            point = []
            # 修改后的坐标
            for i in range(2):
                point.append(start_p[i]+decison[i])
            # 修改后的坐标==[0,0]结束循环
            if point == END:
                print ("It succesed !!")
                result_path.append(s_reached)
                result_path.append(d_reached)
                print(result_path)
                return 0
            elif (point in restrict_point_list) and (point not in d_reached):
                # 此岸和对岸来回迭代
                SearchCrossRiver(point, -1, s_reached, d_reached)
            else:
                pass
        return 1
    elif derict == -1:
        if start_p not in d_reached:
            d_reached.append(start_p)
        for decison in dst_decision:
            point = []
            for i in range(2):
                point.append(start_p[i] + decison[i])
            if point == END:
                result_path.append(s_reached)
                result_path.append(d_reached)
                return 0
            elif (point in restrict_point_list) and (point not in s_reached):
                SearchCrossRiver(point, 1, s_reached, d_reached)
            else:pass
        return 1
    else:
        print ("error")
        return 2
def find_function():
    start_point = [N,N]     # starting point
    init_restrict_point()

    src_reached_list = []
    dst_reached_list = []

    result = SearchCrossRiver(start_point, 1, src_reached_list, dst_reached_list)
    print ("result = ", result_path)
    print ('\n')

if __name__ == '__main__':
    find_function()

相关文章

  • 商人渡河

    三名商人各带一个随从乘船渡河。现此岸有一小船只能容纳两人,由他们自己划行。若在河的任一岸随从人数比商人多,他们就可...

  • 商人渡河的故事

    清朝年间有个商人去河南置办了一批货物,抄水路想赶往外地出售卖个好价。 某日船正顺风行驶正到河中央,忽然间乌云密布,...

  • 回溯算法之商人渡河

    回溯算法 回溯算法实际上是一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求...

  • 信不信?

    话说明朝年间济阴有个商人,渡河时船沉没了,他掉入水中大声求救。 有一个渔夫用船去救他,商人许诺:“...

  • 泰戈尔:虚伪的真诚,比魔鬼更可怕

    文|艾三月 曾经有个商人渡河时船沉了,他急忙大声呼救。正好有个渔夫闻声而来。 商人说:“我是济阳最大的富翁,你若能...

  • 人生小故事2:故事很短,寓意深长,读懂开悟就行

    济水南边有一个商人,渡河时船翻了,他大喊救命。 有一个渔人驾船去救他,船还没有开到,商人大声叫道:“我是大富翁,你...

  • 奈 何

    公无渡河 公竟渡河 如若渡河 凭何渡河

  • 典故学习之47:贾gǔ人渡河,行商坐贾gǔ

    贾gǔ人渡河:gǔ rén dù hé 比喻说话不讲信用,言而无信。商人许金不酬,失信于人,终遭灭顶之灾...

  • 成年人的世界不论对错,只谈利益

    公无渡河,公竟渡河!

  • 渡河

    时常痛恨 浮于表面的 波光 却无法承受 深潜于水底的 窒息 此岸的卒 渡过河去 从前的卒 在此岸死去

网友评论

      本文标题:商人渡河

      本文链接:https://www.haomeiwen.com/subject/cojdgftx.html