CSES Solutions
#1638Dynamic Programming

Grid Paths I

View on CSES

Solution

1638-Grid_Paths_I.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4constexpr int M=1e9+7;
5signed main(){
6  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
7  int n; cin>>n;
8  string g[n];
9  for(auto&x:g) cin>>x;
10  int dp[n][n]{}; dp[0][0]=(g[0][0]=='.');
11  g[0][0]='s';
12  for(int i=0;i<n;++i) for(int j=0;j<n;++j){
13    if(g[i][j]=='.') dp[i][j]=((i?dp[i-1][j]:0)+(j?dp[i][j-1]:0))%M;
14  }
15  cout<<dp[n-1][n-1]<<'\n';
16  return 0;
17}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.