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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int maze[110][110];
int f[110][110];
int w, h, xs, ys, xg, yg, res = 0;
int color;

void dfs(int cy, int cx) {
  if (f[cy][cx])
    return;
  f[cy][cx] = 1;
  if (cy == yg && cx == xg) {
    res = 1;
    return;
  }
  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (abs(dx - dy) != 1)
        continue;
      int nx = cx + dx, ny = cy + dy;
      if (maze[ny][nx] == color) {
        dfs(ny, nx);
      }
    }
  }
  return;
}

int main(void) {
  int n;
  while (cin >> w >> h) {
    res = 0;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < 110; ++i) {
      for (int j = 0; j < 110; ++j) {
        maze[i][j] = 6;
        f[i][j] = 6;
      }
    }
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        maze[i][j] = 0;
        f[i][j] = 0;
      }
    }

    for (int i = 0; i < n; ++i) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = y; j <= y + 1; ++j) {
          for (int k = x; k <= x + 3; ++k) {
            maze[j][k] = c;
          }
        }
      }
      if (d == 1) {
        for (int j = y; j <= y + 3; ++j) {
          for (int k = x; k <= x + 1; ++k) {
            maze[j][k] = c;
          }
        }
      }
    }

    color = maze[ys][xs];
    if (color == 0) {
      cout << "NG" << endl;
      continue;
    }

    dfs(ys, xs);

    if (res) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int maze[110][110];
int f[110][110];
int w, h, xs, ys, xg, yg, res = 0;
int color;

void dfs(int cx, int cy) {
  if (f[cx][cy])
    return;
  f[cx][cy] = 1;

  if (cx == xg && cy == yg) {
    res = 1;
    return;
  }
  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (abs(dx - dy) != 1)
        continue;
      int nx = cx + dx, ny = cy + dy;
      if (maze[nx][ny] == color) {
        dfs(nx, ny);
      }
    }
  }
  return;
}

int main(void) {
  int n;
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < 110; ++i) {
      for (int j = 0; j < 110; ++j) {
        maze[i][j] = 0;
        f[i][j] = 0;
      }
    }

    for (int i = 0; i < n; ++i) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = y; j <= y + 1; ++j) {
          for (int k = x; k <= x + 3; ++k) {
            maze[j][k] = c;
          }
        }
      }
      if (d == 1) {
        for (int j = y; j <= y + 3; ++j) {
          for (int k = x; k <= x + 1; ++k) {
            maze[j][k] = c;
          }
        }
      }
    }

    color = maze[xs][ys];

    dfs(xs, ys);

    if (res) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

  return 0;
}