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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(type, i, n) for (type i = 0; i < (n); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> Pi;

const int INF = 100000000;
const long long int INF_ = 1000000000000000000;

int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
vector<int> parent, rank;

void Init(int n) {
  parent.resize(n);
  rank.resize(n);
  REP(int, i, n) {
    parent[i] = i;
    rank[i] = 0;
  }
}

int Find(int x) {
  if (parent[x] == x)
    return x;
  else
    return parent[x] = Find(parent[x]);
}

void Unite(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x == y)
    return;
  if (rank[x] < rank[y]) {
    parent[x] = y;
  } else {
    parent[y] = x;
    if (rank[x] == rank[y])
      ++rank[x];
  }
}

bool Same(int x, int y) { return Find(x) == Find(y); }

int serialize(const int y, const int x, const int width) {
  return y * width + x;
}

bool IsIn(const int y, const int x, const int height, const int width) {
  return 0 <= y && y < height && 0 <= x && x < width;
}

int main() {
  int W, H;
  cin >> W >> H;
  Init(W * H);
  vector<string> field(H);
  int goal_id;
  int start_id = serialize(0, 0, W);
  REP(int, i, H) {
    cin >> field[i];
    int idx = field[i].find('t');
    if (idx != string::npos) {
      goal_id = serialize(i, idx, W);
    }
  }
  REP(int, i, H) {
    REP(int, j, W) {
      if (field[i][j] == '#')
        continue;
      REP(int, k, 4) {
        int ny = i + dy[k], nx = j + dx[k];
        if (!IsIn(ny, nx, H, W) || field[ny][nx] == '#')
          continue;
        Unite(serialize(i, j, W), serialize(ny, nx, W));
      }
    }
  }
  int N;
  cin >> N;
  vector<Pi> query(N);
  REP(int, i, N) { cin >> query[i].S_ >> query[i].F_; }
  int ans = -1;
  if (Same(start_id, goal_id)) {
    ans = 0;
  } else {
    REP(int, i, N) {
      int y = query[i].F_, x = query[i].S_;
      if (field[y][x] == '.')
        continue;
      field[y][x] = '.';
      REP(int, j, 4) {
        int ny = y + dy[j], nx = x + dx[j];
        if (!IsIn(ny, nx, H, W) || field[ny][nx] == '#')
          continue;
        Unite(serialize(y, x, W), serialize(ny, nx, W));
      }
      if (Same(start_id, goal_id)) {
        ans = i + 1;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(type, i, n) for (type i = 0; i < (n); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> P;

const int INF = 100000000;
const long long int INF_ = 1000000000000000000;

int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1};
int W, H;

bool IsIn(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

bool BFS(vector<string> &vs, P &goal) {
  vector<vector<int>> f(H, vector<int>(W));
  queue<P> que;
  que.push(P(0, 0));
  bool result = false;
  while (!que.empty()) {
    const P p = que.front();
    que.pop();
    f[p.F_][p.S_] = 1;
    if (p == goal) {
      result = true;
      break;
    }
    REP(int, i, 4) {
      const int ny = p.F_ + dy[i], nx = p.S_ + dx[i];
      if (!IsIn(ny, nx))
        continue;
      if (vs[ny][nx] == '#')
        continue;
      if (f[ny][nx] != 1)
        que.push(P(ny, nx));
    }
  }
  return result;
}

int main() {
  while (cin >> W >> H) {
    vector<string> vs(H);
    P goal;
    REP(int, i, H) {
      cin >> vs[i];
      int idx = vs[i].find('t');
      if (idx != string::npos) {
        goal.F_ = i;
        goal.S_ = idx;
      }
    }
    int N;
    cin >> N;
    vector<P> query(N);
    REP(int, i, N) cin >> query[i].S_ >> query[i].F_;
    int ans = -1;
    if (BFS(vs, goal)) {
      ans = 0;
    } else {
      REP(int, i, N) {
        if (vs[query[i].F_][query[i].S_] == '.')
          continue;
        vs[query[i].F_][query[i].S_] = '.';
        if (BFS(vs, goal)) {
          ans = i + 1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}