Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can only move in two directions, right and down, but certain cells are 'off limit' 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.

Similar questions in Leetcode:

https://leetcode.com/problems/unique-paths/

  1. public class Solution {
  2. public int uniquePaths(int m, int n) {
  3. int[][] paths = new int[m][n];
  4. for(int i = 0; i < m; ++i) {
  5. for(int j = 0; j < n; ++j) {
  6. if(i == 0 && j == 0) {
  7. paths[0][0] = 1;
  8. } else {
  9. paths[i][j] = (i==0 ? 0: paths[i - 1][j]) + (j ==0 ? 0: paths[i][j - 1]);
  10. }
  11. }
  12. }
  13. return paths[m - 1][n - 1];
  14. }
  15. }

https://leetcode.com/problems/unique-paths-ii/

  1. public class Solution {
  2. public int uniquePathsWithObstacles(int[][] obstacleGrid) {
  3. int m = obstacleGrid.length;
  4. int n = obstacleGrid[0].length;
  5. if(obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
  6. return 0;
  7. }
  8. int[][] dp = new int[m][n];
  9. dp[0][0] = 1;
  10. for(int i = 1; i < n; ++i) {
  11. if(obstacleGrid[0][i] == 0) {
  12. dp[0][i] = dp[0][i - 1];
  13. } else {
  14. dp[0][i] = 0;
  15. }
  16. }
  17. for(int i = 1; i < m; ++i) {
  18. if(obstacleGrid[i][0] == 0) {
  19. dp[i][0] = dp[i-1][0];
  20. } else {
  21. dp[i][0] = 0;
  22. }
  23. }
  24. for(int i = 1; i < m; ++i) {
  25. for(int j = 1; j < n; ++j) {
  26. if(obstacleGrid[i][j] == 0) {
  27. dp[i][j] = dp[i-1][j] + dp[i][j - 1];
  28. } else {
  29. dp[i][j] = 0;
  30. }
  31. }
  32. }
  33.  
  34. return dp[m - 1][n - 1];
  35. }
  36. }

[8.2] Robot in a Grid的更多相关文章

  1. Robots on a grid(DP+bfs())

    链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...

  2. 2017 ACM Jordanian Collegiate Programming Contest

    A. Chrome Tabs 当$n=1$时答案为$0$,当$k=1$或$k=n$时答案为$1$,否则答案为$2$. #include<cstdio> int T,n,k; int mai ...

  3. 【PYTHON】a-start寻路算法

    本文章适合黄金段位的LOL大神,同样更适合出门在外没有导航,就找不到家的孩子. 在英雄联盟之中,当你和你的队友都苦苦修炼到十八级的时候,仍然与敌方阵营不分胜负,就在你刚买好装备已经神装的时候,你看见信 ...

  4. SLAM

    |__all together ship |__SLAM__ |__Graph SLAM__ |__完成约束 |__完成Graph SLAM__ |                          ...

  5. poj1573模拟

    Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...

  6. PRM路径规划算法

    路径规划作为机器人完成各种任务的基础,一直是研究的热点.研究人员提出了许多规划方法:如人工势场法.单元分解法.随机路标图(PRM)法.快速搜索树(RRT)法等.传统的人工势场.单元分解法需要对空间中的 ...

  7. Robot Framework + Selenium2Library环境下,结合Selenium Grid实施分布式自动化测试

    最近一段时间,公司在推行自动化测试流程,本人有幸参与了自定义通用控件的关键字封装和脚本辅助编写.数据驱动管理.测试用例执行管理等一系列工具软件的研发工作,积累了一些经验,在此与大家做一下分享,也算是做 ...

  8. Robot Framework和Selenium 2 Grid集成指南

    1. 环境搭建 A. 所需软件 1. Selenium2Lib 1.0.1 这个特性需要用到Selenium2Lib的最新版本1.0.1,但是这个版本还有一些iframe支持和IE支持的问题需要修改, ...

  9. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

随机推荐

  1. 阿里无线前端性能优化指南 (Pt.1 加载优化)

    前言 阿里无线前端团队在过去一年对所负责业务进行了全面的性能优化.以下是我们根据实际经验总结的优化指南,希望对大家有所帮助. 第一部分仅包括数据加载期优化. 图片控制 对于网页特别是电商类页面来说,图 ...

  2. C++中的vector 用法解析

         一.概述     vector 是C++标准模板库的部分内容,他是一个多功能的,能够操作多种 数据结构和算法 的模板类和函数库.     vector 是一个容器,它能够存放各种类型的对象, ...

  3. bzoj4237 稻草人

    我是萌萌的传送门 题意不难理解吧-- 一开始看到这道题的时候lrd告诉我这题要分治,还给我讲了讲分治要怎么写,好像是CDQ+树状数组来着--(好吧我已经忘了--)然而我第一眼看完题之后的思路是数据结构 ...

  4. hdu1045 DFS

    #include<stdio.h> #include<string.h> int n; int maxx; ][]; ]={,-,,}; ]={,,,-}; ][][];//炮 ...

  5. ffmpeg+x264 Windows MSVC 静态编译

    尝试ubuntu和win下mingw编译版本,但都在Vistual Studio链接时因为依赖 libgcc.a, libmingw.a, libmingwex.a 会与mscrt 有符号冲突. 最后 ...

  6. java性能调优及问题追踪--Btrace的使用

    在生产环境中经常遇到格式各样的问题,如OOM或者莫名其妙的进程死掉.一般情况下是通过修改程序,添加打印日志:然后重新发布程序来完成.然而,这不仅麻烦,而且带来很多不可控的因素.有没有一种方式,在不修改 ...

  7. Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器

    public static void DownFileStream(MemoryStream ms, string fileName) { if (ms !=Stream.Null) { ) { fi ...

  8. MMAP和DIRECT IO区别

    看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的东西,技术人员非常关 ...

  9. AnyImgUpload

    @{ ViewBag.Title = "ImgForAny"; Layout = null; } <h2>ImgForAny</h2> <script ...

  10. 多功能前台交互效果插件superSlide

    平时我们常用的"焦点图/幻灯片""Tab标签切换""图片滚动""无缝滚动"等效果要加载n个插件,又害怕代码冲突又怕不兼容 ...