Advertisement
welleyth

15. Mouse and corns

Dec 27th, 2020
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6. #define mp make_pair
  7. #define pb push_back
  8.  
  9. const int N = 1000;
  10. const int INF = (int)1e18;
  11.  
  12. int dp[N][N];
  13. pair<int,int> parent[N][N];
  14.  
  15. signed main()
  16. {
  17.     ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
  18.     //freopen("input.txt","r",stdin);
  19.     //freopen("output.txt","w",stdout);
  20.  
  21.     int n,m;
  22.     cin >> n >> m;
  23.  
  24.     for(int i=n;i>=1;i--)
  25.     {
  26.         for(int j=1;j<=m;j++)
  27.         {
  28.             cin >> dp[i][j];
  29.         }
  30.     }
  31.  
  32.     for(int i=1;i<=n;i++)
  33.     {
  34.         for(int j=1;j<=m;j++)
  35.         {
  36.             if(i == 1 && j == 1)
  37.                 continue;
  38.             if(i == 1)
  39.             {
  40.                 dp[i][j] += dp[i][j-1];
  41.                 parent[i][j].first = 0;
  42.                 parent[i][j].second = -1;
  43.                 continue;
  44.             }
  45.             if(j == 1)
  46.             {
  47.                 dp[i][j] += dp[i-1][j];
  48.                 parent[i][j].first = -1;
  49.                 parent[i][j].second = 0;
  50.                 continue;
  51.             }
  52.             if(dp[i-1][j] > dp[i][j-1])
  53.             {
  54.                 dp[i][j] += dp[i-1][j];
  55.                 parent[i][j].first = -1;
  56.                 parent[i][j].second = 0;
  57.             }
  58.             else
  59.             {
  60.                 dp[i][j] += dp[i][j-1];
  61.                 parent[i][j].first = 0;
  62.                 parent[i][j].second = -1;
  63.             }
  64.         }
  65.     }
  66.  
  67.     int x,y;
  68.     int dx,dy;
  69.  
  70.     x = n, y = m;
  71.  
  72.     stack<char> st;
  73.  
  74.     while(x != 1 || y != 1)
  75.     {
  76.         dx = parent[x][y].first;
  77.         dy = parent[x][y].second;
  78.         if(dx == -1)
  79.             st.push('F');
  80.         else
  81.             st.push('R');
  82.         x += dx;
  83.         y += dy;
  84.     }
  85.  
  86.     while(!st.empty())
  87.     {
  88.         cout << st.top();
  89.         st.pop();
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement