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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define REP(i, n) for (ll i = 0; i < n; i++)

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

int h, w, ax, ay, bx, by, t, flag;
char field[30][30];
char pattern[20];

int main() {

  while (true) {

    scanf("%d%d", &h, &w);

    if (h == 0 && w == 0)
      break;

    REP(y, h) {
      scanf("%s", field[y]);
      REP(x, w) {
        if (field[y][x] == 'A') {
          ax = x;
          ay = y;
        } else if (field[y][x] == 'B') {
          bx = x;
          by = y;
        }
      }
    }

    scanf("%s", pattern);

    int leng = strlen(pattern);

    t = 0, flag = 0;

    queue<pii> q, nq;
    q.push(pii(ax, ay));

    REP(i, 200) {
      t++;

      // girl
      while (!q.empty()) {
        pii p = q.front();
        q.pop();
        REP(j, 4) {
          int nx = p.first + dx[j];
          int ny = p.second + dy[j];
          if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != '#' &&
              field[ny][nx] != 'A') {
            field[ny][nx] = 'A';
            nq.push(pii(nx, ny));
          }
        }
      }
      while (!nq.empty()) {
        q.push(nq.front());
        nq.pop();
      }

      // ghost
      switch (pattern[i % leng]) {
      case '5':
        break;
      case '8':
        by--;
        if (by < 0)
          by = 0;
        break;
      case '6':
        bx++;
        if (bx >= w)
          bx = w - 1;
        break;
      case '4':
        bx--;
        if (bx < 0)
          bx = 0;
        break;
      case '2':
        by++;
        if (by >= h)
          by = h - 1;
        break;
      }

      if (field[by][bx] == 'A') {
        flag = 1;
        break;
      }
    }

    if (flag == 0) {
      printf("impossible\\n");
    } else {
      printf("%d %d %d\\n", t, by, bx);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define REP(i, n) for (ll i = 0; i < n; i++)

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

int h, w, ax, ay, bx, by, t, flag;
char field[30][30];
char pattern[20];

int main() {

  while (true) {

    scanf("%d%d", &h, &w);

    if (h == 0 && w == 0)
      break;

    REP(y, h) {
      scanf("%s", field[y]);
      REP(x, w) {
        if (field[y][x] == 'A') {
          ax = x;
          ay = y;
        } else if (field[y][x] == 'B') {
          bx = x;
          by = y;
        }
      }
    }

    scanf("%s", pattern);

    int leng = strlen(pattern);

    t = 0, flag = 0;

    queue<pii> q, nq;
    q.push(pii(ax, ay));

    REP(i, 200) {
      t++;

      // girl
      while (!q.empty()) {
        pii p = q.front();
        q.pop();
        REP(j, 4) {
          int nx = p.first + dx[j];
          int ny = p.second + dy[j];
          if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != '#') {
            field[ny][nx] = 'A';
            nq.push(pii(nx, ny));
          }
        }
      }
      while (!nq.empty()) {
        q.push(nq.front());
        nq.pop();
      }

      // ghost
      switch (pattern[i % leng]) {
      case '5':
        break;
      case '8':
        by--;
        if (by < 0)
          by = 0;
        break;
      case '6':
        bx++;
        if (bx >= w)
          bx = w - 1;
        break;
      case '4':
        bx--;
        if (bx < 0)
          bx = 0;
        break;
      case '2':
        by++;
        if (by >= h)
          by = h - 1;
        break;
      }

      if (field[by][bx] == 'A') {
        flag = 1;
        break;
      }
    }

    if (flag == 0) {
      printf("impossible\\n");
    } else {
      printf("%d %d %d\\n", t, by, bx);
    }
  }

  return 0;
}