[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 + 1, 1);
        for (int i = 1; i <= x; ++i) {
            for (int j = 1; j <= y; ++j) {
                dp[j] += dp[j - 1];
            }
        }
        return dp[y];
    }
};

解法二:

class Solution {
public:
    int getPath(int x, int y) {
        double num = 1, denom = 1;
        int small = x < y ? x : y;
        for (int i = 1; i <= small; ++i) {
            num *= x + y - i + 1;
            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 < 0 || y < 0 || grid[x][y] == 1) return false;
        Point *p = new Point(x, y);
        if (m.find(p) != m.end()) return m[p];
        bool isAtOrigin = (x == 0) && (y == 0), success = false;
        if (isAtOrigin || getPathDFS(grid, x, y - 1, m, res) || getPathDFS(grid, x - 1, y, m, res)) {
            res.push_back(p);
            success = true;
        }
        m[p] = success;
        return success;
    }
};

本文转自博客园Grandyang的博客,原文链接:机器人移动[CareerCup] 9.2 Robot Moving,如需转载请自行联系原博主。

相关文章
|
传感器 机器学习/深度学习 Web App开发
AI之Robot:机器人Robot的简介、发展历史、案例应用之详细攻略
AI之Robot:机器人Robot的简介、发展历史、案例应用之详细攻略
|
9月前
|
传感器 XML 数据可视化
[ros robot] --- 机器人系统仿真
[ros robot] --- 机器人系统仿真
310 0
|
10月前
|
机器人 语音技术 Android开发
App Inventor 2 语音交互机器人Robot,使用讯飞语音识别引擎
App Inventor 2 语音识别及交互App。识别语言指令并控制机器人运动,主要用到语音识别器及文本朗读器组件,语音识别相关开发最佳入门。代码逻辑简单,App交互性及趣味性非常强~
160 0
|
机器人
robot(1):关于机器人和互联网的思考
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/47778621 未经博主允许不得转载。 1,开始 最近新闻上讲机器人都非常热,而且这个和创业一样从国家层面都开始重视了。 互联网公司现在啥都做,最近O2O的项目也非常的火。 拼车,专车,大巴啥都有了。几年前难以想象。 最近机器人也开始火起来了,而且:
1193 0
|
2月前
|
传感器 人工智能 监控
智能耕耘机器人
智能耕耘机器人
45 3
|
6月前
|
人工智能 自然语言处理 机器人
智能电话机器人核心技术:自然语言处理
什么是自然语言处理? 自然语言处理是计算机科学领域与人工智能领域中的一个重要方向.它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法.自然语言处理是一门融语言学、计算机科学、数学于一体的科学.因此,这一领域的研究将涉及自然语言,即人们日常使用的语言,所以它与语言学的研究有着密切的联系,但又有重要的区别. 自然语言处理并不是一般地研究自然语言,而在于研制能有效地实现自然语言通信的计算机系统,特别是其中的软件系统.因而它是计算机科学的一部分. 自然语言处理(NLP)是计算机科学,人工智能,语言学关注计算机和人类(自然)语言之间的相互作用的领域.

热门文章

最新文章