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

#include <iostream>
#include <queue>

using namespace std;
#define MAX 64

typedef pair<int, int> pi;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

bool isinside(pi pos, int H, int W) {
  return 0 <= pos.first && pos.first < H && 0 <= pos.second && pos.second < W;
}
class State {
public:
  pi taka;
  pi kazu;
  int time;
  State(pi taka, pi kazu, int t) : taka(taka), kazu(kazu), time(t) {}
};

int bfs(int sti, int stj, int ski, int skj, int H, int W, bool M[][MAX]) {
  static bool vis[50][50][50][50];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < H; ++k) {
        for (int l = 0; l < W; ++l) {
          vis[i][j][k][l] = false;
        }
      }
    }
  }

  State st(pi(sti, stj), pi(ski, skj), 0);
  queue<State> qs;
  qs.push(st);
  vis[sti][stj][ski][skj] = true;
  while (!qs.empty()) {
    State now = qs.front();
    qs.pop();
    // cout << now.taka.first << ' ' << now.taka.second << ' ' << now.kazu.first
    // << ' ' << now.kazu.second << endl;
    if (now.taka.first == now.kazu.first &&
        now.taka.second == now.kazu.second) {
      return now.time;
    }
    for (int k = 0; k < 4; ++k) {
      pi ntaka(now.taka.first + di[k], now.taka.second + dj[k]);
      pi nkazu(now.kazu.first + di[(k + 2) % 4],
               now.kazu.second + dj[(k + 2) % 4]);

      if (!isinside(ntaka, H, W)) {
        ntaka = now.taka;
      } else {
        if (M[ntaka.first][ntaka.second])
          ntaka = now.taka;
      }

      if (!isinside(nkazu, H, W)) {
        nkazu = now.kazu;
      } else {
        if (M[nkazu.first][nkazu.second])
          nkazu = now.kazu;
      }

      if (!vis[ntaka.first][ntaka.second][nkazu.first][nkazu.second]) {
        vis[ntaka.first][ntaka.second][nkazu.first][nkazu.second] = true;

        State next(ntaka, nkazu, now.time + 1);
        qs.push(next);
      }
    }
  }
  return -1;
}

int main() {
  while (true) {
    int H, W;

    cin >> H >> W;
    swap(H, W);
    if (H == 0 && W == 0)
      break;
    bool m[MAX][MAX];
    int tx, ty;
    int kx, ky;
    cin >> tx >> ty;
    cin >> kx >> ky;
    --tx;
    --ty;
    --kx;
    --ky;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        int t;
        cin >> t;
        if (t == 1)
          m[i][j] = true;
        else
          m[i][j] = false;
      }
    }
    int ans = bfs(ty, tx, ky, kx, H, W, m);
    if (ans < 0) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;
#define MAX 50

typedef pair<int, int> pi;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

bool isinside(pi pos, int H, int W) {
  return 0 <= pos.first && pos.first < H && 0 <= pos.second && pos.second < W;
}
class State {
public:
  pi taka;
  pi kazu;
  int time;
  State(pi taka, pi kazu, int t) : taka(taka), kazu(kazu), time(t) {}
};

int bfs(int sti, int stj, int ski, int skj, int H, int W, bool M[][MAX]) {
  bool vis[H][W][H][W];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < H; ++k) {
        for (int l = 0; l < W; ++l) {
          vis[i][j][k][l] = false;
        }
      }
    }
  }

  State st(pi(sti, stj), pi(ski, skj), 0);
  queue<State> qs;
  qs.push(st);
  while (!qs.empty()) {
    State now = qs.front();
    qs.pop();
    // cout << now.taka.first << ' ' << now.taka.second << ' ' << now.kazu.first
    // << ' ' << now.kazu.second << endl;
    if (now.taka.first == now.kazu.first &&
        now.taka.second == now.kazu.second) {
      return now.time;
    }
    for (int k = 0; k < 4; ++k) {
      pi ntaka(now.taka.first + di[k], now.taka.second + dj[k]);
      pi nkazu(now.kazu.first + di[(k + 2) % 4],
               now.kazu.second + dj[(k + 2) % 4]);
      if (!isinside(ntaka, H, W)) {
        ntaka = now.taka;
      } else {
        if (M[ntaka.first][ntaka.second])
          ntaka = now.taka;
      }
      if (!isinside(nkazu, H, W)) {
        nkazu = now.kazu;
      } else {
        if (M[nkazu.first][nkazu.second])
          nkazu = now.kazu;
      }
      if (!vis[ntaka.first][ntaka.second][nkazu.first][nkazu.second]) {
        vis[ntaka.first][ntaka.second][nkazu.first][nkazu.second] = true;

        State next(ntaka, nkazu, now.time + 1);
        qs.push(next);
      }
    }
  }
  return -1;
}

int main() {
  while (true) {
    int H, W;

    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    bool m[MAX][MAX];
    int tx, ty;
    int kx, ky;
    cin >> tx >> ty;
    cin >> kx >> ky;
    --tx;
    --ty;
    --kx;
    --ky;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> m[i][j];
      }
    }
    int ans = bfs(ty, tx, ky, kx, H, W, m);
    if (ans < 0) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}