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

#include <iostream>
#include <stack>

#define UNDEF -1
#define IS_RD -2
#define INF 1073741824

using namespace std;

int M[1001][1001], H, W;
int dp[1001][1001];
stack<int> callX;
stack<int> callY;

int getM(int x, int y) {
  if (x >= 0 && y >= 0 && x < W && y < H) {
    return M[x][y];
  } else {
    return INF;
  }
}

void calc(int _x, int _y) {
  int x, y;
  callX.push(_x);
  callY.push(_y);
  while (!callX.empty()) {
  endfn:
    x = callX.top();
    y = callY.top();
    if (dp[x][y] == UNDEF) {
      int a[4] = {INF, INF, INF, INF};
      if (getM(x + 1, y) < getM(x, y)) {
        if (dp[x + 1][y] == UNDEF) {
          callX.push(x + 1);
          callY.push(y);
          goto endfn;
        } else {
          a[0] = dp[x + 1][y];
        }
      }
      if (getM(x - 1, y) < getM(x, y)) {
        if (dp[x - 1][y] == UNDEF) {
          callX.push(x - 1);
          callY.push(y);
          goto endfn;
        } else {
          a[1] = dp[x - 1][y];
        }
      }
      if (getM(x, y + 1) < getM(x, y)) {
        if (dp[x][y + 1] == UNDEF) {
          callX.push(x);
          callY.push(y + 1);
          goto endfn;
        } else {
          a[2] = dp[x][y + 1];
        }
      }
      if (getM(x, y - 1) < getM(x, y)) {
        if (dp[x][y - 1] == UNDEF) {
          callX.push(x);
          callY.push(y - 1);
          goto endfn;
        } else {
          a[3] = dp[x][y - 1];
        }
      }
      if (a[0] == INF && a[1] == INF && a[2] == INF && a[3] == INF) {
        dp[x][y] = (x * 1000) + y;
        goto endok;
      } else {
        for (int i = 0; i < 3; i++) {
          for (int ii = i + 1; ii < 4; ii++) {
            if (a[i] != INF && a[ii] != INF && a[i] != a[ii]) {
              dp[x][y] = IS_RD;
              goto endok;
            }
          }
        }
        for (int i = 0; i < 4; i++) {
          if (a[i] != INF) {
            dp[x][y] = a[i];
            goto endok;
          }
        }
      }
    }
  endok:
    callX.pop();
    callY.pop();
  }
}

int main(void) {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int ii = 0; ii < W; ii++) {
      cin >> M[ii][i];
    }
  }
  for (int x = 0; x < 1001; x++) {
    for (int y = 0; y < 1001; y++) {
      dp[x][y] = UNDEF;
    }
  }
  int res = 0;
  for (int x = 0; x < W; x++) {
    for (int y = 0; y < H; y++) {
      calc(x, y);
    }
  }
  for (int x = 0; x < W; x++) {
    for (int y = 0; y < H; y++) {
      if (dp[x][y] == IS_RD)
        res++;
    }
  }

  cout << res << endl;
}

Error: 0

Fixed submission:

#include <iostream>

#define UNDEF -1
#define IS_RD -2
#define INF 1073741824

using namespace std;

int M[1000][1000], H, W;
int dp[1000][1000];

int getM(int x, int y) {
  if (x >= 0 && y >= 0 && x < W && y < H) {
    return M[x][y];
  } else {
    return INF;
  }
}

int dfs(int x, int y) {
  if (dp[x][y] == UNDEF) {
    int a[4] = {INF, INF, INF, INF};
    if (getM(x + 1, y) < getM(x, y)) {
      a[0] = dfs(x + 1, y);
    }
    if (getM(x - 1, y) < getM(x, y)) {
      a[1] = dfs(x - 1, y);
    }
    if (getM(x, y + 1) < getM(x, y)) {
      a[2] = dfs(x, y + 1);
    }
    if (getM(x, y - 1) < getM(x, y)) {
      a[3] = dfs(x, y - 1);
    }
    if (a[0] == INF && a[1] == INF && a[2] == INF && a[3] == INF) {
      dp[x][y] = (x * 1000) + y;
      goto endloop;
    } else {
      for (int i = 0; i < 4; i++) {
        for (int ii = i + 1; ii < 4; ii++) {
          if (a[i] != INF && a[ii] != INF && a[i] != a[ii]) {
            dp[x][y] = IS_RD;
            goto endloop;
          }
        }
      }
      for (int i = 0; i < 4; i++) {
        if (a[i] != INF) {
          dp[x][y] = a[i];
          goto endloop;
        }
      }
    }
  }
endloop:
  return dp[x][y];
}

int main(void) {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int ii = 0; ii < W; ii++) {
      cin >> M[ii][i];
    }
  }
  for (int x = 0; x < 1000; x++) {
    for (int y = 0; y < 1000; y++) {
      dp[x][y] = UNDEF;
    }
  }
  int res = 0;
  for (int x = 0; x < W; x++) {
    for (int y = 0; y < H; y++) {
      if (dfs(x, y) == IS_RD)
        res++;
    }
  }
  cout << res << endl;
}