1118 机器人走方格

51 Nod 题目链接

题意

从一个矩阵的左上角走到右下角,只能往右走和往下走一步,问有多少种走法。

解析

这是动态规划的经典题,可以使用一维数组优化,同时要在计算的过程中取模。

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int MOD = 1e9+7;
const int N = 1e3+5;
int dp[N];
int main(){
	int m, n;
	cin>>m>>n;
	for(int i = 1; i <= m; i++){
		for(int j = 1; j <= n; j++){
			if(i==1 && j == 1) dp[j] = 1;
			else{
				dp[j] = ( dp[j]+dp[j-1] ) % MOD;
			}	
		}
	}
	cout<<dp[n]<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章