[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 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...
随机推荐
- Xcode的内存清理
1.删除Xcode中多余的证书provisioning profile 手动删除: 打开finder,然后在最上面的前往中前往下方的路径,就可以看到你xcode运行到现在使用过的证书provision ...
- 阿里云ECS服务器(ubuntu)下基本配置以及升级git
最近需要在阿里云服务器上远程搭建调试环境,这里把遇到的问题做一下记录: 1.ECS Linux解决SSH会话连接超时问题 用SSH客户端(我使用的Xshell)连接linux服务器时,经常会出现与服务 ...
- (转)为什么大公司青睐Java
转自 http://www.zhihu.com/question/25908953/answer/32119971 因为这是一个商业问题,不是技术问题. 我在面试时探讨过这个问题,对方创业期,问我如果 ...
- 未能找到元数据文件“引用的DLL的路径”
使用VS的时候 偶尔会出现错误 [未能找到元数据文件“引用的DLL的路径”] 但是实际上项目中这些DLL都是做了引用的,甚至你前一天打开还是好好的,睡一觉起来 不知道什么原因 就酱紫了 原因:不详 ...
- hibernate一对多映射实现
Junit4方法详解 setUpBeforeClass()类初始化前调用 tearDownAfterClass()类初始化后调用 setUp()在测试方法前调用 tearDown()在测试方法后调用 ...
- Android分步注册,Activity由B返回A修改再前往B,B中已填项不变
某日突然想到标题问题,一般来说返回上一个Activity,当前Activity应该自动销毁.要想保留值,便想到用bundle传递的方式 最后功能是实现了,但感觉方法很笨. 主要代码如下: packag ...
- 分享一个Fluent风格的邮件发送封装类
C#中用SmtpClient发邮件很简单,闲着无事,简单封装一下 IEmailFactory public interface IEmailFactory { IEmailFactory SetHos ...
- HDU 3333 Turing Tree --树状数组+离线处理
题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...
- 纯js和纯css+html制作的手风琴的效果
一:纯css+html的手风琴效果 这种用css写的手风琴比较简单,主要是应用到css中的,transition属性. 代码如下: <!DOCTYPE HTML> <html> ...
- WPF标注装饰器
标注 在许多地方我们都会用到标注,比如在画图中: 在Office中: 在Foxit Reader中: 在Blend中: 等等. 简介 以前,因项目上需要做标注,简单找了一下,没发现适合要求的控件(包括 ...