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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};

int n, m;
int cl[100][100];

int main() {
  queue<PII> q;
  set<PII> s;
  while (~scanf("%d %d", &n, &m) && n && m) {
    CLR(cl);
    bool can = false;
    int sx, sy, gx, gy;
    scanf("%d %d", &sx, &sy);
    --sx;
    --sy;
    scanf("%d %d", &gx, &gy);
    --gx;
    --gy;
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; i++) {
      int c, d, x, y;
      scanf("%d %d %d %d", &c, &d, &x, &y);
      --x;
      --y;
      if (d) {
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            cl[y + i][x + j] = c;
          }
        }
      } else {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 4; j++) {
            cl[y + i][x + j] = c;
          }
        }
      }
    }
    q.push(MP(sx, sy));
    while (q.size()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      if (s.count(MP(x, y))) {
        continue;
      }
      s.insert(MP(x, y));
      if (x == gx && y == gy) {
        can = true;
        break;
      }
      for (int i = 0; i < 4; i++) {
        if (x + dx[i] < 0 || x + dx[i] == n || y + dy[i] < 0 ||
            y + dy[i] == m || cl[y][x] != cl[y + dy[i]][x + dx[i]]) {
          continue;
        }
        q.push(MP(x + dx[i], y + dy[i]));
      }
    }
    printf("%s\\n", can ? "OK" : "NG");
    while (q.size()) {
      q.pop();
    }
    s.clear();
  }
  return 0;
}

Error: 0

Fixed submission:

// include----------------------------------------------------------------------------------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <vector>

// using
// namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

// define,const-----------------------------------------------------------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1 << 25;
#define MAX 1000

// global-----------------------------------------------------------------------------------------------------------------------
int board_range_w, board_range_h, start_x, start_y, goal_x, goal_y;
int color[MAX][MAX];
bool dp[MAX][MAX];

// function---------------------------------------------------------------------------------------------------------------------
bool solve(int x, int y, int before_x, int before_y) {
  if (x == goal_x && y == goal_y)
    return true;
  if (!color[x][y]) {
    return dp[x][y] = false;
  }
  if (x == 0 || y == 0)
    return dp[x][y] = false;
  if (color[x][y] == color[x + 1][y] && x + 1 != before_x && dp[x + 1][y])
    if (solve(x + 1, y, x, y))
      return true;
  if (color[x][y] == color[x][y + 1] && y + 1 != before_y && dp[x][y + 1])
    if (solve(x, y + 1, x, y))
      return true;
  if (color[x][y] == color[x][y - 1] && y - 1 != before_y && dp[x][y - 1])
    if (solve(x, y - 1, x, y))
      return true;
  if (color[x][y] == color[x - 1][y] && x - 1 != before_x && dp[x - 1][y])
    if (solve(x - 1, y, x, y))
      return true;
  return dp[x][y] = false;
}

int main() {
  while (scanf("%d %d", &board_range_w, &board_range_h)) {
    if (board_range_w == 0 && board_range_h == 0)
      return 0;
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++) {
        color[i][j] = 0;
        dp[i][j] = true;
      }
    }
    scanf("%d %d", &start_x, &start_y);
    scanf("%d %d", &goal_x, &goal_y);
    int block;
    scanf("%d", &block);
    for (int i = 0; i < block; i++) {
      // ブロック情報
      int block_color, block_quarter, block_start_x, block_start_y;
      scanf("%d %d %d %d", &block_color, &block_quarter, &block_start_x,
            &block_start_y);
      color[block_start_x + 1][block_start_y + 1] = block_color;
      color[block_start_x][block_start_y + 1] = block_color;
      color[block_start_x + 1][block_start_y] = block_color;
      color[block_start_x][block_start_y] = block_color;
      if (block_quarter == 0) {
        color[block_start_x + 2][block_start_y + 1] = block_color;
        color[block_start_x + 2][block_start_y] = block_color;
        color[block_start_x + 3][block_start_y + 1] = block_color;
        color[block_start_x + 3][block_start_y] = block_color;
      } else {
        color[block_start_x + 1][block_start_y + 3] = block_color;
        color[block_start_x][block_start_y + 3] = block_color;
        color[block_start_x + 1][block_start_y + 2] = block_color;
        color[block_start_x][block_start_y + 2] = block_color;
      }
    }
    if (solve(start_x, start_y, start_x, start_y))
      printf("OK\\n");
    else
      printf("NG\\n");
  }
  return 0;
}