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:

  1. All given positions won't overlap.
  2. The squirrel can take at most one nut at one time.
  3. The given positions of nuts have no order.
  4. Height and width are positive integers. 3 <= height * width <= 10,000.
  5. 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]的更多相关文章

  1. 573. Squirrel Simulation

    Problem statement: There's a tree, a squirrel, and several nuts. Positions are represented by the ce ...

  2. [LeetCode] Squirrel Simulation 松鼠模拟

    There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...

  3. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. [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 ...

  8. leetcode 874. Walking Robot Simulation

    874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...

  9. LeetCode.874-走路机器人模拟(Walking Robot Simulation)

    这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...

  10. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

随机推荐

  1. 读 Zepto 源码之神奇的 $

    经过前面三章的铺垫,这篇终于写到了戏肉.在用 zepto 时,肯定离不开这个神奇的 $ 符号,这篇文章将会看看 zepto 是如何实现 $ 的. 读Zepto源码系列文章已经放到了github上,欢迎 ...

  2. windows上安装jdk

    1.下载jdk安装包 jdk官网   http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.安装jdk 傻瓜式下一步 ...

  3. Java UDP Socket

    本文转载地址:          Java学习路上的收获:http://blog.csdn.net/qinpeng100423/article/details/8980423 一. UDP协议定义 U ...

  4. 字符串和整形数组的相互转化(JAVA程序)

    package te; public class StringConvert { static int[] a = {0,1,1,0,1,1,0,2}; static String s = " ...

  5. org.apache.commons.lang下的工具类

    1.org.apache.commons.lang.ArrayUtils 例子 package chongqingyusp; import java.util.Map; import org.apac ...

  6. 《JAVA与模式》之简单工厂模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...

  7. 利用EF ORM Mysql实体运行程序出错解决方案

    程序环境:VS2013 + mysql (server 5.7 + connector net 6.9.9 + for visual studio 1.2.6) + entity framework ...

  8. Merge INTO的用法参考

    Merge是一个非常有用的功能,类似于MySQL里的insert into on duplicate key. Oracle在9i引入了merge命令, 通过这个merge你能够在一个SQL语句中对一 ...

  9. VR问题无关方向,VR全景为您领航,全景智慧城市已势不可当

    2016年,VR绝对是互联网科技圈的一个高频词. 在这一年里,Magic Leap获得阿里领投的近8亿美元的融资,VR公司的商业价值得到认可.Oculus Rift和HTC Vive的VR产品正式发货 ...

  10. RabbitMQ系列教程之二:工作队列(Work Queues)

        今天开始RabbitMQ教程的第二讲,废话不多说,直接进入话题.   (使用.NET 客户端 进行事例演示)          在第一个教程中,我们编写了一个从命名队列中发送和接收消息的程序. ...