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

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int fld[100][100] = {};
    int sx, sy, gx, gy, n;
    cin >> sy >> sx >> gy >> gx >> n;
    sy--;
    sx--;
    gy--;
    gx--;
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> y >> x;
      y--;
      x--;
      if (d) {
        REP(i, 4) REP(j, 2) fld[x + i][y + j] = c;
      } else {
        REP(i, 2) REP(j, 4) fld[x + i][y + j] = c;
      }
    }
    /*
    REP(i, h)
    {
            REP(j, w) cout << fld[i][j];
            cout << endl;
    }
    */
    queue<P> que;
    que.push(P(sx, sy));
    bool f = false;
    int color = fld[sx][sy];
    while (que.size()) {
      if (color == 0)
        break;
      int tx = que.front().first, ty = que.front().second;
      fld[tx][ty] = 0;
      que.pop();
      if (tx == gx && ty == gy) {
        f = true;
        break;
      }
      REP(i, 4) {
        int nx = tx + dx[i], ny = ty + dy[i];
        if (valid(nx, ny, h, w) && fld[nx][ny] == color)
          que.push(P(nx, ny));
      }
    }
    puts(f ? "OK" : "NG");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<vi> v(h, vi(w));
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;
    int n;
    cin >> n;
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      if (d == 0) {
        REP(i, 4) REP(j, 2) { v[y + j][x + i] = c; }
      } else {
        REP(i, 2) REP(j, 4) { v[y + j][x + i] = c; }
      }
    }
    int co = v[sx][sy];
    if (co == 0) {
      puts("NG");
      continue;
    }

    queue<P> que;
    que.push(P(sx, sy));
    bool f = false;
    while (que.size()) {
      P tmp = que.front();
      que.pop();
      int tx = tmp.first, ty = tmp.second;
      if (tx == gx && ty == gy) {
        f = true;
        break;
      }
      v[tx][ty] = 0;
      REP(i, 4) {
        int nx = tx + dx[i], ny = ty + dy[i];
        if (valid(nx, ny, w, h) && v[nx][ny] == co)
          que.push(P(nx, ny));
      }
    }
    puts(f ? "OK" : "NG");
  }
}