Advertisement
tien_noob

Đếm đường đi trên ma trận (GRID1) - DP

Feb 16th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <cmath>
  6. using namespace std;
  7. const int N = 1e3, mod = 1e9 + 7;
  8. char a[N+1][N+1];
  9. int dp[N+1][N+1], h, w;
  10. void read()
  11. {
  12.    cin >> h >> w;
  13.    for (int i = 1; i <= h; ++ i)
  14.    {
  15.        for (int j = 1; j <= w; ++ j)
  16.        {
  17.            cin >> a[i][j];
  18.        }
  19.    }
  20. }
  21. void solve()
  22. {
  23.     dp[0][1] = 1;
  24.     for (int i = 1; i <= h; ++ i)
  25.     {
  26.         for (int j = 1; j <= w; ++ j)
  27.         {
  28.             if (a[i][j] == '#')
  29.             {
  30.                 dp[i][j] = 0;
  31.             }
  32.             else
  33.             {
  34.                 dp[i][j] = (dp[i][j-1] + dp[i-1][j]) % mod;
  35.             }
  36.         }
  37.     }
  38.     cout << dp[h][w];
  39. }
  40. int main()
  41. {
  42.     ios_base::sync_with_stdio(false);
  43.     cin.tie(nullptr);
  44.     read();
  45.     solve();
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement