[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 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...
随机推荐
- 用Leangoo做敏捷需求管理-敏捷团队协作
传统的瀑布工作模式使用详细的需求说明书来表达需求,需求人员负责做需求调研,根据调研情况编制详细的需求说明书,进行需求评审,评审之后签字确认交给研发团队设计开发.在这样的环境下,需求文档是信息传递的主体 ...
- 怎么录制Android或IOS动画教程
前一篇文章介绍了用DemoCreator制作Android视频教程,今天再介绍一种方法. 那就是用GifCam软件录制,此软件录制导出成Gif动画图片,可直接放在你的文章里面,效果比flash要好. ...
- SpringMVC4 + Spring + MyBatis3 基于注解的最简配置
本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...
- 无法将匿名方法转换为System.Delegate
在WinForm中,不允许非UI线程访问UI,如果非UI线程需要跨线程调用UI控件,通常的解决办法是使用Control类中的Invoke方法,传递给该方法一个委托和委托调用的参数列表(params [ ...
- Windows7下安装MongoDB(转)
1.下载 地址:http://www.mongodb.org/downloads(32位还是64位自行选择). 我下载的是:mongodb-win32-x86_64-2.4.5.zip 2.解压 把m ...
- 实验:sigsuspend(),sigprocmask()
实验:sigsuspend(),sigprocmask() 源代码: /* * Program: pause_suspend.c * To test the difference between si ...
- Sql 随机更新一条数据返回更新数据的ID编号
DECLARE @parimaryTable(临时表) Table(prizecode varchar(50)); update top (1) 数据表 set 字段a='数值' ,字段b=‘数值 ...
- Hadoop_YARN框架
Hadoop学习笔记总结 01. YARN框架 1. 新一代的框架介绍 YARN的职能就是将资源调度和任务调度分开.资源管理器ResourceManager全局管理所有应用程序计算资源的分配,每一个j ...
- 计算机中的颜色XIV——快速变换颜色的V分量
基本知识回顾: 计算机中的颜色Color,用RGB模式存储(用R.G.B三个分量表示颜色,每个分量的范围是0—255). 而计算机中的颜色除了用RGB模式表示以外,常见的还有HSV模式(或者是HSB. ...
- CentOS7.2安装总结
第一次自己写文章,想想还有点小激动呢.折腾了大半天,终于在一个没用的台式机上面装了个mini版的CentOS7.2.写这篇文章也是做个记载,要是以后再装要注意了. 一.安装过程 采用U盘安装.最初是准 ...