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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
using pll = pair<ll, ll>;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using ti3 = tuple<int, int, int>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#pragma warning(disable : 4996)
#define INF 1000000000000000.0

int res[502][502][3];
bool xwall[501][501];
bool ywall[501][501];
bool used[502][502];
typedef pair<int, int> P;

int main() {
  while (1) {
    int h, w, x, y;
    int i, j;
    scanf("%d %d %d %d", &h, &w, &x, &y);
    if (h == 0)
      break;
    for (int k = 0; k <= 2 * h; k++) {
      if (k % 2 == 0) {
        i = k / 2;
        for (j = 1; j <= w; j++) {
          int t;
          scanf("%d", &t);
          ywall[i][j] = (t == 1);
        }
      } else {
        i = (k + 1) / 2;
        for (j = 0; j <= w; j++) {
          int t;
          scanf("%d", &t);
          xwall[i][j] = (t == 1);
        }
      }
    }
    for (i = 0; i <= h + 1; i++)
      for (j = 0; j <= w + 1; j++)
        used[i][j] = false;
    for (i = 0; i <= h + 1; i++) {
      for (j = 0; j <= w + 1; j++) {
        res[i][j][0] = 0;
        res[i][j][1] = 0;
        res[i][j][2] = 0;
        if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
          res[i][j][0] = 1;
          res[i][j][1] = 1;
          res[i][j][2] = 1;
          used[i][j] = true;
        }
      }
    }
    queue<P> que;
    for (i = 1; i <= h; i++) {
      que.push(P(i, 0));
      que.push(P(i, w + 1));
    }
    for (j = 1; j <= w; j++) {
      que.push(P(0, j));
      que.push(P(h + 1, j));
    }
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      i = p.first;
      j = p.second;
      if (j - 1 >= 0) {
        if (xwall[i][j - 1])
          res[i][j - 1][1] = 1;
        else
          res[i][j - 1][0] = 1;
        if (res[i][j - 1][0] == 1 && res[i][j - 1][1] == 1 && !used[i][j - 1]) {
          used[i][j - 1] = true;
          que.push(P(i, j - 1));
        }
      }
      if (j + 1 <= w + 1) {
        if (xwall[i][j])
          res[i][j + 1][1] = 1;
        else
          res[i][j + 1][0] = 1;
        if (res[i][j + 1][0] == 1 && res[i][j + 1][1] == 1 && !used[i][j + 1]) {
          used[i][j + 1] = true;
          que.push(P(i, j + 1));
        }
      }
      if (i - 1 >= 0) {
        if (ywall[i - 1][j])
          res[i - 1][j][1] = 1;
        else
          res[i - 1][j][0] = 1;
        if (res[i - 1][j][0] == 1 && res[i - 1][j][1] == 1 && !used[i - 1][j]) {
          used[i - 1][j] = true;
          que.push(P(i - 1, j));
        }
      }
      if (i + 1 <= h + 1) {
        if (ywall[i][j])
          res[i + 1][j][1] = 1;
        else
          res[i + 1][j][0] = 1;
        if (res[i + 1][j][0] == 1 && res[i + 1][j][1] == 1 && !used[i + 1][j]) {
          used[i + 1][j] = true;
          que.push(P(i + 1, j));
        }
      }
    }
    printf("%s\\n", res[x][y][0] == 1 ? "Yes" : "No");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
using pll = pair<ll, ll>;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using ti3 = tuple<int, int, int>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#pragma warning(disable : 4996)
#define INF 1000000000000000.0

int res[502][502][3];
bool xwall[501][501];
bool ywall[501][501];

bool dfs(int x, int y, int t) {
  if (res[x][y][t] == 2)
    return false;
  if (res[x][y][t] != -1)
    return (res[x][y][t] == 1);
  res[x][y][t] = 2;
  if (t == 0) {
    if (!xwall[x][y - 1] && dfs(x, y - 1, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (!xwall[x][y] && dfs(x, y + 1, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (!ywall[x - 1][y] && dfs(x - 1, y, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (!ywall[x][y] && dfs(x + 1, y, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    res[x][y][t] = 0;
    return false;
  }
  if (t == 1) {
    if (xwall[x][y - 1] && dfs(x, y - 1, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (xwall[x][y] && dfs(x, y + 1, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (ywall[x - 1][y] && dfs(x - 1, y, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    if (ywall[x][y] && dfs(x + 1, y, 2)) {
      res[x][y][t] = 1;
      return true;
    }
    res[x][y][t] = 0;
    return false;
  }
  if (t == 2) {
    bool r = dfs(x, y, 0) && dfs(x, y, 1);
    res[x][y][t] = (r ? 1 : 0);
    return r;
  }
}

int main() {
  while (1) {
    int h, w, x, y;
    int i, j;
    scanf("%d %d %d %d", &h, &w, &x, &y);
    if (h == 0)
      break;
    for (int k = 0; k <= 2 * h; k++) {
      if (k % 2 == 0) {
        i = k / 2;
        for (j = 1; j <= w; j++) {
          int t;
          scanf("%d", &t);
          ywall[i][j] = (t == 1);
        }
      } else {
        i = (k + 1) / 2;
        for (j = 0; j <= w; j++) {
          int t;
          scanf("%d", &t);
          xwall[i][j] = (t == 1);
        }
      }
    }
    for (i = 0; i <= h + 1; i++) {
      for (j = 0; j <= w + 1; j++) {
        res[i][j][0] = -1;
        res[i][j][1] = -1;
        res[i][j][2] = -1;
        if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
          res[i][j][0] = 1;
          res[i][j][1] = 1;
          res[i][j][2] = 1;
        }
      }
    }
    printf("%s\\n", dfs(x, y, 0) ? "Yes" : "No");
  }
  return 0;
}