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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章