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/

public class Solution {
public int uniquePaths(int m, int n) {
int[][] paths = new int[m][n];
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(i == 0 && j == 0) {
paths[0][0] = 1;
} else {
paths[i][j] = (i==0 ? 0: paths[i - 1][j]) + (j ==0 ? 0: paths[i][j - 1]);
}
}
}
return paths[m - 1][n - 1];
}
}

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

public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if(obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
return 0;
}
int[][] dp = new int[m][n];
dp[0][0] = 1;
for(int i = 1; i < n; ++i) {
if(obstacleGrid[0][i] == 0) {
dp[0][i] = dp[0][i - 1];
} else {
dp[0][i] = 0;
}
}
for(int i = 1; i < m; ++i) {
if(obstacleGrid[i][0] == 0) {
dp[i][0] = dp[i-1][0];
} else {
dp[i][0] = 0;
}
}
for(int i = 1; i < m; ++i) {
for(int j = 1; j < n; ++j) {
if(obstacleGrid[i][j] == 0) {
dp[i][j] = dp[i-1][j] + dp[i][j - 1];
} else {
dp[i][j] = 0;
}
}
} return dp[m - 1][n - 1];
}
}

最新文章

  1. js之路
  2. IDA在内存中dump出android的Dex文件
  3. HTML 学习笔记(列表)
  4. Mysql的简单使用(三)
  5. Quartz作业调度框架
  6. BLE-NRF51822教程16-BLE地址
  7. 【20160924】GOCVHelper MFC增强算法(3)
  8. HNOI2004宠物收养所(平衡树)
  9. [iOS微博项目 - 1.6] - 自定义TabBar
  10. DEDECMS中,list标签和pagelist标签
  11. Node.js REPL终端
  12. 【录音】Android录音--AudioRecord、MediaRecorder
  13. 深入Java虚拟机——类型装载、连接(转)
  14. 数据库 MySQL基础知识
  15. [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
  16. C#实现物联网系统温度解析的函数(支持补码)
  17. Python 包、模块、函数、变量作用域
  18. 使用select函数改进客户端/服务器端程序
  19. linux用户及用户组操作
  20. Block Towers---cf626c(二分)

热门文章

  1. NOI2016滚粗记
  2. Mongodb命令集合
  3. php 路径
  4. int ,long , long long类型的范围
  5. C/C++: C++位域和内存对齐问题
  6. Linux学习记录
  7. 关于chart.js 设置canvas的宽度为父级元素的宽度的百分百 以及 X轴上面刻度数据太多如何处理
  8. C# 使用AForge调用笔记本摄像头拍照
  9. ProtocolBuffers-3 For Objective C (1)-简单的使用
  10. js 与 jq 的节点添加删除实例