[leetcode-573-Squirrel Simulation]
There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. The squirrel can only take at most one nut at one time and can move in four directions - up, down, left and right, to the adjacent cell. The distance is represented by the number of moves.
Example 1:
Input:
Height : 5
Width : 7
Tree position : [2,2]
Squirrel : [4,4]
Nuts : [[3,0], [2,5]]
Output: 12
Explanation:

Note:
- All given positions won't overlap.
- The squirrel can take at most one nut at one time.
- The given positions of nuts have no order.
- Height and width are positive integers. 3 <= height * width <= 10,000.
- The given positions contain at least one nut, only one tree and one squirrel.
思路:
由于松鼠每次只能走一个单位,而且每次只能带一个坚果到树下,那么除了第一个要捡的坚果外,从树出发,捡其他每一个
坚果的距离都要乘以2(从树到坚果i的距离 +从坚果i到树的距离)。
不妨认为捡每一个坚果都是从树出发,那么最终的距离:
dis = 2*(所有getDis(tree,nut[i])的和) - getDis(tree[i],nut[i])) + getDis(squireel,nut[i])
int getDis(vector<int>& a,vector<int>& b)
{
return abs(a[] - b[]) + abs(a[] - b[]);
}
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int> >& nuts)
{
//dis = 2*(所有getDis(tree,nut[i])) - getDis(tree,nut[i])) + getDis(squireel,nut[i])
int tempdis = ;
int distance = ;
for(int i=;i<nuts.size();i++)
{
tempdis += *getDis(tree,nuts[i]);
}
for(int i=;i<nuts.size();i++)
{
int distanceTemp = tempdis - getDis(tree,nuts[i]) + getDis(squirrel,nuts[i]);
if(distance >distanceTemp) distance = distanceTemp;
}
return distance;
}
[leetcode-573-Squirrel Simulation]的更多相关文章
- 573. Squirrel Simulation
Problem statement: There's a tree, a squirrel, and several nuts. Positions are represented by the ce ...
- [LeetCode] Squirrel Simulation 松鼠模拟
There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- [LeetCode] 874. Walking Robot Simulation 走路机器人仿真
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of th ...
- leetcode 874. Walking Robot Simulation
874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
- C#LeetCode刷题之#874-模拟行走机器人(Walking Robot Simulation)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...
随机推荐
- RUN vs CMD vs ENTRYPOINT - 每天5分钟玩转 Docker 容器技术(17)
RUN.CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆.本节将通过实践详细讨论它们的区别. 简单的说: RUN 执行命令并创建新的镜像层,RUN 经常用于 ...
- docker核心概念及centos6下安装
Docker三大核心概念 镜像 容器 仓库 镜像 docker镜像类似于虚拟机镜像,可以将它理解为一个面向Docker引擎的只读模板,包含了文件系统. 容器 1.容器是从镜像创建的应用运行实例,容器和 ...
- sparklyr包:实现Spark与R的接口
日前,Rstudio公司发布了sparklyr包.该包具有以下几个功能: 实现R与Spark的连接—sparklyr包提供了一个完整的dplyr后端 筛选并聚合Spark数据集,接着在R中实现分析与可 ...
- My "Top 5 R Functions"(转)
In preparation for a R Workgroup meeting, I started thinking about what would be my "Top 5 R Fu ...
- OpenCv关于灰度积分图的SSE代码学习和改进。
最近一直沉迷于SSE方面的优化,实在找不到想学习的参考资料了,就拿个笔记本放在腿上翻翻OpenCv的源代码,无意中看到了OpenCv中关于积分图的代码,仔细研习了一番,觉得OpenCv对SSE的灵活运 ...
- RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World
一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到邮筒之时,你可以确定的是邮递员先生肯定会把邮件发送到需要接收邮件的人 ...
- 7.ReadWriteLock接口及其实现ReentrantReadWriteLock
Java并发包的locks包里的锁基本上已经介绍得差不多了,ReentrantLock重入锁是个关键,在清楚的了解了同步器AQS的运行机制后,实际上再分析这些锁就会显得容易得多,这章节主讲另外一个重要 ...
- java中的一些规则(菜鸟的课堂笔记)
ls 查看目录下文件 java规则 代码都定义在类中,用class定义 禁止一个源文件写两个类: 一个源文件中,只能有一个类文件是pubic: 一个源文件中如果有多个类,编译完之后会产生多个class ...
- 【JAVAWEB学习笔记】29_文件的上传------commons-fileupload
今天内容: 文件的上传------commons-fileupload 文件上传和下载的实质:文件的拷贝 文件上传:从本地拷贝到服务器磁盘上 客户端需要编写文件上传表单---->服务端需要编 ...
- Selenium WebDriver + python 自动化测试框架
目标 组内任何人都可以进行自动化测试用例的编写 完全分离测试用例和自动化测试代码,就像写手工测试用例一下,编写excel格式的测试用例,包括步骤.检查点,然后执行自动化工程,即可执行功能自动化测试用例 ...