Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 105;
- int n, m;
- int a[N][N], dp[N][N];
- int main(){
- cin >> n >> m;
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- cin >> a[i][j];
- }
- }
- dp[n][1] = a[n][1];
- for (int i = n; i >= 1; i--) {
- for (int j = 1; j <= m; j++) {
- if (i == n && j == 1) continue;
- int first_value = 0, second_value = 0;
- if (i + 1 <= n) {
- first_value = dp[i + 1][j];
- }
- if (j - 1 >= 1) {
- second_value = dp[i][j - 1];
- }
- dp[i][j] = max(first_value, second_value) + a[i][j];
- }
- }
- string path;
- int x = 1, y = m;
- while (x != n || y != 1) {
- // cout << x << " " << y << endl;
- int first_value = 0, second_value = 0;
- if (x + 1 <= n) {
- first_value = dp[x + 1][y];
- }
- if (y - 1 >= 1) {
- second_value = dp[x][y - 1];
- }
- if (first_value >= second_value && x + 1 <= n) {
- x++;
- path += 'F';
- } else {
- y--;
- path += 'R';
- }
- }
- reverse(path.begin(), path.end());
- cout << path << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment