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 机器人移动的更多相关文章

  1. UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

    UVA 1600 Patrol Robot   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   ...

  2. 寒假训练——搜索——C - Robot

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  3. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  4. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  5. 用python写一个预警机器人(支持微信和钉钉)

    背景 线上的系统在运行中,发生故障时怎么及时的通过手机通知到相关人员?当然这是个很简单的需求,现有的方法有很多,例如: 如果我们用的云产品,那么一般都会有配套对应的监控预警功能,根据需要配置一下即可, ...

  6. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  7. V-rep学习笔记:机器人模型创建4—定义模型

    完成之前的操作后终于来到最后一步——定义模型,即将之前创建的几何体.关节等元素按层级关系组织成为一个整体. 将最后一个连杆robot_link_dyn6拖放到相应的关节(robot_joint6)下, ...

  8. Robot Framework - 基础关键字 BuiltIn 库(二)

    本篇教程,我们继续接着上篇内容进行讲解,我们本节教程讲解的是Robot Framework 机器人框架中的变量中使用判断.字符串的拼接.Evaluate的用法.调用Python文件.条件分支语句.以及 ...

  9. Robot Framework - 基础关键字 BuiltIn 库(一)

    今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...

随机推荐

  1. iOS开发网络篇—NSURLConnection基本使用(二)

    1.常用的类       NSURL:请求地址 NSURLRequest:一个NSURLRequest对象就代表一个请求,它包含的信息有:      一个NSURL对象      请求方法.请求头.请 ...

  2. 传递给后台的Json数据解析

    后台代码如下: public void ProcessRequest(HttpContext context) { context.Response.ContentType = "appli ...

  3. C# Socket和TCP连接的区别

    网络通信七层参考模型介绍: 物理层: HUB,网线 链路层: MAC,ARP,交换机 网络层:IP,ICMP,IGMP,路由器 传输层: TCP,UDP 会话层: HTTP,SMTP,FTP,POP3 ...

  4. docker常用术语命令

    镜像(Image) vs Dockerfile 这组概念很少会让人产生疑惑,但是这两者的区别非常重要.Docker在镜像(image)中运行你的代码,而不是Dockerfile.Dockerfile是 ...

  5. Kafka原理与java simple producer示例

    brokers和消费者使用zk来获取状态信息和追踪消息坐标. 每一个partition是一个有序的,不可变的消息序列. 只有当partition里面的file置换到磁盘文件以后,才开放给消费者来消费. ...

  6. 动手学习TCP:TCP特殊状态

    前面两篇文章介绍了TCP状态变迁,以及通过实验演示了客户端和服务端的正常状态变迁. 下面就来看看TCP状态变迁过程中的几个特殊状态. SYN_RCVD 在TCP连接建立的过程中,当服务端接收到[SYN ...

  7. JVM 垃圾回收算法

    在说垃圾回收算法之前,先谈谈JVM怎样确定哪些对象是“垃圾”. 1.引用计数器算法: 引用计数器算法是给每个对象设置一个计数器,当有地方引用这个对象的时候,计数器+1,当引用失效的时候,计数器-1,当 ...

  8. WEB安全--渗透笔记

    前言 服务器被攻击是常有的事,自从上一次被注入挂马后最先想到的是安全狗,最新版的安全狗软件可以抵挡大部分的恶意攻击,但是却有很多网站管理员疏于管理,才有了接下来的入侵. 我们的攻击目标近期上线了安全狗 ...

  9. Coax Transformers[转载]

    Coax Transformers How to determine the needed Z for a wanted Quarter Wave Lines tranformation ratio ...

  10. [ubuntu]deb软件源

    虽然ubuntu的中国服务器的速度已经非常不错,但是难免,会有网络不畅的情形,所以修改软件源地址是一个基础的知识点. 修改ubuntu的软件源的方式有多种,一直是通过ubuntu软件中心提供的UI,还 ...