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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int sx, sy;
int gx, gy;
vvi tmp;
int q;
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
void solve(int x, int y) {
  tmp[x][y] = -1;
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (tmp[nx][ny] == q) {
      solve(nx, ny);
    }
  }
}
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vvi in(n + 2, vi(m + 2));
    cin >> sx >> sy >> gx >> gy;
    cin >> n;
    while (n--) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      if (!b)
        rep(i, 4) rep(j, 2) in[c + i][d + j] = a;
      else
        rep(i, 2) rep(j, 4) in[c + i][d + j] = a;
    }
    tmp = in;
    // rep(i,in.size()){rep(j,in[0].size())cout<<in[i][j]<<" ";cout<<endl;}
    if (in[sx][sy]) {
      q = in[sx][sy];
      solve(sx, sy);
    }
    // cout<<endl;
    // rep(i,tmp.size()){rep(j,tmp[0].size())cout<<tmp[i][j]<<" ";cout<<endl;}
    cout << ((tmp[gx][gy] == -1) ? "OK" : "NG") << endl;
    // rep(i,22){rep(j,22)cout<<tmp[i][j]<<" ";cout<<endl;}
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int x[4] = {-1, 1, 0, 0};
int y[4] = {0, 0, -1, 1};
int color;
void func(vector<vector<int>> &in, vector<vector<int>> &map, int a, int b) {
  // cout<<a<<" "<<b<<endl;
  map[a][b] = 2;
  rep(i, 4) if (in[a + x[i]][b + y[i]] == color && map[a + x[i]][b + y[i]] == 1)
      func(in, map, a + x[i], b + y[i]);
}
int main() {
  int n, m;
  while (cin >> m >> n, n || m) {
    vector<vector<int>> in(n + 2, vector<int>(m + 2));
    int xs, ys, xg, yg;
    cin >> ys >> xs >> yg >> xg;
    int q;
    cin >> q;
    rep(i, q) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      if (b)
        rep(i, 2) rep(j, 4) in[i + c][j + d] = a;
      else
        rep(i, 4) rep(j, 2) in[i + c][j + d] = a;
    }
    // cout<<n<<" "<<m<<endl;
    // loop(i,1,n+1){loop(j,1,m+1)cout<<" "<<in[i][j];cout<<endl;}

    if (in[xs][ys] == 0 || in[xg][yg] == 0) {
      cout << "NG" << endl;
      continue;
    }
    color = in[xs][ys];
    vector<vector<int>> map(n + 2, vector<int>(m + 2));
    loop(i, 1, n + 1) loop(j, 1, m + 1) map[i][j] = 1;
    // loop(i,1,n+1){loop(j,1,m+1)cout<<" "<<map[i][j];cout<<endl;}

    func(in, map, xs, ys);
    if (map[xg][yg] == 2)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
    // cout<<n<<" "<<m<<endl;
    // loop(i,1,n+1){loop(j,1,m+1)cout<<" "<<map[i][j];cout<<endl;}
  }
}