题目地址:

https://www.lintcode.com/problem/pick-carrots/description

给定一个m×nm\times nm×n的矩阵AAA,保证每个位置的数都是正的,其代表该位置的胡萝卜数量。从矩阵中心点出发(中心点的意思是正中央的行与列;如果行数列数是偶数,则取偏左或者偏上的),每次都朝四个方向中胡萝卜最多的方向走一步,如果四个方向都没有胡萝卜,则停止。问最后总共采了多少个胡萝卜。

代码如下:

public class Solution {/*** @param carrot: an integer matrix* @return: Return the number of steps that can be moved.*/public int PickCarrots(int[][] carrot) {// write your code hereint m = carrot.length, n = carrot[0].length;int x = (m - 1) >> 1, y = (n - 1) >> 1;int res = carrot[x][y], dir = 0;int[] d = {1, 0, -1, 0, 1};while ((dir = getDir(x, y, carrot, d)) != -1) {carrot[x][y] = 0;x += d[dir];y += d[dir + 1];res += carrot[x][y];}return res;}// 判断下一步走哪个方向private int getDir(int x, int y, int[][] carrot, int[] d) {// max记录最多的胡萝卜数,maxDir记录最多胡萝卜的方向int max = 0, maxDir = -1;for (int i = 0; i < 4; i++) {int nextX = x + d[i], nextY = y + d[i + 1];if (inBound(nextX, nextY, carrot)) {if (carrot[nextX][nextY] > max) {max = carrot[nextX][nextY];maxDir = i;}}}// 如果四周都是0,则返回-1,不用走了;否则返回方向return max == 0 ? -1 : maxDir;}private boolean inBound(int x, int y, int[][] carrot) {return 0 <= x && x < carrot.length && 0 <= y && y < carrot[0].length;}
}

时间复杂度O(mn)O(mn)O(mn),空间O(1)O(1)O(1)。

【Lintcode】1896. Pick Carrots相关推荐

  1. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  2. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  3. 【Lintcode】1413. Tree

    题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...

  4. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  5. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  6. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

  7. 【Lintcode】1799. Sequence Summation

    题目地址: https://www.lintcode.com/problem/sequence-summation/description 给定三个数i,j,ki,j,ki,j,k,计算i+(i+1) ...

  8. 【Lintcode】1367. Police Distance

    题目地址: https://www.lintcode.com/problem/1367/ 给定一个m×nm\times nm×n的矩阵,只含1,0,−11,0,-11,0,−1,111表示警察,000 ...

  9. 【Lintcode】444. Graph Valid Tree II

    题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...

  10. 【Lintcode】1718. Minimize Malware Spread

    题目地址: https://www.lintcode.com/problem/minimize-malware-spread/description 给定一个nnn个节点的无向图,以邻接矩阵给出.每个 ...

最新文章

  1. .Net 2.0 新功能:迭代器(Iterators)
  2. 如何有效地连接字符串
  3. java中怎样避免方法被重写
  4. Oracle 11g Dataguard 物理备库配置(四)之broker snapshot standby测试
  5. cypress 的错误消息 - the element has become detached or removed from the dom
  6. 医学图像处理期末复习(二)
  7. 全球最神秘的9大葡萄园...
  8. [XSY] 绿色(圆方树、树形DP、树上差分)
  9. Java常见GC算法_垃圾收集器及内存分配_G1垃圾收集器
  10. hive The specified datastore driver (“com.mysql.jdbc.Driver“) was not found
  11. java对字符串快查找_字符串快速查找 - Trie算法
  12. java page 分页_自己整理写的java分页工具条pageUtil
  13. golang 微信小程序获取二维码scene参数报错 invalid scene rid: f05f96ab-5382f139-14b13d2f
  14. (1)快速了解Redis
  15. win10系统做T3服务器,win10系统安装用友t3的图文教程
  16. 手游方舟重启维护服务器要多久,方舟生存进化手游日常维护多久
  17. 信息学奥赛一本通1003:对齐输出
  18. Face Attention Network: An Effective Face Detector for the Occluded Faces
  19. LeetCode(172) Factorial Trailing Zeroes
  20. Doxygen 配置详解

热门文章

  1. 虚拟机实现远程桌面连接
  2. ASO优化之新人必备的基础知识技能
  3. PolSARpro v6.0之Sentinel-1A Wishart与SVM监督分类
  4. qt实现涂鸦板_Qt之旅--- 10 涂鸦板
  5. 基于单片机的GSM短信家庭防盗报警系统
  6. Chang-Xiao Li Machine Learning 2019 Task5
  7. 更新python pip 时提示操作超时错误
  8. 计算机文件夹添加密码,电脑如何给文件夹设置密码
  9. 2022最新QQ协议登陆,QQ密码加密源代码
  10. 【USACO19FEB】Mowing Mischief P(决策单调性)(线段树辅助分治)