Leetcode-unique-paths

来源:互联网 发布:mac qq代理服务器 编辑:程序博客网 时间:2024/06/01 22:45

题目描述

 

A robotis located at the top-left corner of a m x n grid (marked 'Start' in the diagrambelow).

The robotcan only move either down or right at any point in time. The robot is trying toreach the bottom-right corner of the grid (marked 'Finish' in the diagrambelow).

How many possibleunique paths are there?

Above isa 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

这个题目是最典型的动态规划了,经过前一段时期的训练,这样的题目终于是会做了,但是在坐标index的操作上需要多加注意,就是从(1,1)出发到(m,n),或者(0,0)出发到(n-1,m-1),两种表示方法都可以,需要注意的就是index怎么操作。

public class Solution {    public int uniquePaths(int m, int n) {        int[][] dp =new int[m][n];        dp[0][0] = 1;        for(int i=1;i<m; i++){               dp[i][0] = 1;        }        for(int j=1;j<n; j++){               dp[0][j] = 1;        }        for(int i=1;i<m; i++){            for(int j=1; j<n; j++){               dp[i][j] =dp[i-1][j]+dp[i][j-1];        }        }        return dp[m-1][n-1];    }}


我在想的是,如果题目中给出一些限制条件,比如说地图中有一些地方是封闭的,不可以经过,如(2,2)这一点无法通过,求在这种情况下的方案个数。我需要thinkthink。

0 0