您的位置:首页 > 产品设计 > UI/UE

Unique Paths

2015-06-14 10:17 489 查看
Description:

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?



Code:

int uniquePaths(int m, int n) {
//空间复杂度:O(MN)
assert(m<=100 && n<=100);

int path[MAX+1][MAX+1] = {0};//有效数字下标从1开始,便于阅读,因此这里加1
for (int i = 1; i <= m; ++i)
{
path[i][1] = 1;
}
for (int i = 1; i <= n; ++i)
{
path[1][i] = 1;
}

for (int i = 2; i <= m; ++i)
{//因为m或n为1时的路径数都为1,所以不用再考虑
for (int j = 2; j <= n; ++j)
{
path[i][j] = path[i-1][j] + path[i][j-1];
}
}
return path[m]
;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: