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

#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

struct point {
  int x;
  int y;
};

int main() {
  int houkou[5] = {0, 1, 0, -1, 0};
  int h, w, r, c;
  while (1) {
    cin >> h >> w >> r >> c;
    if (h == 0)
      return 0;
    vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
    vector<vector<int>> horwall(h + 1, vector<int>(w));
    vector<vector<int>> verwall(h, vector<int>(w + 1));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> horwall[i][j];
      }
      for (int j = 0; j < w + 1; j++) {
        cin >> verwall[i][j];
      }
    }
    for (int j = 0; j < w; j++) {
      cin >> horwall[h][j];
    }
    for (int i = 0; i < h + 2; i++) {
      field[i][0] = 1;
      field[i][w + 1] = 1;
    }
    for (int i = 0; i < w + 2; i++) {
      field[0][i] = 1;
      field[h + 1][i] = 1;
    }
    // for(int i=0;i<h+2;i++){
    //   for(int j=0;j<w+2;j++){
    //     cout << field[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    queue<struct point> q;
    bool ok[2];
    for (int i = 1; i <= h; i++) {
      q.push({1, i});
      q.push({w, i});
    }
    for (int i = 1; i <= w; i++) {
      q.push({i, 1});
      q.push({i, h});
    }
    struct point now;
    while (!q.empty()) {
      now = q.front();
      q.pop();
      if (!field[now.y][now.x]) {
        ok[0] = ok[1] = false;
        // cout << now.x << " " << now.y << endl;
        if (field[now.y][now.x - 1]) {
          ok[verwall[now.y - 1][now.x - 1]] = true;
        }
        if (field[now.y][now.x + 1]) {
          ok[verwall[now.y - 1][now.x]] = true;
        }
        if (field[now.y - 1][now.x]) {
          ok[horwall[now.y - 1][now.x - 1]] = true;
        }
        if (field[now.y + 1][now.x]) {
          ok[horwall[now.y][now.x - 1]] = true;
        }
        if (ok[0] & ok[1]) {
          field[now.y][now.x] = 1;
          for (int i = 0; i < 4; i++) {
            if (!field[now.y + houkou[i]][now.x + houkou[i + 1]])
              q.push({now.x + houkou[i + 1], now.y + houkou[i]});
          }
          // for(int i=0;i<h+2;i++){
          //   for(int j=0;j<w+2;j++){
          //     cout << field[i][j] << " ";
          //   }
          //   cout << endl;
          // }
        }
      }
    }
    // for(int i=1;i<=h;i++){
    //   for(int j=1;j<=w;j++){
    //     cout << field[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    if ((!horwall[r - 1][c - 1] & field[r - 1][c]) |
        (!horwall[r][c - 1] & field[r + 1][c]) |
        (!verwall[r - 1][c - 1] & field[r][c - 1]) |
        (!verwall[r - 1][c] & field[r][c + 1]))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

struct point {
  int x;
  int y;
};

int main() {
  int houkou[5] = {0, 1, 0, -1, 0};
  int h, w, r, c;
  while (1) {
    cin >> h >> w >> r >> c;
    if (h == 0)
      return 0;
    vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
    vector<vector<int>> horwall(h + 1, vector<int>(w));
    vector<vector<int>> verwall(h, vector<int>(w + 1));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> horwall[i][j];
      }
      for (int j = 0; j < w + 1; j++) {
        cin >> verwall[i][j];
      }
    }
    for (int j = 0; j < w; j++) {
      cin >> horwall[h][j];
    }
    for (int i = 0; i < h + 2; i++) {
      field[i][0] = 1;
      field[i][w + 1] = 1;
    }
    for (int i = 0; i < w + 2; i++) {
      field[0][i] = 1;
      field[h + 1][i] = 1;
    }
    // for(int i=0;i<h+2;i++){
    //   for(int j=0;j<w+2;j++){
    //     cout << field[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    queue<struct point> q;
    bool ok[2];
    for (int i = 1; i <= h; i++) {
      q.push({1, i});
      q.push({w, i});
    }
    for (int i = 1; i <= w; i++) {
      q.push({i, 1});
      q.push({i, h});
    }
    struct point now;
    while (!q.empty()) {
      now = q.front();
      q.pop();
      ok[0] = ok[1] = false;
      // cout << now.x << " " << now.y << endl;
      if (field[now.y][now.x - 1]) {
        ok[verwall[now.y - 1][now.x - 1]] = true;
      }
      if (field[now.y][now.x + 1]) {
        ok[verwall[now.y - 1][now.x]] = true;
      }
      if (field[now.y - 1][now.x]) {
        ok[horwall[now.y - 1][now.x - 1]] = true;
      }
      if (field[now.y + 1][now.x]) {
        ok[horwall[now.y][now.x - 1]] = true;
      }
      if (ok[0] & ok[1]) {
        field[now.y][now.x] = 1;
        for (int i = 0; i < 4; i++) {
          if (!field[now.y + houkou[i]][now.x + houkou[i + 1]])
            q.push({now.x + houkou[i + 1], now.y + houkou[i]});
        }
        // for(int i=0;i<h+2;i++){
        //   for(int j=0;j<w+2;j++){
        //     cout << field[i][j] << " ";
        //   }
        //   cout << endl;
        // }
      }
    }
    if (field[c][r])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}