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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
map<pair<int, int>, int> futon;
map<pair<int, int>, int> color;
int futonX[30000];
int futonY[30000];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

bool dfs(int x, int y, int c) {
  if (color[make_pair(x, y)] != -1) {
    if (color[make_pair(x, y)] == c) {
      return true;
    } else {
      return false;
    }
  }
  color[make_pair(x, y)] = c;
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!futon.count(make_pair(nx, ny))) {
      continue;
    }
    int nc = c;
    if (futon[make_pair(nx, ny)] == futon[make_pair(x, y)]) {
      nc ^= 1;
    }
    if (!dfs(nx, ny, nc)) {
      return false;
    }
  }
  return true;
}

int main() {
  while (scanf("%d", &n), n) {
    futon.clear();
    color.clear();
    REP(i, n) {
      int x, y;
      char c;
      scanf("%d %d %c", &x, &y, &c);
      futonX[i] = x;
      futonY[i] = y;
      int dir = c == 'x' ? 0 : 1;
      REP(j, 2) {
        int nx = x + dx[dir] * j;
        int ny = y + dy[dir] * j;
        futon[make_pair(nx, ny)] = i;
        color[make_pair(nx, ny)] = -1;
      }
    }
    REP(i, n) {
      int x = futonX[i];
      int y = futonY[i];
      if (color[make_pair(x, y)] != -1) {
        continue;
      }
      if (!dfs(x, y, 0)) {
        puts("No");
        goto next;
      }
    }
    puts("Yes");
  next:;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
map<pair<int, int>, int> futon;
map<pair<int, int>, int> color;
int futonX[30000];
int futonY[30000];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

bool dfs(int x, int y, int c) {
  if (color[make_pair(x, y)] != -1) {
    if (color[make_pair(x, y)] == c) {
      return true;
    } else {
      return false;
    }
  }
  color[make_pair(x, y)] = c;
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!futon.count(make_pair(nx, ny))) {
      continue;
    }
    int nc = c;
    if (futon[make_pair(nx, ny)] == futon[make_pair(x, y)]) {
      nc ^= 1;
    }
    assert(nx != 1 || ny != 0 || nc != 0);
    if (!dfs(nx, ny, nc)) {
      return false;
    }
  }
  return true;
}

int main() {
  while (scanf("%d", &n), n) {
    futon.clear();
    color.clear();
    REP(i, n) {
      int x, y;
      char c;
      scanf("%d %d %c", &x, &y, &c);
      futonX[i] = x;
      futonY[i] = y;
      int dir = c == 'x' ? 0 : 1;
      REP(j, 2) {
        int nx = x + dx[dir] * j;
        int ny = y + dy[dir] * j;
        futon[make_pair(nx, ny)] = i;
        color[make_pair(nx, ny)] = -1;
      }
    }
    REP(i, n) {
      int x = futonX[i];
      int y = futonY[i];
      if (color[make_pair(x, y)] != -1) {
        continue;
      }
      if (!dfs(x, y, 0)) {
        puts("No");
        goto next;
      }
    }
    puts("Yes");
  next:;
  }
}