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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int w, h, ans;
int a[51][51];
bool f[51][51][51][51];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct cost {
  P tp, kp;
  int cnt;
};

void solve(struct cost c) {
  queue<struct cost> q;
  q.push(c);
  ans = -1;
  for (int ty = 0; ty < h; ty++) {
    for (int tx = 0; tx < w; tx++) {
      for (int ky = 0; ky < h; ky++) {
        for (int kx = 0; kx < w; kx++) {
          f[ty][tx][ky][kx] = false;
        }
      }
    }
  }

  while (!q.empty()) {
    struct cost now = q.front(), next;
    q.pop();
    int tx = now.tp.first;
    int ty = now.tp.second;
    int kx = now.kp.first;
    int ky = now.kp.second;
    next.cnt = now.cnt + 1;
    if (f[ty][tx][ky][kx]) {
      continue;
    } else {
      f[ty][tx][ky][kx] = true;
    }
    if (now.cnt == 100) {
      break;
    }
    if (tx == kx && ty == ky) {
      ans = now.cnt;
      break;
    }

    for (int i = 0; i < 4; i++) {
      int tmx = tx + dx[i];
      int tmy = ty + dy[i];
      int kmx = kx - dx[i];
      int kmy = ky - dy[i];
      if (tmx < 0 || tmy < 0 || tmx >= w || tmy >= h || a[tmy][tmx]) {
        P tp(tx, ty);
        next.tp = tp;
      } else {
        P tp(tmx, tmy);
        next.tp = tp;
      }
      if (kmx < 0 || kmy < 0 || kmx >= w || kmy >= h || a[kmy][kmx]) {
        P kp(kx, ky);
        next.kp = kp;
      } else {
        P kp(kmx, kmy);
        next.kp = kp;
      }
      q.push(next);
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    tx--;
    ty--;
    kx--;
    ky--;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> a[y][x];
      }
    }

    struct cost c;
    P tp(tx, ty), kp(kx, ky);
    c.tp = tp;
    c.kp = kp;
    c.cnt = 0;
    solve(c);
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int w, h, ans;
int a[51][51];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct cost {
  P tp, kp;
  int cnt;
};

void solve(struct cost c) {
  queue<struct cost> q;
  q.push(c);
  ans = -1;

  while (!q.empty()) {
    struct cost now = q.front(), next;
    q.pop();
    int tx = now.tp.first;
    int ty = now.tp.second;
    int kx = now.kp.first;
    int ky = now.kp.second;
    next.cnt = now.cnt + 1;
    if (now.cnt == 100) {
      break;
    }
    if (tx == kx && ty == ky) {
      ans = now.cnt;
      break;
    }

    for (int i = 0; i < 4; i++) {
      int tmx = tx + dx[i];
      int tmy = ty + dy[i];
      int kmx = kx - dx[i];
      int kmy = ky - dy[i];
      if (tmx < 0 || tmy < 0 || kmx < 0 || kmy < 0) {
        continue;
      } else if (tmx >= w || tmy >= h || kmx >= w || kmy >= h) {
        continue;
      }

      if (a[tmy][tmx] == 0) {
        P tp(tmx, tmy);
        next.tp = tp;
      } else {
        P tp(tx, ty);
        next.tp = tp;
      }
      if (a[kmy][kmx] == 0) {
        P kp(kmx, kmy);
        next.kp = kp;
      } else {
        P kp(kx, ky);
        next.kp = kp;
      }
      q.push(next);
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> a[y][x];
      }
    }

    struct cost c;
    P tp(tx, ty), kp(kx, ky);
    c.tp = tp;
    c.kp = kp;
    c.cnt = 0;
    solve(c);
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}