[8.2] Robot in a Grid
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];
- }
- }
[8.2] Robot in a Grid的更多相关文章
- 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 ...
- 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 ...
- 【PYTHON】a-start寻路算法
本文章适合黄金段位的LOL大神,同样更适合出门在外没有导航,就找不到家的孩子. 在英雄联盟之中,当你和你的队友都苦苦修炼到十八级的时候,仍然与敌方阵营不分胜负,就在你刚买好装备已经神装的时候,你看见信 ...
- SLAM
|__all together ship |__SLAM__ |__Graph SLAM__ |__完成约束 |__完成Graph SLAM__ | ...
- poj1573模拟
Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- PRM路径规划算法
路径规划作为机器人完成各种任务的基础,一直是研究的热点.研究人员提出了许多规划方法:如人工势场法.单元分解法.随机路标图(PRM)法.快速搜索树(RRT)法等.传统的人工势场.单元分解法需要对空间中的 ...
- Robot Framework + Selenium2Library环境下,结合Selenium Grid实施分布式自动化测试
最近一段时间,公司在推行自动化测试流程,本人有幸参与了自定义通用控件的关键字封装和脚本辅助编写.数据驱动管理.测试用例执行管理等一系列工具软件的研发工作,积累了一些经验,在此与大家做一下分享,也算是做 ...
- Robot Framework和Selenium 2 Grid集成指南
1. 环境搭建 A. 所需软件 1. Selenium2Lib 1.0.1 这个特性需要用到Selenium2Lib的最新版本1.0.1,但是这个版本还有一些iframe支持和IE支持的问题需要修改, ...
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
随机推荐
- 阿里无线前端性能优化指南 (Pt.1 加载优化)
前言 阿里无线前端团队在过去一年对所负责业务进行了全面的性能优化.以下是我们根据实际经验总结的优化指南,希望对大家有所帮助. 第一部分仅包括数据加载期优化. 图片控制 对于网页特别是电商类页面来说,图 ...
- C++中的vector 用法解析
一.概述 vector 是C++标准模板库的部分内容,他是一个多功能的,能够操作多种 数据结构和算法 的模板类和函数库. vector 是一个容器,它能够存放各种类型的对象, ...
- bzoj4237 稻草人
我是萌萌的传送门 题意不难理解吧-- 一开始看到这道题的时候lrd告诉我这题要分治,还给我讲了讲分治要怎么写,好像是CDQ+树状数组来着--(好吧我已经忘了--)然而我第一眼看完题之后的思路是数据结构 ...
- hdu1045 DFS
#include<stdio.h> #include<string.h> int n; int maxx; ][]; ]={,-,,}; ]={,,,-}; ][][];//炮 ...
- ffmpeg+x264 Windows MSVC 静态编译
尝试ubuntu和win下mingw编译版本,但都在Vistual Studio链接时因为依赖 libgcc.a, libmingw.a, libmingwex.a 会与mscrt 有符号冲突. 最后 ...
- java性能调优及问题追踪--Btrace的使用
在生产环境中经常遇到格式各样的问题,如OOM或者莫名其妙的进程死掉.一般情况下是通过修改程序,添加打印日志:然后重新发布程序来完成.然而,这不仅麻烦,而且带来很多不可控的因素.有没有一种方式,在不修改 ...
- Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器
public static void DownFileStream(MemoryStream ms, string fileName) { if (ms !=Stream.Null) { ) { fi ...
- MMAP和DIRECT IO区别
看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的东西,技术人员非常关 ...
- AnyImgUpload
@{ ViewBag.Title = "ImgForAny"; Layout = null; } <h2>ImgForAny</h2> <script ...
- 多功能前台交互效果插件superSlide
平时我们常用的"焦点图/幻灯片""Tab标签切换""图片滚动""无缝滚动"等效果要加载n个插件,又害怕代码冲突又怕不兼容 ...