LeetCode_61uniquePaths [Unique Paths]

#pragma warning(disable:4996)

#include <cstdio>
#include <Windows.h>
#include <tchar.h>

/*
	submit time : 3
		1. Time Limit Exceeded
			23, 12
	request :
		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?

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

		Note: m and n will be at most 100.
*/

/*
 * 二逼解法
void uniquePathsRecursively(int& count, int row, int column, int rows, int columns) {
	if (row == rows && column == columns) {
		++count;
		return;
	}

	if (row < rows)
		uniquePathsRecursively(count, row + 1, column, rows, columns);
	if (column < columns)
		uniquePathsRecursively(count, row, column + 1, rows, columns);
}

int uniquePaths(int m, int n) {
	if (m == 0 || n == 0) return 0;
	if (m == 1 || n == 1) return 1;

	int count = 0;
	uniquePathsRecursively(count, 1, 1, m, n);

	return count;
}
*/

/*
	一共要走m-1 + n-1步, 其中向下m-1步,向右n-1步, 所以结果就是C m-1 m+n-2
*/

long long factorial(int k) {
	if (k == 0) return 1;
	long long fact = 1;
	for (int i = 1; i <= k; ++i)
		fact *= i;

	return fact;
}

int c(int bottom, int top) {
	int diff = bottom - top;
	if (top > diff)
		return c(bottom, diff);

	double res = 1.0;
	int i = bottom, j = top;
	while (j) {
		res *= (double)i / j;
		--i;
		--j;
	}

	int result = (int)(res + 0.5);

	return result;
}

int uniquePaths(int m, int n) {
	if (m == 0 || n == 0) return 0;
	if (m == 1 || n == 1) return 1;

	return c(m + n - 2, m - 1);
}

//===================Test==================
void Test1() {
	int m = 23, n = 12;
	printf("%d\n", uniquePaths(m, n));
}

void Test2() {
	int m = 3, n = 7;
	printf("%d\n", uniquePaths(m, n));
}

int _tmain(int argc, _TCHAR* argv[]) {
	Test1();
	Test2();

	system("pause");
	return 0;
}

LeetCode_61uniquePaths [Unique Paths]

时间: 2024-10-13 04:01:51

LeetCode_61uniquePaths [Unique Paths]的相关文章

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m 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

LeetCode --- 62. Unique Paths

题目链接:Unique Paths 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 (ma

62.Unique Paths (法1递归-动态规划法2数学公式)

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. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

[LeetCode]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

LintCode : Unique Paths II

Problem Description: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. Code: public class Solutio

LintCode : Unique Paths

Problem 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

62. 63. Unique Paths 64. Minimum Path Sum

1. 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' i

&lt;LeetCode OJ&gt; 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

Unique Paths

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 t