Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

int dif_pii(pii &p) { return abs(p.first - p.second); }

struct solve {
  int w, h;

  solve(int w, int h) : w(w), h(h) {}
  ll operator()(vector<vpii> &f) {
    int d_max = 0;
    int x, y;
    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        max_u(d_max, dif_pii(f[y][x]));
      }
    }
    int s_max = d_max * (w + h);
    vector<vector<vector<bool>>> dp(h);
    for (auto &m : dp) {
      m.resize(w);
      for (auto &mm : m) {
        mm.assign(s_max, false);
      }
    }

    dp[0][0][dif_pii(f[0][0])] = true;
    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        int dx = (x + 1 < w) ? dif_pii(f[y][x + 1]) : 0;
        int dy = (y + 1 < h) ? dif_pii(f[y + 1][x]) : 0;
        for (int v = 0; v < s_max; v++) {
          if (!dp[y][x][v])
            continue;
          if (x + 1 < w) {
            if (abs(v + dx) < s_max)
              dp[y][x + 1][abs(v + dx)] = true;
            dp[y][x + 1][abs(v - dx)] = true;
          }
          if (y + 1 < h) {
            if (abs(v + dy) < s_max)
              dp[y + 1][x][abs(v + dy)] = true;
            dp[y + 1][x][abs(v - dy)] = true;
          }
        }
      }
    }

    for (int v = 0; v < s_max; v++) {
      if (dp[h - 1][w - 1][v])
        return v;
    }
    return -1;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int h, w;
    cin >> h >> w;
    if (cin.fail())
      break;

    vector<vpii> f(h);
    for (auto &m : f)
      m.resize(w);

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> f[y][x].first;
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> f[y][x].second;
      }
    }

    cout << solve(w, h)(f) << "\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

int dif_pii(pii &p) { return abs(p.first - p.second); }

struct solve {
  int w, h;

  solve(int w, int h) : w(w), h(h) {}
  ll operator()(vector<vpii> &f) {
    int d_max = 0;
    int x, y;
    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        max_u(d_max, dif_pii(f[y][x]));
      }
    }
    int s_max = d_max * (w + h) + 1;
    vector<vector<vector<bool>>> dp(h);
    for (auto &m : dp) {
      m.resize(w);
      for (auto &mm : m) {
        mm.assign(s_max, false);
      }
    }

    dp[0][0][dif_pii(f[0][0])] = true;
    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        int dx = (x + 1 < w) ? dif_pii(f[y][x + 1]) : 0;
        int dy = (y + 1 < h) ? dif_pii(f[y + 1][x]) : 0;
        for (int v = 0; v < s_max; v++) {
          if (!dp[y][x][v])
            continue;
          if (x + 1 < w) {
            if (abs(v + dx) < s_max)
              dp[y][x + 1][abs(v + dx)] = true;
            dp[y][x + 1][abs(v - dx)] = true;
          }
          if (y + 1 < h) {
            if (abs(v + dy) < s_max)
              dp[y + 1][x][abs(v + dy)] = true;
            dp[y + 1][x][abs(v - dy)] = true;
          }
        }
      }
    }

    for (int v = 0; v < s_max; v++) {
      if (dp[h - 1][w - 1][v])
        return v;
    }
    return -1;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int h, w;
    cin >> h >> w;
    if (cin.fail())
      break;

    vector<vpii> f(h);
    for (auto &m : f)
      m.resize(w);

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> f[y][x].first;
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> f[y][x].second;
      }
    }

    cout << solve(w, h)(f) << "\n";
  }
  return 0;
}