[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 ...
随机推荐
- 浅谈mmap()和ioremap()的用法与区别
一.mmap()mmap()函数是用来将设备内存线性地址映射到用户地址空间.(1)首先映射基地址,再通过偏移地址寻址:(2)unsigned char *map_cru_base=(unsigned ...
- 解决ssh或ftp下root用户认证失败问题
问题:当连接ssh远程终端或使用ftp方式进行文件传输时,使用普通用户可以进行远程登录,但使用root用户则认证失败,提示密码错误.而我们在普通用户登录下,su - root,验证密码,是可以正常切换 ...
- 【JAVAWEB学习笔记】19_事务
事务 学习目标 案例-完成转账 一.事务概述 1.什么是事务 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败 就是将n个组成单元放到一个事务中 2.mysql的事务 默认的 ...
- Adobe Photoshop CS6中文破解MAC版
Adobe Photoshop CS6中文破解MAC版 下载地址及破解方法 http://www.sdifenzhou.com/657.html
- 【Netty】Netty传输
一.前言 在简单学习了Netty中的组件后,接着学习Netty中数据的传输细节. 二.传输 2.1 传输示例 Netty中的数据传输都是使用的字节类型,下面通过一个实例进行说明,该实例中服务器接受请求 ...
- 干货——基于Nop的精简版开发框架(附源码)
.NET的开发人员应该都知道这个大名鼎鼎的高质量b2c开源项目-nopCommerce,基于EntityFramework和MVC开发,拥有透明且结构良好的解决方案,同时结合了开源和商业软件的最佳特性 ...
- 你会python不?当你听到这个问题要谨慎回答!!!
问:你会python不? 答:python啊,略微有点小研究,虽然不精通,但是写写网络小爬虫,搜集搜集网络资源,学习视频什么的,还是手到擒来的...(for循环一小时中) 旁白:然而你没有明白人家的真 ...
- NFS文件共享
NFS文件共享 简介 NFS即网络文件系统(network file system),监听在TCP 2049端口. 服务器需要记住客户端的ip地址以及相应的端口信息,这些信息可以委托给RPC(remo ...
- <bits/stdc++.h>头文件介绍(包含源代码)
注:转自http://blog.csdn.net/charles_dong2/article/details/56909347,同为本人写的,有部分修改. 之前在一个小OJ上刷题时发现有人是这么写的: ...
- 纯css实现select下拉框并排显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...