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

#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;

int H, W;
char map[55][55];
int memo[55][55][55][55];
vector<P> vec[26];

int calc(int sx, int sy, int tx, int ty) {
  if (memo[sx][sy][tx][ty] != -2)
    return memo[sx][sy][tx][ty];
  if (sx > tx || sy > ty)
    return -1;
  if (map[sx][sy] == '#' || map[tx][ty] == '#')
    return -1;
  if (sx == tx && sy == ty)
    return 0;

  int ret = -1;
  if (map[sx][sy] >= 'a' && map[sx][sy] <= 'z') {
    int idx = map[sx][sy] - 'a';
    for (int i = 0; i < vec[idx].size(); i++) {
      int tmp = -1, tmp2 = -1, nx = vec[idx][i].first, ny = vec[idx][i].second;
      if ((sx == nx && sy + 1 == ny) || (sx + 1 == nx && sy == ny))
        tmp = 0;
      else {
        if (sx < W && nx > 1)
          tmp = max(tmp, calc(sx + 1, sy, nx - 1, ny));
        if (sy < H && nx > 1)
          tmp = max(tmp, calc(sx, sy + 1, nx - 1, ny));
        if (sx < W && ny > 1)
          tmp = max(tmp, calc(sx + 1, sy, nx, ny - 1));
        if (sy < H && ny > 1)
          tmp = max(tmp, calc(sx, sy + 1, nx, ny - 1));
        if (tmp == -1)
          continue;
      }
      if (nx == tx && ny == ty)
        tmp2 = 0;
      else {
        if (nx < W)
          tmp2 = max(tmp2, calc(nx + 1, ny, tx, ty));
        if (ny < H)
          tmp2 = max(tmp2, calc(nx, ny + 1, tx, ty));
        if (tmp2 == -1)
          continue;
      }
      ret = max(ret, tmp + tmp2 + 1);
    }
  }
  if (sx < W)
    ret = max(ret, calc(sx + 1, sy, tx, ty));
  if (sy < H)
    ret = max(ret, calc(sx, sy + 1, tx, ty));

  return memo[sx][sy][tx][ty] = ret;
}

int main(void) {
  while (1) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    for (int i = 0; i < 26; i++)
      vec[i].clear();
    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> map[x][y];
        if (map[x][y] >= 'A' && map[x][y] <= 'Z') {
          vec[map[x][y] - 'A'].push_back(make_pair(x, y));
        }
      }
    }

    for (int sx = 1; sx <= W; sx++) {
      for (int sy = 1; sy <= H; sy++) {
        for (int tx = 1; tx <= W; tx++) {
          for (int ty = 1; ty <= H; ty++) {
            memo[sx][sy][tx][ty] = -2;
          }
        }
      }
    }
    cout << calc(1, 1, W, H) << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;

int H, W;
char map[55][55];
int memo[55][55][55][55];
vector<P> vec[26];

int calc(int sx, int sy, int tx, int ty) {
  if (memo[sx][sy][tx][ty] != -2)
    return memo[sx][sy][tx][ty];
  if (sx > tx || sy > ty)
    return -1;
  if (sx == tx && sy == ty)
    return 0;

  int ret = -1, res, res2;
  if (sx < W && map[sx + 1][sy] != '#') {
    ret = max(ret, calc(sx + 1, sy, tx, ty));
    if (map[sx + 1][sy] >= 'a' || map[sx + 1][sy] <= 'z') {
      int idx = map[sx + 1][sy] - 'a';
      for (int i = 0; i < vec[idx].size(); i++) {
        res = calc(sx + 1, sy, vec[idx][i].first, vec[idx][i].second);
        if (res == -1)
          continue;
        res2 = calc(vec[idx][i].first, vec[idx][i].second, tx, ty);
        if (res2 == -1)
          continue;
        ret = max(ret, res + res2);
      }
    }
  }
  if (sy < H && map[sx][sy + 1] != '#') {
    ret = max(ret, calc(sx, sy + 1, tx, ty));
    if (map[sx][sy + 1] >= 'a' || map[sx][sy + 1] <= 'z') {
      int idx = map[sx][sy + 1] - 'a';
      for (int i = 0; i < vec[idx].size(); i++) {
        res = calc(sx, sy + 1, vec[idx][i].first, vec[idx][i].second);
        if (res == -1)
          continue;
        res2 = calc(vec[idx][i].first, vec[idx][i].second, tx, ty);
        if (res2 == -1)
          continue;
        ret = max(ret, res + res2);
      }
    }
  }

  if (map[sx][sy] - 'a' + 'A' == map[tx][ty] && ret != -1)
    ret++;
  return memo[sx][sy][tx][ty] = ret;
}

int main(void) {
  int cnt = 0;
  while (1) {
    if (++cnt > 4)
      return 0;
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    for (int i = 0; i < 26; i++)
      vec[i].clear();
    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> map[x][y];
        if (map[x][y] >= 'A' && map[x][y] <= 'Z') {
          vec[map[x][y] - 'A'].push_back(make_pair(x, y));
        }
      }
    }
    map[0][1] = '.';
    map[0][2] = '#';

    for (int sx = 0; sx <= W; sx++) {
      for (int sy = 1; sy <= H; sy++) {
        for (int tx = 1; tx <= W; tx++) {
          for (int ty = 1; ty <= H; ty++) {
            memo[sx][sy][tx][ty] = -2;
          }
        }
      }
    }
    cout << calc(0, 1, W, H) << endl;
  }

  return 0;
}