Dynamic Programming【DP】2
问题 F: Grid Paths
题目描述
Consider an n * n grid whose squares may have traps. It is not allowed to move to a square with a trap.
Your task is to calculate the number of paths from the upper-left square to the lower-right square. You can only move right or down.输入
The first input line has an integer n: the size of the grid.
After this, there are n lines that describe the grid. Each line has n characters: . denotes an empty cell, and * denotes a trap.
Constraints
1 ≤ n ≤ 1000输出
Print the number of paths modulo 109+7.
样例输入
复制
4 .... .*.. ...* *...样例输出
复制
3
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1010;
const int mod=1e9+7;int n,k[1010][1010];
vector<vector<int>>dp(N,vector<int>(N,0));
char x;int main()
{cin>>n;for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){cin>>x;if(x=='.')k[i][j]=1;else k[i][j]=0;}}if(k[1][1])dp[1][1]=1;for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){if(k[i][j]){dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i][j-1])%mod;}}}cout<<dp[n][n];return 0;
}