Advertisement
hidrogenio3

2805.cpp

Feb 26th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.     int n, m;
  7.     cin >> n >> m;
  8.     vector<string> s(n);
  9.     using ll = long long;
  10.     vector<vector<ll>> has, dp;
  11.     vector<pair<int,int>> chng;
  12.     has = dp = vector<vector<ll>>(n, vector<ll>(m, 0));
  13.     for (int i = 0; i < n; ++i){
  14.         s[i].resize(m);
  15.         cin >> s[i];
  16.         for (int j = 0; j < m; ++j){
  17.             if (s[i][j] == '1') chng.push_back({i,j});
  18.             if (i == 0 and j == 0){
  19.                 has[i][j] = s[i][j] == '0';
  20.                 continue;
  21.             }
  22.             if (i == 0 and j){
  23.                 has[i][j] = has[i][j-1] or s[i][j] == '0';
  24.                 continue;
  25.             }
  26.             if (i and j == 0){
  27.                 has[i][j] = has[i-1][j] or s[i][j] == '0';
  28.                 continue;
  29.             }
  30.             has[i][j] = has[i-1][j] or has[i][j-1] or s[i][j] == '0';
  31.         }
  32.     }
  33.     for (auto a : chng){
  34.         for (int i = 0; i <= a.first; ++i){
  35.             for (int j = 0; j <= a.second; ++j){
  36.                 if (s[i][j] == '0'){
  37.                     cout << 0 << '\n';
  38.                     return 0;
  39.                 }
  40.                 s[i][j] = '1';
  41.             }
  42.         }
  43.     }
  44.     // for (auto a: s){
  45.     //     cout << a;
  46.     //     cout << '\n';
  47.     // }
  48.     // cout << "oi" << endl;
  49.     const int mod = 1e9+7;
  50.     dp[0][0] = s[0][0] != '0';
  51.     for (int i = 1; i < m; ++i){
  52.         if (s[0][i] == '1') dp[0][i] = 1;
  53.         else
  54.             dp[0][i] = dp[0][i-1] + !has[0][i];
  55.     }
  56.     for (int i = 1; i < n; ++i) {
  57.         if (s[i][0] == '1') dp[i][0] = 1;
  58.         else dp[i][0] = dp[i-1][0] + !has[i][0];
  59.     }
  60.     for (int i = 1; i < n; ++i){
  61.         for (int j = 1; j < m; ++j){
  62.             if (s[i][j] == '1') dp[i][j] = 1;
  63.             else    dp[i][j] = (dp[i-1][j]*dp[i][j-1] + !has[i][j]) % mod;
  64.         }
  65.     }
  66.    
  67.     // for (auto a: dp){
  68.     //     for (auto b : a){
  69.     //         cout << b;
  70.     //     }
  71.     //     cout << '\n';
  72.     // }
  73.     cout << dp[n-1][m-1] << '\n';
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement