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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
struct P {
  P(){};
  P(int a, int b) { x = a, y = b; }
  int x, y;
  void move(int, int);
};
struct S {
  S(){};
  S(P a, P b) {
    t = a;
    k = b;
  }
  P t, k;
  void move(int);
};

P size;
S f_pos;
vector<vector<int>> f;
bool input() {
  size = P();
  f_pos = S();
  f.clear();
  cin >> size.y >> size.x;
  if (size.x == 0 && size.y == 0)
    return false;
  cin >> f_pos.t.y >> f_pos.t.x;
  cin >> f_pos.k.y >> f_pos.k.x;
  vector<int> t;
  for (int i = 0; i < size.y + 2; i++) {
    t.push_back(1);
  }
  f.push_back(t);
  for (int i = 0; i < size.x; i++) {
    vector<int> tt;
    tt.push_back(1);
    for (int j = 0; j < size.y; j++) {
      int tmp;
      cin >> tmp;
      tt.push_back(tmp);
    }
    tt.push_back(1);
    f.push_back(tt);
  }
  f.push_back(t);
  return true;
}
int cost[60][60][60][60];
const int vx[] = {0, 0, 1, -1};
const int vy[] = {1, -1, 0, 0};
void S::move(int i) {
  t.move(vx[i], vy[i]);
  k.move(-vx[i], -vy[i]);
}
void P::move(int a, int b) {
  if (f[x + a][y + b] != 1) {
    x += a;
    y += b;
  }
}
int solve() {
  for (int i = 0; i < 60; i++) {
    for (int j = 0; j < 60; j++) {
      for (int k = 0; k < 60; k++) {
        for (int l = 0; l < 60; l++) {
          cost[i][j][k][l] = -1;
        }
      }
    }
  }
  queue<S> q;
  q.push(f_pos);
  cost[f_pos.t.x][f_pos.t.y][f_pos.k.x][f_pos.k.y] = 0;
  while (!q.empty()) {
    S now = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      S tmp = now;
      tmp.move(i);
      if (cost[tmp.t.x][tmp.t.y][tmp.k.x][tmp.k.y] != -1)
        continue;

      cost[tmp.t.x][tmp.t.y][tmp.k.x][tmp.k.y] =
          cost[now.t.x][now.t.y][now.k.x][now.k.y] + 1;
      if (cost[tmp.t.x][tmp.t.y][tmp.k.x][tmp.k.y] >= 100) {
        return -1;
      } else if ((tmp.t.x == tmp.k.x) && (tmp.t.y == tmp.k.y)) {
        return cost[tmp.t.x][tmp.t.y][tmp.k.x][tmp.k.y];
      } else {
        q.push(tmp);
      }
    }
  }
  return -1;
}
int main() {
  while (input()) {
    /*
    for(int i=0;i<f.size();i++){
      for(int j=0;j<f[i].size();j++){
        cout<<f[i][j];
      }cout<<endl;
    }cout<<endl;
    */
    int ans = solve();
    if (ans == -1 || ans >= 100) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

struct S {
  S(){};
  int tx, ty, kx, ky;
};
int X, Y;
S s;
vector<vector<int>> f;

bool input() {
  f.clear();
  s = S();
  cin >> X >> Y;
  if (X == 0 && Y == 0)
    return false;
  cin >> s.tx >> s.ty;
  cin >> s.kx >> s.ky;
  // swap(s.tx,s.ty);
  // swap(s.kx,s.ky);
  vector<int> kabe;
  for (int i = 0; i < X + 2; i++) {
    kabe.push_back(1);
  }
  f.push_back(kabe);
  for (int i = 0; i < Y; i++) {
    vector<int> tt;
    tt.push_back(1);
    for (int j = 0; j < X; j++) {
      int tmp;
      cin >> tmp;
      tt.push_back(tmp);
    }
    tt.push_back(1);
    f.push_back(tt);
  }
  f.push_back(kabe);
  return true;
}

int vx[] = {0, 0, 1, -1};
int vy[] = {1, -1, 0, 0};
int used[60][60][60][60];
queue<S> q;
int bfs(S pos) {
  memset(used, -1, (60 * 60 * 60 * 60));
  q = queue<S>();
  q.push(pos);
  used[pos.tx][pos.ty][pos.kx][pos.ky] = 0;
  while (!q.empty()) {
    S now = q.front();
    q.pop();
    if (used[now.tx][now.ty][now.kx][now.ky] >= 100) {
      return -1;
    }
    if (now.tx == now.kx && now.ty == now.ky) {
      return used[now.tx][now.ty][now.kx][now.ky];
    }
    for (int i = 0; i < 4; i++) {
      S tmp = now;
      bool a, b;
      if (a = (f[tmp.tx + vx[i]][tmp.ty + vy[i]] != 1)) {
        tmp.tx += vx[i];
        tmp.ty += vy[i];
      }
      if (b = (f[tmp.kx - vx[i]][tmp.ky - vy[i]] != 1)) {
        tmp.kx -= vx[i];
        tmp.ky -= vy[i];
      }
      if ((!(a || b)) || (used[tmp.tx][tmp.ty][tmp.kx][tmp.ky] != -1)) {
        continue;
      }
      used[tmp.tx][tmp.ty][tmp.kx][tmp.ky] =
          used[now.tx][now.ty][now.kx][now.ky] + 1;
      q.push(tmp);
    }
  }
  return -1;
}

int solve() { return bfs(s); }

int main() {
  while (input()) {
    int ans = solve();
    if (ans >= 0) {
      cout << ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}