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

#include <cctype>
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c) {
  T n = 0;
  bool m = false;
  while (isspace(c)) {
    c = gcu();
  }
  if (c == '-')
    m = true, c = gcu();
  do {
    n = 10 * n + (c - '0'), c = gcu();
  } while (c >= '0' && c <= '9');
  return m ? -n : n;
}
_in() { return in<int>(gcu()); }
_T T scan(T &n) {
  int c = gcu();
  return c == EOF ? false : (n = in<T>(c), true);
}
_sc(char &c) {
  c = gcu();
  gcu();
  return c != EOF;
}
#ifdef _GLIBCXX_STRING
_sc(string &s) {
  int c;
  s = "";
  for (;;) {
    c = gcu();
    if (c == '\\n' || c == ' ')
      return true;
    else if (c == EOF)
      return false;
    s += c;
  }
}
#endif
_HT _sc(H &h, T &&...t) { return scan(h) && scan(t...); }
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) { pcu('0' + b); }
_vo(const char *s) {
  while (*s)
    pcu(*s++);
}
_vo(char c) { pcu(c); }
#ifdef _GLIBCXX_STRING
_vo(string s) {
  for (char c : s)
    pcu(c);
}
#endif
_T _vo(T n) {
  static char buf[20];
  char *p = buf;
  if (n < 0)
    pcu('-'), n *= -1;
  if (!n)
    *p++ = '0';
  else
    while (n)
      *p++ = n % 10 + '0', n /= 10;
  while (p != buf)
    pcu(*--p);
}
_vl() { out('\\n'); }
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v) {
  for (T &x : v)
    out(&x == &v[0] ? "" : " "), out(x);
  outl();
}
#endif
_HT _vo(H &&h, T &&...t) {
  out(h);
  out(move(t)...);
}
template <typename... T> _vl(T &&...t) {
  out(move(t)...);
  outl();
}

using V = vector<int>;
using VV = vector<V>;
struct P {
  int x, y;
};

int main() {
  int dx[] = {4, 2}, dy[] = {2, 4};
  for (int w, h; w = in(), h = in();) {
    P s = {in(), in()}, g = {in(), in()};
    int n = in();
    VV m(h + 2, V(w + 2));
    while (n--) {
      int c = in(), d = in(), x = in(), y = in();
      for (int i = 0; i < dy[d]; i++)
        for (int j = 0; j < dx[d]; j++)
          m[i + y][j + x] = c;
    }
    int c = m[s.y][s.x];
    m[s.y][s.x] = 0;
    function<bool(int, int)> f = [&](int x, int y) {
      if (x == g.x && y == g.y)
        return true;
      P d = {1, 0};
      for (int i = 0; i < 4; i++) {
        int tx = x + d.x, ty = y + d.y;
        if (m[ty][tx] == c) {
          m[ty][tx] = 0;
          if (f(tx, ty))
            return true;
        }
        swap(d.x, d.y *= -1);
      }
      return false;
    };
    outl(c ? f(s.x, s.y) ? "OK" : "NG" : "NG");
  }
}


Error: 0

Fixed submission:

#include <cctype>
#include <cstdio>
#include <functional>
#include <utility>
#include <vector>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c) {
  T n = 0;
  bool m = false;
  while (isspace(c)) {
    c = gcu();
  }
  if (c == '-')
    m = true, c = gcu();
  do {
    n = 10 * n + (c - '0'), c = gcu();
  } while (c >= '0' && c <= '9');
  return m ? -n : n;
}
_in() { return in<int>(gcu()); }
_T T scan(T &n) {
  int c = gcu();
  return c == EOF ? false : (n = in<T>(c), true);
}
_sc(char &c) {
  c = gcu();
  gcu();
  return c != EOF;
}
#ifdef _GLIBCXX_STRING
_sc(string &s) {
  int c;
  s = "";
  for (;;) {
    c = gcu();
    if (c == '\\n' || c == ' ')
      return true;
    else if (c == EOF)
      return false;
    s += c;
  }
}
#endif
_HT _sc(H &h, T &&...t) { return scan(h) && scan(t...); }
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) { pcu('0' + b); }
_vo(const char *s) {
  while (*s)
    pcu(*s++);
}
_vo(char c) { pcu(c); }
#ifdef _GLIBCXX_STRING
_vo(string s) {
  for (char c : s)
    pcu(c);
}
#endif
_T _vo(T n) {
  static char buf[20];
  char *p = buf;
  if (n < 0)
    pcu('-'), n *= -1;
  if (!n)
    *p++ = '0';
  else
    while (n)
      *p++ = n % 10 + '0', n /= 10;
  while (p != buf)
    pcu(*--p);
}
_vl() { out('\\n'); }
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v) {
  for (T &x : v)
    out(&x == &v[0] ? "" : " "), out(x);
  outl();
}
#endif
_HT _vo(H &&h, T &&...t) {
  out(h);
  out(move(t)...);
}
template <typename... T> _vl(T &&...t) {
  out(move(t)...);
  outl();
}

using V = vector<int>;
using VV = vector<V>;
struct P {
  int x, y;
};

int main() {
  int dx[] = {4, 2}, dy[] = {2, 4};
  for (int w, h; w = in(), h = in();) {
    P s = {in(), in()}, g = {in(), in()};
    int n = in();
    VV m(h + 1, V(w + 1));
    while (n--) {
      int c = in(), d = in(), x = in(), y = in();
      for (int i = 0; i < dy[d]; i++)
        for (int j = 0; j < dx[d]; j++)
          m[i + y][j + x] = c;
    }
    int c = m[s.y][s.x];
    m[s.y][s.x] = 0;
    function<bool(int, int)> f = [&](int x, int y) {
      if (x == g.x && y == g.y)
        return true;
      P d = {1, 0};
      for (int i = 0; i < 4; i++) {
        int tx = x + d.x, ty = y + d.y;
        if (m[ty][tx] == c) {
          m[ty][tx] = 0;
          if (f(tx, ty))
            return true;
        }
        swap(d.x, d.y *= -1);
      }
      return false;
    };
    outl(c ? f(s.x, s.y) ? "OK" : "NG" : "NG");
  }
}