Problem p00223 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int h, w;
int map[100][100];
int qtx[600000], qty[600000], qkx[600000], qky[600000];
int ql[600000];
int tail, head;
int ans;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool vis[60][60][60][60];
bool bend;
void BFS(int tx, int ty, int kx, int ky, int len) {
  if (len == 100)
    return;
  if (tx == kx && ty == ky) {
    ans = len;
    bend = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    bool mt = false, mk = false;
    if (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
        ty + dy[i] < h) {
      if (map[ty + dy[i]][tx + dx[i]] == 0) {
        mt = true;
      }
    }
    if (kx - dx[i] >= 0 && kx - dx[i] < w && ky - dy[i] >= 0 &&
        ky - dy[i] < h) {
      if (map[ky - dy[i]][kx - dx[i]] == 0) {
        mk = true;
      }
    }
    qtx[tail] = tx + mt * dx[i], qty[tail] = ty + mt * dy[i];
    qkx[tail] = kx - mk * dx[i], qky[tail] = ky - mk * dy[i];
    ql[tail] = len + 1;
    tail++;
  }
}
int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 60; i++) {
      for (int j = 0; j < 60; j++) {
        for (int k = 0; k < 60; k++) {
          for (int r = 0; r < 60; r++) {
            vis[i][j][k][r] = false;
          }
        }
      }
    }
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans = 869121;
    qtx[0] = tx, qty[0] = ty, qkx[0] = kx, qky[0] = ky;
    ql[0] = 0;
    tail = 1, head = 0;
    bend = false;
    while (true) {
      if (tail == head)
        break;
      if (!vis[qtx[head]][qty[head]][qkx[head]][qky[head]]) {
        vis[qtx[head]][qty[head]][qkx[head]][qky[head]] = true;
        BFS(qtx[head], qty[head], qkx[head], qky[head], ql[head]);
      }
      if (bend)
        break;
      head++;
    }
    if (ans == 869121)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int h, w;
int map[100][100];
int qtx[800000], qty[800000], qkx[800000], qky[800000];
int ql[800000];
int tail, head;
int ans;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool vis[60][60][60][60];
bool bend;
void BFS(int tx, int ty, int kx, int ky, int len) {
  if (len == 100)
    return;
  if (tx == kx && ty == ky) {
    ans = len;
    bend = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    bool mt = false, mk = false;
    if (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
        ty + dy[i] < h) {
      if (map[ty + dy[i]][tx + dx[i]] == 0) {
        mt = true;
      }
    }
    if (kx - dx[i] >= 0 && kx - dx[i] < w && ky - dy[i] >= 0 &&
        ky - dy[i] < h) {
      if (map[ky - dy[i]][kx - dx[i]] == 0) {
        mk = true;
      }
    }
    qtx[tail] = tx + mt * dx[i], qty[tail] = ty + mt * dy[i];
    qkx[tail] = kx - mk * dx[i], qky[tail] = ky - mk * dy[i];
    ql[tail] = len + 1;
    tail++;
  }
}
int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 60; i++) {
      for (int j = 0; j < 60; j++) {
        for (int k = 0; k < 60; k++) {
          for (int r = 0; r < 60; r++) {
            vis[i][j][k][r] = false;
          }
        }
      }
    }
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans = 869121;
    qtx[0] = tx, qty[0] = ty, qkx[0] = kx, qky[0] = ky;
    ql[0] = 0;
    tail = 1, head = 0;
    bend = false;
    while (true) {
      if (tail == head)
        break;
      if (!vis[qtx[head]][qty[head]][qkx[head]][qky[head]]) {
        vis[qtx[head]][qty[head]][qkx[head]][qky[head]] = true;
        BFS(qtx[head], qty[head], qkx[head], qky[head], ql[head]);
      }
      if (bend)
        break;
      head++;
    }
    if (ans == 869121)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}