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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int w, h;
  while (cin >> w >> h, w + h) {
    int xs, ys, xg, yg;
    bool Map[h + 10][w + 10] = {};
    bool flag = false;
    cin >> xs >> ys >> xg >> yg;
    xs--;
    ys--;
    xg--;
    yg--;
    int n;
    cin >> n;
    int Map2[h + 10][w + 10] = {};
    Map[ys][xs] = true;
    queue<pair<int, int>> que;
    que.push(mp(ys, xs));
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      if (d == 0) {
        for (int i = y; i < y + 2; i++)
          for (int j = x; j < x + 4; j++)
            Map2[i][j] = c;
      } else if (d == 1) {
        for (int i = y; i < y + 4; i++)
          for (int j = x; j < x + 2; j++)
            Map2[i][j] = c;
      }
    }
    int vx[4] = {0, -1, 0, 1};
    int vy[4] = {1, 0, -1, 0};
    while (!que.empty()) {
      pair<int, int> now = que.front();
      que.pop();
      if (now.F == yg && now.S == xg) {
        flag = true;
        break;
      }
      for (int kk = 0; kk < 4; kk++) {
        int as = now.F + vx[kk];
        int bs = now.S + vy[kk];
        if (as < h && as >= 0 && bs < w && bs >= 0 &&
            Map2[now.F][now.S] == Map2[as][bs] && !Map[as][bs]) {
          que.push(mp(as, bs));
          Map[as][bs] = true;
        }
      }
    }
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int vx[4] = {1, 0, -1, 0};
int vy[4] = {0, 1, 0, -1};
int flag = 0;
int board[102][102] = {0};
vector<pair<int, int>> com;
queue<pair<pair<int, int>, int>> qp;
int num = 0;
pair<pair<int, int>, int> now;
int check(int i) {
  if (now.second != board[now.first.second + vy[i]][now.first.first + vx[i]])
    return 0;
  for (int j = 0; j < com.size(); j++)
    if (now.first.second + vy[i] == com[j].second &&
        now.first.first + vx[i] == com[j].first)
      return 0;
  return 1;
}
int main() {
  int w, h;
  while (cin >> w >> h, w + h != 0) {
    flag = 0;
    num = 0;
    com.clear();
    while (!qp.empty())
      qp.pop();
    int xs, ys, xg, yg;
    for (int i = 0; i < h + 2; i++)
      for (int j = 0; j < w + 2; j++)
        board[i][j] = 0;
    cin >> xs >> ys;
    cin >> xg >> yg;
    int n, c, d, x, y;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 4; k++) {
          if (d == 0)
            board[y + j][x + k] = c;
          else
            board[y + k][x + j] = c;
        }
      }
    }
    qp.push(make_pair(make_pair(xs, ys), board[ys][xs]));
    while (!qp.empty()) {
      now = qp.front();
      qp.pop();
      for (int i = 0; i < 4; i++) {
        num++;
        if (check(i) == 1) {
          qp.push(make_pair(
              make_pair(now.first.first + vx[i], now.first.second + vy[i]),
              now.second));
          com.push_back(
              make_pair(now.first.first + vx[i], now.first.second + vy[i]));
          if (now.first.first + vx[i] == xg && now.first.second + vy[i] == yg) {
            flag = 1;
            break;
          }
        }
      }
      if (flag == 1) {
        cout << "OK" << endl;
        break;
      }
      if (num == 10000) {
        cout << "NG" << endl;
        break;
      }
    }
  }
  return 0;
}