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

#include <iostream>
#include <queue>
#include <vector>

#define INF (1 << 25)

using namespace std;

vector<vector<vector<vector<int>>>> dp;

vector<vector<int>> M;

int H, W, sx1, sy1, sx2, sy2;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int bfs() {
  dp = vector<vector<vector<vector<int>>>>(
      H, vector<vector<vector<int>>>(
             W, vector<vector<int>>(H, vector<int>(W, INF))));
  dp[sy1][sx1][sy2][sx2] = 0;

  queue<pair<pair<pair<int, int>, pair<int, int>>, int>> que;
  que.push(make_pair(make_pair(make_pair(sx1, sy1), make_pair(sx2, sy2)), 0));

  while (!que.empty()) {
    int x1 = que.front().first.first.first;
    int y1 = que.front().first.first.second;
    int x2 = que.front().first.second.first;
    int y2 = que.front().first.second.second;

    int turn = que.front().second;

    que.pop();

    for (int dir = 0; dir < 4; dir++) {
      int tx1 = x1 + dx[dir];
      if (tx1 < 0 || tx1 >= W) {
        tx1 -= dx[dir];
      }
      int ty1 = y1 + dy[dir];
      if (ty1 < 0 || ty1 >= H) {
        ty1 -= dy[dir];
      }
      int tx2 = x2 - dx[dir];
      if (tx2 < 0 || tx2 >= W) {
        tx2 += dx[dir];
      }
      int ty2 = y2 - dy[dir];
      if (ty2 < 0 || ty2 >= H) {
        ty2 += dy[dir];
      }

      if (M[ty1][tx1] == 1) {
        tx1 -= dx[dir];
        ty1 -= dy[dir];
      }
      if (M[ty2][tx2] == 1) {
        tx2 += dx[dir];
        ty2 += dy[dir];
      }

      if (tx1 == tx2 && ty1 == ty2) {
        return turn + 1;
      }

      if (dp[ty1][tx1][ty2][tx2] == INF) {
        dp[ty1][tx1][ty2][tx2] = turn + 1;

        que.push(make_pair(make_pair(make_pair(tx1, ty1), make_pair(tx2, ty2)),
                           turn + 1));
      }
    }
  }

  return -1;
}

int main() {
  while (true) {
    cin >> W >> H;

    if (H == 0 && W == 0) {
      break;
    }

    M = vector<vector<int>>(H, vector<int>(W));

    cin >> sx1 >> sy1 >> sx2 >> sy2;

    sx1--;
    sy1--;
    sx2--;
    sy2--;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> M[i][j];
      }
    }

    int Ans = bfs();

    if (Ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << Ans << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

#define INF (1 << 25)

using namespace std;

vector<vector<vector<vector<int>>>> dp;

vector<vector<int>> M;

int H, W, sx1, sy1, sx2, sy2;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int bfs() {
  dp = vector<vector<vector<vector<int>>>>(
      H, vector<vector<vector<int>>>(
             W, vector<vector<int>>(H, vector<int>(W, INF))));
  dp[sy1][sx1][sy2][sx2] = 0;

  queue<pair<pair<pair<int, int>, pair<int, int>>, int>> que;
  que.push(make_pair(make_pair(make_pair(sx1, sy1), make_pair(sx2, sy2)), 0));

  while (!que.empty()) {
    int x1 = que.front().first.first.first;
    int y1 = que.front().first.first.second;
    int x2 = que.front().first.second.first;
    int y2 = que.front().first.second.second;

    int turn = que.front().second;

    que.pop();

    for (int dir = 0; dir < 4; dir++) {
      int tx1 = x1 + dx[dir];
      if (tx1 < 0 || tx1 > W) {
        tx1 -= dx[dir];
      }
      int ty1 = y1 + dy[dir];
      if (ty1 < 0 || ty1 > H) {
        ty1 -= dy[dir];
      }
      int tx2 = x2 - dx[dir];
      if (tx2 < 0 || tx2 > W) {
        tx2 += dx[dir];
      }
      int ty2 = y2 - dy[dir];
      if (ty2 < 0 || ty2 > H) {
        ty2 += dy[dir];
      }

      if (M[ty1][tx1] == 1) {
        tx1 -= dx[dir];
        ty1 -= dy[dir];
      }
      if (M[ty2][tx2] == 1) {
        tx2 += dx[dir];
        ty2 += dy[dir];
      }

      if (tx1 == tx2 && ty1 == ty2) {
        return turn + 1;
      }

      if (dp[ty1][tx1][ty2][tx2] == INF) {
        dp[ty1][tx1][ty2][tx2] = turn + 1;

        que.push(make_pair(make_pair(make_pair(tx1, ty1), make_pair(tx2, ty2)),
                           turn + 1));
      }
    }
  }

  return -1;
}

int main() {
  while (true) {
    cin >> W >> H;

    if (H == 0 && W == 0) {
      break;
    }

    M = vector<vector<int>>(H, vector<int>(W));

    cin >> sx1 >> sy1 >> sx2 >> sy2;

    sx1--;
    sy1--;
    sx2--;
    sy2--;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> M[i][j];
      }
    }

    int Ans = bfs();

    if (Ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << Ans << endl;
    }
  }

  return 0;
}