Home BOJ. Control Robot (2169)
Post
Cancel

BOJ. Control Robot (2169)

[Link] https://www.acmicpc.net/problem/2169


Point:

Have to initialize left_dp[0] to -INF


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <algorithm>
#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
typedef long long ll;
const int INF = 987654321;

int n, m, ans = INF;
int mm[1000][1000];
int left_dp[1000][1000];
int right_dp[1000][1000];

void solve() {
  right_dp[0][0] = mm[0][0];
  left_dp[0][0] = -INF;
  for1(i, m) {
    right_dp[0][i] = right_dp[0][i-1] + mm[0][i];
    left_dp[0][i] = -INF;
  }
  for1(i, n) {
    right_dp[i][0] = max(left_dp[i-1][0], right_dp[i-1][0]) + mm[i][0];
    for1(j, m) {
      right_dp[i][j] = max({ left_dp[i-1][j], right_dp[i-1][j], right_dp[i][j-1] }) + mm[i][j];
    }
    left_dp[i][m-1] = max(left_dp[i-1][m-1], right_dp[i-1][m-1]) + mm[i][m-1];
    for(int j = m - 2; j >= 0; j--) {
      left_dp[i][j] = max({ left_dp[i-1][j], right_dp[i-1][j], left_dp[i][j+1] }) + mm[i][j];
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for0(i, n) {
    for0(j, m) {
      cin >> mm[i][j];
    }
  }
  solve();
  cout << max(left_dp[n-1][m-1], right_dp[n-1][m-1]);
  return 0;
}
This post is licensed under CC BY 4.0 by the author.