[CareerCup] 9.2 Robot Moving 机器人移动
9.2 Imagine a robot sitting on the upper left corner of an X by Y grid. The robot can only move in two directions: right and down. How many possible paths are there for the robot to go from (0,0) to (X,Y)?
FOLLOW UP
Imagine certain spots are "off limits," such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.
LeetCode上的原题,请参见我之前的博客Unique Paths 不同的路径和Unique Paths II 不同的路径之二。
解法一:
class Solution {
public:
int getPath(int x, int y) {
vector<int> dp(y + , );
for (int i = ; i <= x; ++i) {
for (int j = ; j <= y; ++j) {
dp[j] += dp[j - ];
}
}
return dp[y];
}
};
解法二:
class Solution {
public:
int getPath(int x, int y) {
double num = , denom = ;
int small = x < y ? x : y;
for (int i = ; i <= small; ++i) {
num *= x + y - i + ;
denom *= i;
}
return (int)(num / denom);
}
};
这道题的Follow up说格子中可能有障碍物,即不能到达的位子,让我们找到一条从左上点到右下点的路径,只需一条而已,不用将所有的都找出来,那么我们使用递归来做,我们反方向走,从右下点往左上点找,我们用哈希表来记录某个位置是否访问过,当递归到原点的时候,我们把原点加入结果中,然后逐层递归返回,把路径中的点依次加入结果中,这样结果中保存的顺序就是正确的,参见代码如下:
class Point {
public:
int _x, _y;
Point(int x, int y): _x(x), _y(y) {}
}; class Solution {
public:
vector<Point*> getPath(vector<vector<int> > &grid, int x, int y) {
vector<Point*> res;
unordered_map<Point*, bool> m;
getPathDFS(grid, x, y, m, res);
return res;
}
bool getPathDFS(vector<vector<int> > &grid, int x, int y, unordered_map<Point*, bool> &m, vector<Point*> &res) {
if (x < || y < || grid[x][y] == ) return false;
Point *p = new Point(x, y);
if (m.find(p) != m.end()) return m[p];
bool isAtOrigin = (x == ) && (y == ), success = false;
if (isAtOrigin || getPathDFS(grid, x, y - , m, res) || getPathDFS(grid, x - , y, m, res)) {
res.push_back(p);
success = true;
}
m[p] = success;
return success;
}
};
[CareerCup] 9.2 Robot Moving 机器人移动的更多相关文章
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- 寒假训练——搜索——C - Robot
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- Robot POJ - 1376
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- 用python写一个预警机器人(支持微信和钉钉)
背景 线上的系统在运行中,发生故障时怎么及时的通过手机通知到相关人员?当然这是个很简单的需求,现有的方法有很多,例如: 如果我们用的云产品,那么一般都会有配套对应的监控预警功能,根据需要配置一下即可, ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- V-rep学习笔记:机器人模型创建4—定义模型
完成之前的操作后终于来到最后一步——定义模型,即将之前创建的几何体.关节等元素按层级关系组织成为一个整体. 将最后一个连杆robot_link_dyn6拖放到相应的关节(robot_joint6)下, ...
- Robot Framework - 基础关键字 BuiltIn 库(二)
本篇教程,我们继续接着上篇内容进行讲解,我们本节教程讲解的是Robot Framework 机器人框架中的变量中使用判断.字符串的拼接.Evaluate的用法.调用Python文件.条件分支语句.以及 ...
- Robot Framework - 基础关键字 BuiltIn 库(一)
今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...
随机推荐
- myeclipse2013 安装 egit
myeclipse2013版本: Version: 2013 Build id: 11.0-20130401 手工安装不了,那就到市场上安装. 1.Help--->Install ...
- Monyer.cn黑客小游戏
花了一天的时间,Monyer给大家带来了一个有趣的东东——拥有15个关卡的黑客小游戏. 入口http://monyer.com/game/game1 因为一直以来都是大家跟我一起学习网络技术嘛,所以这 ...
- C++基础——子类转父类转子类 (派生类转基类转派生类)
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...
- 一个关于group by和having子句的小例子
表结构: 要求: 查询有多个员工的工资不低于2000的部门编号(就是说如果一个部门的员工大于2000的人数有两个或两个以上就查询出来) sql语句: select [DEPARTMENT_ID],co ...
- 编译流程,C开发常见文件类型名
编译流程 我们常说的编译是一个整体的概念,是指从源程序到可执行程序的整个过程,实际上,C语言编译的过程可以进一步细分为预编译->编译->汇编->链接 预编译是把include关键字所 ...
- 基础总结篇之一:Activity生命周期
子曰:溫故而知新,可以為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就完全掌握,那基本不大可能,所以我们需要经常回过头再仔细研读几遍,以领悟到作者的思想精 ...
- centos下yum搭建安装linux+apache+mysql+php环境
一.脚本YUM源安装: 1.yum install wget #安装下载工具wget 2.wge ...
- HTML5客户端数据存储
HTML5 使在不影响网站性能的情况下存储大量数据成为可能.之前,这些都是由 cookie 完成的,cookie不适合大量数据的存储,因为会影响速度. 举个例子: var obj = {x:1}; / ...
- 技巧:利用 Workflow 显示附近的免费 Wi-Fi
得益于 Workflow 自 1.5.3 版本起更新的 Get Content of URL 动作,该 App 的潜力得到了极大的提升.本文分享一种有趣的用法,搜寻附近的免费 Wi-Fi 并择一显示在 ...
- 去哪儿网2017校招在线笔试(前端工程师)编程题及JavaScript代码
编程题很简单.整个试卷结构为: 一.问答题: 对前端的理解,了解哪些框架库? 二.在线编程题:身份证分组 如下第一道:身份证分组 三.在线编程题:身份证分组.统计字符.酒店价格(三选二) 如下第二三四 ...