Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vec>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

int main() {

  ll r, c, k;
  cin >> r >> c >> k;

  ll a[k], b[k], v[k];
  mat G(r + 1, vec(c + 1));
  for (ll i = 0; i < k; i++) {
    cin >> a[i] >> b[i] >> v[i];
    G[a[i]][b[i]] = v[i];
  }

  vector<vector<vector<ll>>> dp(r + 1);
  for (ll i = 0; i < r + 1; i++)
    dp[i].resize(c + 1);
  for (ll i = 0; i < r + 1; i++) {
    for (ll j = 0; j < c + 1; j++)
      dp[i][j].resize(4);
  }
  if (G[1][1] != 0) {
    dp[1][1][1] = G[1][1];
  }

  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      if (j >= 2) {
        if (G[i][j] != 0) {
          dp[i][j][1] =
              max(dp[i][j - 1][1], max(dp[i][j][1], dp[i][j - 1][0] + G[i][j]));
          dp[i][j][2] =
              max(dp[i][j - 1][2], max(dp[i][j][2], dp[i][j - 1][1] + G[i][j]));
          dp[i][j][3] =
              max(max(dp[i][j][3], dp[i][j - 1][2] + G[i][j]), dp[i][j - 1][3]);
        } else {
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
        }
      }
      if (i >= 2) {
        if (G[i][j] != 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + G[i][j]);
        }
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
        /*
        if(j>=2){
        if(G[i][j]!=0){
          dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][0]+G[i][j]);
          dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][1]+G[i][j]);
          dp[i][j][3]=max(max(dp[i][j][3],dp[i][j-1][2]+G[i][j]),dp[i][j-1][3]);
        }
        else{
          dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1]);
          dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][2]);
          dp[i][j][3]=max(dp[i][j][3],dp[i][j-1][3]);
        }
      }
      if(i>=2){
        if(G[i][j]!=0){
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][0]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][1]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][2]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][3]+G[i][j]);
        }
        else{
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][0]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][1]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][2]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][3]);
        }
        */
      }
    }
  }

  ll m = 0;
  for (ll i = 0; i < 4; i++)
    m = max(dp[r][c][i], m);

  cout << m << endl;

  // cout << dp[2][2][1] << endl;

  // for(ll i=1;i<4;i++)cout << dp[1][2][i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vec>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

int main() {

  ll r, c, k;
  cin >> r >> c >> k;

  ll a[k], b[k], v[k];
  mat G(r + 1, vec(c + 1));
  for (ll i = 0; i < k; i++) {
    cin >> a[i] >> b[i] >> v[i];
    G[a[i]][b[i]] = v[i];
  }

  ll dp[r + 1][c + 1][4];
  memset(dp, 0, sizeof(dp));
  if (G[1][1] != 0) {
    dp[1][1][1] = G[1][1];
  }

  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      if (j >= 2) {
        if (G[i][j] != 0) {
          dp[i][j][1] =
              max(dp[i][j - 1][1], max(dp[i][j][1], dp[i][j - 1][0] + G[i][j]));
          dp[i][j][2] =
              max(dp[i][j - 1][2], max(dp[i][j][2], dp[i][j - 1][1] + G[i][j]));
          dp[i][j][3] =
              max(max(dp[i][j][3], dp[i][j - 1][2] + G[i][j]), dp[i][j - 1][3]);
        } else {
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
        }
      }
      if (i >= 2) {
        if (G[i][j] != 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + G[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + G[i][j]);
        }
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
        /*
        if(j>=2){
        if(G[i][j]!=0){
          dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][0]+G[i][j]);
          dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][1]+G[i][j]);
          dp[i][j][3]=max(max(dp[i][j][3],dp[i][j-1][2]+G[i][j]),dp[i][j-1][3]);
        }
        else{
          dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1]);
          dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][2]);
          dp[i][j][3]=max(dp[i][j][3],dp[i][j-1][3]);
        }
      }
      if(i>=2){
        if(G[i][j]!=0){
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][0]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][1]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][2]+G[i][j]);
          dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][3]+G[i][j]);
        }
        else{
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][0]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][1]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][2]);
          dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][3]);
        }
        */
      }
    }
  }

  ll m = 0;
  for (ll i = 0; i < 4; i++)
    m = max(dp[r][c][i], m);

  cout << m << endl;

  // for(ll i=1;i<4;i++)cout << dp[1][2][i] << endl;
}