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

#include <iostream>
#include <map>
#include <queue>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
class state {
public:
  int tx;
  int ty;
  int kx;
  int ky;
  bool operator<(const state &s) const {
    if (ty != s.ty)
      return ty < s.ty;
    if (tx != s.tx)
      return tx < s.tx;
    if (ky != s.ky)
      return ky < s.ky;
    if (kx != s.kx)
      return kx < s.kx;
    return false;
  }
};
int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    int d[52][52];
    for (int i = 0; i < H + 2; i++) {
      for (int j = 0; j < W + 2; j++) {
        d[i][j] = 1;
      }
    }
    state num;
    cin >> num.tx >> num.ty >> num.kx >> num.ky;
    for (int i = 1; i < H + 1; i++) {
      for (int j = 1; j < W + 1; j++) {
        cin >> d[i][j];
      }
    }
    queue<state> q;
    q.push(num);
    map<state, int> m;
    m[num] = 0;
    while (true) {
      num = q.front();
      q.pop();
      if (m[num] >= 100) {
        cout << "NA" << endl;
        break;
      }
      if (num.tx == num.kx && num.ty == num.ky) {
        cout << m[num] << endl;
        break;
      }
      for (int i = 0; i < 4; i++) {
        state copy = num;
        if (d[copy.ty + dy[i]][copy.tx + dx[i]] == 0) {
          copy.ty += dy[i];
          copy.tx += dx[i];
        }
        if (d[copy.ky - dy[i]][copy.kx - dx[i]] == 0) {
          copy.ky -= dy[i];
          copy.kx -= dx[i];
        }
        if (m.find(copy) == m.end()) {
          q.push(copy);
          m[copy] = m[num] + 1;
        }
      }
      if (q.empty()) {
        cout << "NA" << endl;
        break;
      }
    }
    m.clear();
    while (!q.empty()) {
      q.pop();
    }
  }
  return 0;
}


Error: 0
2 4 : 6 2 : 0
3 4 : 5 2 : 1
2 5 : 6 1 : 1
1 4 : 6 2 : 1
2 4 : 6 3 : 1
3 4 : 4 2 : 2
3 5 : 5 2 : 2
2 4 : 6 2 : 2
3 3 : 5 3 : 2
3 5 : 6 1 : 2
2 6 : 6 1 : 2
1 5 : 6 1 : 2
2 4 : 5 2 : 2
1 3 : 6 3 : 2
3 4 : 5 3 : 2
2 5 : 6 2 : 2
1 4 : 6 3 : 2
2 4 : 6 4 : 2
3 4 : 3 2 : 3
3 5 : 4 1 : 3
3 3 : 4 3 : 3
4 5 : 4 2 : 3
3 6 : 5 2 : 3
4 3 : 4 3 : 3
1 1 : 3 3 : 0
2 1 : 2 3 : 1
1 2 : 3 2 : 1
1 1 : 3 3 : 1
3 1 : 1 3 : 2
1 3 : 3 1 : 2
3 2 : 1 2 : 3
2 3 : 2 1 : 3
3 3 : 1 1 : 4


Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <utility>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  int W, H, tx, ty, kx, ky;
  bool d[52][52];
  map<pair<pair<int, int>, pair<int, int>>, bool> m;
  pair<pair<int, int>, pair<int, int>> num;
  pair<pair<int, int>, pair<int, int>> copy;
  while (true) {
    cin >> W >> H;
    if (W == 0)
      break;
    for (int i = 0; i < H + 2; i++) {
      for (int j = 0; j < W + 2; j++) {
        d[i][j] = 1;
      }
    }
    cin >> tx >> ty >> kx >> ky;
    for (int i = 1; i < H + 1; i++) {
      for (int j = 1; j < W + 1; j++) {
        cin >> d[j][i];
      }
    }
    num.first.first = tx;
    num.first.second = ty;
    num.second.first = kx;
    num.second.second = ky;
    queue<pair<pair<int, int>, pair<int, int>>> q;
    q.push(num);
    int qsize = q.size();
    int loopcount = 0;
    while (!q.empty()) {
      num = q.front();
      q.pop();
      if (qsize == 0) {
        qsize = q.size() + 1;
        loopcount++;
        if (loopcount == 100) {
          cout << "NA" << endl;
          break;
        }
      }
      qsize--;
      cerr << num.first.first << " " << num.first.second << " : "
           << num.second.first << " " << num.second.second << " : " << loopcount
           << endl;
      if (num.first.first == num.second.first &&
          num.first.second == num.second.second) {
        cout << loopcount << endl;
        break;
      }
      for (int i = 0; i < 4; i++) {
        copy.first.first = num.first.first + dx[i];
        copy.first.second = num.first.second + dy[i];
        if (d[copy.first.first][copy.first.second]) {
          copy.first.first -= dx[i];
          copy.first.second -= dy[i];
        }
        copy.second.first = num.second.first - dx[i];
        copy.second.second = num.second.second - dy[i];
        if (d[copy.second.first][copy.second.second]) {
          copy.second.first += dx[i];
          copy.second.second += dy[i];
        }
        if (!m[copy]) {
          m[copy] = true;
          q.push(copy);
        }
      }
      if (q.empty()) {
        cout << "NA" << endl;
      }
    }
  }
  return 0;
}