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

#include <iostream>
#include <queue>
using namespace std;
struct xy {
  int tx, ty;
  int kx, ky;
  int ti;
};
int x, y;
xy tk;
int m[51][51], v[51][51][51][51] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

bool g(int a, int b) {
  if (a < 1 || a > x || b < 1 || b > y)
    return 0;
  return 1;
}

int bfs() {
  queue<xy> q;
  xy t, tmp;
  int ta, tb, ka, kb, ans = 10000, tim, cnt = 0;
  q.push(tk);
  while (!q.empty()) {
    t = q.front(), q.pop();
    for (int i = 0; i < 4; i++) {
      ta = t.tx + dx[i], tb = t.ty + dy[i], ka = t.kx - dx[i],
      kb = t.ky - dy[i], tim = t.ti + 1;
      if (g(ta, tb) || g(ka, kb)) {
        cnt++;
        if (!g(ta, tb))
          ta = t.tx, tb = t.ty;
        if (!g(ka, kb))
          ka = t.kx, kb = t.ky;

        if (m[ta][tb] == 1)
          tmp.tx = t.tx, tmp.ty = t.ty;
        else
          tmp.tx = ta, tmp.ty = tb;

        if (m[ka][kb] == 1)
          tmp.kx = t.kx, tmp.ky = t.ky;
        else
          tmp.kx = ka, tmp.ky = kb;

        if (v[tmp.tx][tmp.ty][tmp.kx][tmp.ky] == 1)
          continue;
        if (m[ka][kb] == 1 && m[ta][tb] == 1)
          continue;
        tmp.ti = tim;
        v[tmp.tx][tmp.ty][tmp.kx][tmp.ky] = 1;
        if (tmp.tx == tmp.kx && tmp.ty == tmp.ky) {
          if (ans > tmp.ti)
            ans = tmp.ti;
          continue;
        }

        if (tmp.ti >= 100)
          break;
        q.push(tmp);
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    int ans;
    cin >> x >> y;
    if (x + y == 0)
      break;
    cin >> tk.tx >> tk.ty >> tk.kx >> tk.ky;
    for (int i = 1; i <= x; i++)
      for (int j = 1; j <= y; j++)
        for (int k = 1; k <= x; k++)
          for (int l = 1; l <= y; l++)
            v[i][j][k][l] = 0;
    v[tk.tx][tk.ty][tk.kx][tk.ky] = 1;
    tk.ti = 0;
    for (int i = 1; i <= y; i++)
      for (int j = 1; j <= x; j++)
        cin >> m[j][i];

    ans = bfs();
    if (ans < 100)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
struct xy {
  int tx, ty;
  int kx, ky;
  int ti;
};
int x, y;
xy tk;
int m[51][51], vt[51][51] = {}, vk[51][51] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

bool g(int a, int b) {
  if (a <= 0 || a > x || b <= 0 || b > y)
    return 0;
  return 1;
}

int bfs() {
  queue<xy> q;
  xy t, tmp;
  int ta, tb, ka, kb, ans = 10000, tim;
  q.push(tk);
  while (!q.empty()) {
    t = q.front(), q.pop();
    for (int i = 0; i < 4; i++) {
      ta = t.tx + dx[i], tb = t.ty + dy[i], ka = t.kx - dx[i],
      kb = t.ky - dy[i], tim = t.ti + 1;
      if (g(ta, tb) || g(ka, kb)) {
        if (vt[ta][tb] == 2 && vt[ta][tb] == 2)
          continue;
        if (m[ta][tb] == 1 && m[ka][kb] == 1)
          continue;
        if (m[ta][tb] == 1)
          tmp.tx = t.tx, tmp.ty = t.ty;
        else if (vt[ta][tb] == 0 && m[ta][tb] != 1)
          tmp.tx = ta, tmp.ty = tb, vt[ta][tb] = 2;

        if (m[ta][tb] == 1)
          tmp.kx = t.kx, tmp.ky = t.ky;
        else if (vk[ka][kb] == 0 && m[ka][kb] != 1)
          tmp.kx = ka, tmp.ky = kb, vt[ta][tb] = 2;

        tmp.ti = tim;
        if (tmp.ti >= 100)
          break;
        if (tmp.tx == tmp.kx && tmp.ky == tmp.ty) {
          if (ans > tmp.ti)
            ans = tmp.ti;
          continue;
        }
        q.push(tmp);
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    int ans;
    cin >> x >> y;
    if (x + y == 0)
      break;
    cin >> tk.tx >> tk.ty >> tk.kx >> tk.ky;
    for (int i = 1; i <= x; i++)
      for (int j = 1; j <= y; j++)
        vt[i][j] = 0, vk[i][j] = 0;
    tk.ti = 0;
    for (int i = 1; i <= x; i++)
      for (int j = 1; j <= y; j++)
        cin >> m[i][j];
    ans = bfs();
    if (ans < 100)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}