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

#include <bits/stdc++.h>
#define N 510
using namespace std;
typedef pair<int, int> P;
int h, w;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
bool mp[N][N][4], used[N][N] = {}, gxy[N][N];
;

bool check(int x, int y) {
  bool f[2] = {};
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    f[mp[y][x][i]] |=
        (nx == 0 || ny == 0 || nx == w + 1 || ny == h + 1 || used[ny][nx]);
  }
  return f[0] && f[1];
}

bool bfs() {
  memset(used, 0, sizeof(used));
  queue<P> Q;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (check(j, i))
        Q.push(P(j, i)), used[i][j] = 1;

  while (!Q.empty()) {
    P t = Q.front();
    Q.pop();
    int x = t.first, y = t.second;
    if (gxy[y][x])
      return 1;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx == 0 || ny == 0 || nx == w + 1 || ny == h + 1 || used[ny][nx])
        continue;
      if (check(nx, ny))
        Q.push(P(nx, ny)), used[ny][nx] = 1;
    }
  }
  return 0;
}

int main() {
  while (1) {
    int sy, sx;
    cin >> h >> w >> sy >> sx;
    if (!h && !w && !sy && !sx)
      break;
    for (int i = 1; i <= h + 1; i++) {
      for (int j = 1, a; j <= w; j++)
        cin >> a, mp[i - 1][j][2] = mp[i][j][0] = a;
      for (int j = 1, a; j <= w + 1 && i < h + 1; j++)
        cin >> a, mp[i][j - 1][1] = mp[i][j][3] = a;
    }

    memset(gxy, 0, sizeof(gxy));
    int ans = 0;
    for (int i = 0; i < 4; i++)
      if (!mp[sy][sx][i]) {
        int nx = sx + dx[i], ny = sy + dy[i];
        gxy[ny][nx] = 1;
        if (nx == 0 || ny == 0 || nx == w + 1 || ny == h + 1)
          ans = 1;
      }
    cout << (ans || bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 510
using namespace std;
int h, w;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
bool mp[N][N][4], mem[N][N], used[N][N];

bool check(int x, int y) {
  bool cnt[2] = {}, r[2] = {};
  for (int i = 0; i < 4; i++) {
    cnt[mp[y][x][i]] |= (!used[y + dy[i]][x + dx[i]]);
    r[mp[y][x][i]] |= mem[y + dy[i]][x + dx[i]];
  }
  if (r[0] && r[1])
    return 1;
  return cnt[0] && cnt[1];
}

bool dfs(int x, int y) {
  if (x == 0 || y == 0 || x == w + 1 || y == h + 1)
    return 1;
  if (used[y][x])
    return mem[y][x] == 1;
  used[y][x] = 1;
  if (!check(x, y))
    return mem[y][x] = 0;

  bool r[2] = {};
  for (int i = 0; i < 4 && (!r[0] || !r[1]); i++)
    r[mp[y][x][i]] |= dfs(x + dx[i], y + dy[i]);
  return mem[y][x] = r[0] && r[1];
}

int main() {
  while (1) {
    int sy, sx;
    cin >> h >> w >> sy >> sx;
    if (!h && !w && !sy && !sx)
      break;
    for (int i = 1; i <= h + 1; i++) {
      for (int j = 1, a; j <= w; j++)
        cin >> a, mp[i - 1][j][2] = mp[i][j][0] = a;
      for (int j = 1, a; j <= w + 1 && i < h + 1; j++)
        cin >> a, mp[i][j - 1][1] = mp[i][j][3] = a;
    }

    bool ans = 0;
    for (int i = 0; i < 4; i++) {
      memset(used, 0, sizeof(used));
      memset(mem, 0, sizeof(mem));
      if (!mp[sy][sx][i])
        ans |= dfs(sx + dx[i], sy + dy[i]);
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}