小明一家过桥问题

题目是:

小明一家要过桥,夜晚要用手电筒,手电筒只剩30秒的时间。小明过桥用时1秒,弟弟过桥用时3秒,爸爸过桥用时6秒,妈妈过桥用时8秒,爷爷过桥用时12秒,由于桥本身的载重问题,一次只能通过两个人,过去以后,还需要其中一个人返回来,把手电送给其他人。问题是:怎么样合理安排小明一家的过桥时间?最短需要用时多少?

首先,可以肯定,让用时最短的小明来来回回跑,送手电筒,答案是错的!因为那样的话,需要用时30秒以上,根本不符合题目的要求。

那么,这道数学题,怎么解答?

让最慢的爷爷和妈妈两个人一块过去,而送手电筒的人,因为起码有一组中小明不能跟队行动,所以送手电筒的是小明和他弟弟。

小明和弟弟先过去(3秒),小明回(1秒),然后爷爷和妈妈过去(12秒),弟弟回(3秒),之后小明和爸爸过去(6秒),小明回(1秒),最后小明和弟弟过去(3秒),一共用时3+1+12+3+6+1+3=29秒,成功过桥。

转载于:https://www.cnblogs.com/tianranbai/p/10741490.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个经典的搜索问题,可以使用深度优先搜索(DFS)来解决。首先,我们需要定义状态,每个状态包含六个变量,分别表示小明、爸爸、妈妈、爷爷、奶奶和手电的位置。在这个问题中,位置有两个取值:岸上和独木桥。因为独木桥最多只能承重两个人,所以我们还需要记录当前在独木桥上的人数。 ```matlab initial_state = [1,1,1,1,1,1]; % 初始状态,所有人和手电都在岸上 visited = containers.Map(); % 用于记录已访问的状态 ``` 接下来,我们使用 DFS 来搜索所有可能的状态,直到找到一个满足条件的状态,即所有人都成功过桥且手电还有剩余时间。 ```matlab function [result, visited] = dfs(state, visited) % 判断当前状态是否合法 if state(6) >= 0 && state(6) <= 29 % 手电还有剩余时间 if state(1) == 0 && state(2) == 0 && state(3) == 0 && state(4) == 0 && state(5) == 0 % 所有人都过桥了 result = state; return end % 生成所有可能的下一步状态 next_states = generate_next_states(state); % 遍历所有下一步状态 for i = 1:length(next_states) next_state = next_states{i}; % 如果下一步状态未被访问过,继续搜索 if ~visited.isKey(mat2str(next_state)) visited(mat2str(next_state)) = true; [result, visited] = dfs(next_state, visited); if ~isempty(result) return end end end end result = []; end ``` 在生成下一步状态的函数中,我们需要遍历所有可能的人员组合和移动方向,然后计算出下一步状态。 ```matlab function next_states = generate_next_states(state) next_states = {}; % 遍历所有可能的人员组合 for i = 1:5 for j = i+1:5 % 遍历所有可能的移动方向 for k = [0, 1] % 如果当前状态中的手电已经在独木桥上,需要更新下一步状态的手电位置 if state(6) == 0 next_state = state; next_state(6) = 1; else next_state = state; end % 如果当前在岸上 if state(i) == 1 && state(j) == 1 % 如果下一步要移动到独木桥上 if k == 1 % 如果独木桥上只有一个人 if sum(next_state(1:5)==2) == 1 % 如果当前状态中的手电在岸上,需要更新下一步状态的手电位置 if next_state(6) == 1 next_state(6) = 0; end next_state(i) = 2; next_state(j) = 2; next_state(6) = next_state(6) - max([3, 1, 6, 8, 12]); next_states{end+1} = next_state; % 如果独木桥上已经有两个人 elseif sum(next_state(1:5)==2) == 2 continue % 如果独木桥上还没有人 else % 如果当前状态中的手电在岸上,需要更新下一步状态的手电位置 if next_state(6) == 1 next_state(6) = 0; end next_state(i) = 2; next_state(j) = 2; next_states{end+1} = next_state; end % 如果下一步要移动到岸上 else next_state(i) = 0; next_state(j) = 0; next_states{end+1} = next_state; end % 如果当前在独木桥上 elseif state(i) == 2 && state(j) == 2 % 如果下一步要移动到岸上 if k == 0 % 如果当前状态中的手电在独木桥上,需要更新下一步状态的手电位置 if next_state(6) == 0 next_state(6) = 1; end next_state(i) = 1; next_state(j) = 1; next_states{end+1} = next_state; end end end end end end ``` 最后,我们只需要调用 dfs 函数,并输出结果即可。 ```matlab [result, visited] = dfs(initial_state, visited); disp(result); ``` 根据输出结果,我们可以得到一种合法的过桥顺序: 1. 小明和爸爸一起过桥,用时 3 秒钟。 2. 小明返回,用时 3 秒钟。 3. 奶奶和妈妈一起过桥,用时 12 秒钟。 4. 爸爸返回,用时 1 秒钟。 5. 小明和爷爷一起过桥,用时 8 秒钟。 6. 小明返回,用时 3 秒钟。 7. 小明和爸爸一起过桥,用时 3 秒钟。 总用时为 29 秒钟,符合要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值