2023-01-10:智能机器人要坐专用电梯把货物送到指定地点, 整栋楼只有一部电梯,并且由于容量限制智能机器人只能放下一件货物, 给定K个货物,每个货物都有所在楼层(from)和目的楼层(to),
2023-01-10:智能机器人要坐专用电梯把货物送到指定地点,
整栋楼只有一部电梯,并且由于容量限制智能机器人只能放下一件货物,
给定K个货物,每个货物都有所在楼层(from)和目的楼层(to),
假设电梯速度恒定为1,相邻两个楼层之间的距离为1,
例如电梯从10层去往19层的时间为9,
机器人装卸货物的时间极快不计入,
电梯初始地点为第1层,机器人初始地点也是第1层,
并且在运送完所有货物之后,机器人和电梯都要回到1层。
返回智能机器人用电梯将每个物品都送去目标楼层的最快时间。
注意:如果智能机器人选了一件物品,则必须把这个物品送完,不能中途丢下。
输入描述:
正数k,表示货物数量,1 <= k <= 16,
from、to数组,长度都是k,1 <= from[i]、to[i] <= 10000,
from[i]表示i号货物所在的楼层,
to[i]表示i号货物要去往的楼层,
返回最快的时间。
答案2023-01-10:
状态压缩的动态规划。代码用rust和solidity编写。
代码用solidity编写。代码如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Hello{
function main() public pure returns (int32){
int32 k = 3;
int32[] memory from = new int32[](uint32(k));
from[0] = 1;
from[1] = 3;
from[2] = 6;
// from[3] = 5;
// from[4] = 7;
int32[] memory to = new int32[](uint32(k));
to[0] = 4;
to[1] = 6;
to[2] = 3;
// to[3] = 2;
// to[4] = 8;
int32 ans = minCost2(k,from,to);
return ans;
}
// 正式方法
function minCost2(int32 k, int32[] memory from, int32[] memory to) public pure returns (int32){
int32 m = leftk(k);
int32[][] memory dp = new int32[][](uint32(m));
for (int32 i = 0; i < m; i++) {
dp[uint32(i)]=new int32[](uint32(k));
for (int32 j = 0; j < k; j++) {
dp[uint32(i)][uint32(j)] = -1;
}
}
return f(0, 0, k, from, to, dp);
}
function leftk(int32 k) public pure returns (int32){
int32 ans = 1;
while (k>0){
ans*=2;
k--;
}
return ans;
}
// 2^16 = 65536 * 16 = 1048576
// 1048576 * 16 = 16777216
function f(int32 status, int32 i, int32 k, int32[] memory from, int32[] memory to, int32[][] memory dp) public pure returns (int32){
if (dp[uint32(status)][uint32(i)] != -1) {
return dp[uint32(status)][uint32(i)];
}
int32 ans = 2147483647;
if (status == leftk(k) - 1) {
ans = to[uint32(i)] - 1;
} else {
for (int32 j = 0; j < k; j++) {
if ((status & leftk(j)) == 0) {
int32 t = 0;
if(status == 0){
t=1;
}else{
t = to[uint32(i)];
}
int32 come = abs(from[uint32(j)] - t);
int32 deliver = abs(to[uint32(j)] - from[uint32(j)]);
int32 next = f(status | leftk(j), j, k, from, to, dp);
ans = min(ans, come + deliver + next);
}
}
}
dp[uint32(status)][uint32(i)] = ans;
return ans;
}
function abs(int32 a)public pure returns (int32){
if(a<0){
return -a;
}else{
return a;
}
}
function min(int32 a,int32 b)public pure returns (int32){
if(a<b){
return a;
}else{
return b;
}
}
}

代码用rust编写。代码如下:
use rand::Rng;
use std::iter::repeat;
fn main() {
let k = 3;
let mut from = vec![1, 3, 6];
let mut to = vec![4, 6, 3];
let ans1 = min_cost1(k, &mut from, &mut to);
let ans2 = min_cost2(k, &mut from, &mut to);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
let k = 5;
let mut from = vec![1, 3, 6, 5, 7];
let mut to = vec![4, 6, 3, 2, 8];
let ans1 = min_cost1(k, &mut from, &mut to);
let ans2 = min_cost2(k, &mut from, &mut to);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
let nn: i32 = 8;
let vv: i32 = 100;
let test_time: i32 = 5000;
println!("测试开始");
for i in 0..test_time {
let k = rand::thread_rng().gen_range(0, nn) + 1;
let mut from = random_array(k, vv);
let mut to = random_array(k, vv);
let ans1 = min_cost1(k, &mut from, &mut to);
let ans2 = min_cost2(k, &mut from, &mut to);
if ans1 != ans2 {
println!("出错了!{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!("测试结束");
}
// 0 1 2
// from = {3, 6, 2}
// to = {7, 9, 1}
// from[i] : 第i件货,在哪个楼层拿货,固定
// to[i] : 第i件货,去哪个楼层送货,固定
// k : 一共有几件货,固定
// status : 00110110
// last : 在送过的货里,最后送的是第几号货物
// 返回值: 送完的货,由status代表,
// 而且last是送完的货里的最后一件,后续所有没送过的货都送完,
// 最后回到第一层,返回最小耗时
// k = 7
// status = 01111111
// 0000000000001
// 0000010000000 -1
// 0000001111111
fn zuo(status: i32, last: i32, k: i32, from: &mut Vec<i32>, to: &mut Vec<i32>) -> i32 {
if status == (1 << k) - 1 {
// 所有货送完了,回到1层去了
return to[last as usize] - 1;
} else {
// 不是所有货都送完!
let mut ans = i32::MAX;
for cur in 0..k {
// status : 0010110
// 1
if (status & (1 << cur)) == 0 {
// 当前cur号的货物,可以去尝试
// to[last]
// cur号的货,to[last] -> from[cur]
let come = abs(to[last as usize] - from[cur as usize]);
let delive = abs(to[cur as usize] - from[cur as usize]);
let next = zuo(status | (1 << cur), cur, k, from, to);
let cur_ans = come + delive + next;
ans = get_min(ans, cur_ans);
}
}
return ans;
}
}
fn get_min<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a < b {
a
} else {
b
}
}
fn abs(a: i32) -> i32 {
if a < 0 {
-a
} else {
a
}
}
// 暴力方法
// 全排序代码
fn min_cost1(k: i32, from: &mut Vec<i32>, to: &mut Vec<i32>) -> i32 {
return process(0, k, from, to);
}
fn process(i: i32, k: i32, from: &mut Vec<i32>, to: &mut Vec<i32>) -> i32 {
if i == k {
let mut ans = 0;
let mut cur = 1;
for j in 0..k {
ans += abs(from[j as usize] - cur);
ans += abs(to[j as usize] - from[j as usize]);
cur = to[j as usize];
}
return ans + cur - 1;
} else {
let mut ans = i32::MAX;
for j in i..k {
swap(from, to, i, j);
ans = get_min(ans, process(i + 1, k, from, to));
swap(from, to, i, j);
}
return ans;
}
}
fn swap(from: &mut Vec<i32>, to: &mut Vec<i32>, i: i32, j: i32) {
let tmp = from[i as usize];
from[i as usize] = from[j as usize];
from[j as usize] = tmp;
let tmp = to[i as usize];
to[i as usize] = to[j as usize];
to[j as usize] = tmp;
}
// 正式方法
fn min_cost2(k: i32, from: &mut Vec<i32>, to: &mut Vec<i32>) -> i32 {
let m = 1 << k;
let mut dp: Vec<Vec<i32>> = repeat(repeat(-1).take(k as usize).collect())
.take(m as usize)
.collect();
return f(0, 0, k, from, to, &mut dp);
}
// 2^16 = 65536 * 16 = 1048576
// 1048576 * 16 = 16777216
fn f(
status: i32,
i: i32,
k: i32,
from: &mut Vec<i32>,
to: &mut Vec<i32>,
dp: &mut Vec<Vec<i32>>,
) -> i32 {
if dp[status as usize][i as usize] != -1 {
return dp[status as usize][i as usize];
}
let mut ans = i32::MAX;
if status == (1 << k) - 1 {
ans = to[i as usize] - 1;
} else {
for j in 0..k {
if (status & (1 << j)) == 0 {
let come = abs(from[j as usize] - if status == 0 { 1 } else { to[i as usize] });
let deliver = abs(to[j as usize] - from[j as usize]);
let next = f(status | (1 << j), j, k, from, to, dp);
ans = get_min(ans, come + deliver + next);
}
}
}
dp[status as usize][i as usize] = ans;
return ans;
}
fn random_array(n: i32, v: i32) -> Vec<i32> {
let mut ans: Vec<i32> = repeat(0).take(n as usize).collect();
for i in 0..n {
ans[i as usize] = rand::thread_rng().gen_range(0, v) + 1;
}
return ans;
}

2023-01-10:智能机器人要坐专用电梯把货物送到指定地点, 整栋楼只有一部电梯,并且由于容量限制智能机器人只能放下一件货物, 给定K个货物,每个货物都有所在楼层(from)和目的楼层(to),的更多相关文章
- 2023 01 19 HW
2023 01 19 HW Okay, then let's start. Okay. Maybe Karina, we start with the C2 design freeze. Yeah, ...
- 10大白帽黑客专用的 Linux 操作系统
原文出处: Irshad Pathoor 译文出处:Linux中国 欢迎分享原创到伯乐头条 今天让我们来介绍十个黑客专用的操作系统,它们被白帽黑客用作渗透测试的工具.这里我把 Kali Lin ...
- Cheatsheet: 2016 10.01 ~ 10.31
Docker Introduction to Docker Monitoring Database MongoDB: The Good, The Bad, and The Ugly Web 4 Key ...
- Cheatsheet: 2015 10.01 ~ 10.31
.NET Publishing your ASP.NET App to Linux in 5 minutes with Docker Integrating AngularJS with ASP.NE ...
- Cheatsheet: 2013 10.01 ~ 10.08
Other 20 Tips for becoming a better programmer Top 10 Movies for Programmers .NET RaptorDB - The Key ...
- A.01.10—模块的输出—PWM高端输出
PWM高端输出比低端输出用得多,如上次提到的卤素灯的控制均是采用高端输出的. PWM高端输出与PWM低端输出的差异就像固态高端输出与固态低端输出的差异类似,从线路失效后对用户的影响来看:高端输出为控制 ...
- 2019.01.10 bzoj1095: [ZJOI2007]Hide 捉迷藏(动态点分治)
传送门 蒟蒻真正意义上做的第一道动态点分治! 题意:给一棵最开始所有点都是黑点的树,支持把点的颜色变成从黑/白色变成白/黑色,问当前状态树上两个最远黑点的距离. 思路: 首先考虑不带修改一次点分治怎么 ...
- @雅礼集训01/10 - T1@ matrix
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个矩阵.求它的所有子矩阵中本质不同的行的个数之和. inp ...
- Cheatsheet: 2014 10.01 ~ 10.30
.NET ASP.NET Web Api: Unwrapping HTTP Error Results and Model State Dictionaries Client-Side HTTP 20 ...
- 2016/01/10 C++ Primer 小记 —— 命令行编译环境配置
OK!第一篇博文!自贺一下! 今日看了此书的前几页.嗯,说得挺全,基础易懂. 之前学过c++,但没用过命令行编译. 本人用的VS里的编译器,文件名是cl.exe,在VC目录下. 虽然有了编译器,但并不 ...
随机推荐
- 廖雪峰python教程
来源于廖雪峰博客园python教程 8-25 编程语言分类 机器语言 计算机内部只能接受二进制代码,用二进制代码0和1描述的指令称为机器指令,全部机器指令的集合构成计算机的机器语言,用机器语言编程的程 ...
- go设置http代理
打算把之前python写的程序切换到golang, 结果调试时发现fiddler无法正常捕获go的http请求 需要设置代理才可以正常捕获 const HttpProxyUrl = "htt ...
- GPU Skinning
这个工具的作用是同一种角色在同屏里面出现一大堆时,可以大幅度的降低DrawCall.我试了一下,感觉挺有意思的,各位可以试试.它的原理实际上是把骨骼矩阵存在配置文件里面,然后通过特殊的shader,计 ...
- 基于TDesign风格的Blazor企业级UI组件库
作为一名Web开发人员,开发前端少不了使用JavaScript,而Blazor就是微软推出的基于.net平台交互式客户 Web UI 框架,可以使用C#替代JavaScript,减少我们的技术栈.降低 ...
- ZOJ 3735 Josephina and RPG (概率dp)
题意:给你一个n,然后给你C(n,3)个队伍, 给你每个队伍之间的胜率. 接下来给你m个队伍,让你依次跟他们比赛,开始你能选择任意的队伍,如果你打赢了一支队伍,你可以选择换成输给你的这个队伍或者不换, ...
- 手把手 Golang 实现静态图像与视频流人脸识别
说起人脸识别,大家首先想到的实现方式应该是 Python 去做相关的处理,因为相关的机器学习框架,库都已经封装得比较好了.但是我们今天讨论的实现方式换成 Golang,利用 Golang 去做静态图像 ...
- 使用python爬虫爬取链家潍坊市二手房项目
使用python爬虫爬取链家潍坊市二手房项目 需求分析 需要将潍坊市各县市区页面所展示的二手房信息按要求爬取下来,同时保存到本地. 流程设计 明确目标网站URL( https://wf.lianjia ...
- 【LeetCode2180】[Go/C++/C#/Ruby/Swift/Kotlin/Rust/PHP/TS/Racket/Dart/Java/Elixir/Scala/Erlang] 统计各位数字之和为偶数的整数个数
目录 题解地址 代码 golang c++ C# ruby swift kotlin rust php typescript racket dart java elixir scala erlang ...
- Linux常用命令 备查
区分关系: 有很多类型的shell,最常见的一种Shell是bash env 查看所有环境变量 用echo查看环境变量 echo $LOGNAME echo $PATH https://shimo.i ...
- 还在stream中使用peek?不要被这些陷阱绊住了
目录 简介 peek的定义和基本使用 peek的流式处理 Stream的懒执行策略 peek为什么只被推荐在debug中使用 peek和map的区别 总结 简介 自从JDK中引入了stream之后,仿 ...