Advertisement
smatskevich

Seminar6

Jan 12th, 2022
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <tuple>
  8. #include <unordered_map>
  9. #include <unordered_set>
  10. #include <vector>
  11.  
  12. typedef long long ll;
  13. using namespace std;
  14.  
  15. // Калькулятор
  16. int main1() {
  17.   ios::sync_with_stdio(false);
  18.   cin.tie(nullptr);
  19.  
  20.   int n = 0;
  21.   cin >> n;
  22.   // 0 0 в dp[0] и dp[1] уже
  23.   vector<int> dp(n + 1);
  24.   for (int i = 2; i <= n; ++i) {
  25.     int min_count = dp[i - 1];
  26.     if (i % 2 == 0) min_count = min(min_count, dp[i / 2]);
  27.     if (i % 3 == 0) min_count = min(min_count, dp[i / 3]);
  28.     dp[i] = min_count + 1;
  29.   }
  30.   cout << dp[n] << endl;
  31.   return 0;
  32. }
  33.  
  34. // Пути черепашки
  35. int main() {
  36.   ios::sync_with_stdio(false);
  37.   cin.tie(nullptr);
  38.  
  39.   int n = 0, m = 0;
  40.   cin >> n >> m;
  41.   vector<string> p(n);
  42.   for (int i = 0; i < n; ++i) cin >> p[i];
  43.  
  44.   vector<vector<ll>> dp(n, vector<ll>(m));
  45.   dp[0][0] = 1;
  46.   for (int i = 1; i < m && p[0][i] == '0'; ++i) dp[0][i] = 1;
  47.   for (int i = 1; i < n && p[i][0] == '0'; ++i) dp[i][0] = 1;
  48.  
  49.   for (int i = 1; i < n; ++i) {
  50.     for (int j = 1; j < m; ++j) {
  51.       if (p[i][j] == '1') continue;
  52.       // Можно без проверок, так как на месте '1' стоят 0.
  53.       if (p[i - 1][j] == '0') dp[i][j] += dp[i - 1][j];
  54.       if (p[i][j - 1] == '0') dp[i][j] += dp[i][j - 1];
  55.     }
  56.   }
  57.  
  58.   cout << dp[n - 1][m - 1] << endl;
  59.   return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement