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

#include <cstdio>
#include <cstring>
#include <stack>
struct p {
  int x;
  int y;
};
using namespace std;
int main() {
  int w, h, sx, sy, gx, gy, n, c, d, x, y, i, j, k, o;
  int map[100][100];
  int used[100][100];
  int mx[4] = {-1, 0, 1, 0};
  int my[4] = {0, -1, 0, 1};
  p v, u;
  stack<p> s;
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0) {
      break;
    }
    memset(map, 0, sizeof(map));
    memset(used, 0, sizeof(used));
    scanf("%d %d", &sx, &sy);
    sx--;
    sy--;
    scanf("%d %d", &gx, &gy);
    gx--;
    gy--;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d %d %d %d", &c, &d, &x, &y);
      x--;
      y--;
      if (!d) {
        for (j = y; j < y + 2; j++) {
          for (k = x; k < x + 4; k++) {
            map[j][k] = c;
          }
        }
      } else {
        for (j = y; j < y + 4; j++) {
          for (k = x; k < x + 2; k++) {
            map[j][k] = c;
          }
        }
      }
    }
    if (map[sy][sx] != map[gy][gx]) {
      printf("NG\\n");
      continue;
    }
    v.x = sx;
    v.y = sy;
    s.push(v);
    o = 0;
    while (!s.empty()) {
      v = s.top();
      s.pop();
      if (used[v.y][v.x]) {
        continue;
      } else {
        used[v.y][v.x] = 1;
      }
      if (v.x == gx && v.y == gy) {
        o = 1;
        while (!s.empty()) {
          s.pop();
        }
        break;
      }
      for (i = 0; i < 4; i++) {
        u = v;
        u.x += mx[i];
        u.y += my[i];
        if (u.x >= 0 && u.x < w && u.y >= 0 && u.y < h &&
            map[u.y][u.x] == map[sy][sx]) {
          s.push(u);
        }
      }
    }
    if (o) {
      printf("OK\\n");
    } else {
      printf("NG\\n");
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <string.h>
void haichi(void);
int Block(int, int);
int c, d, x, y, xg, yg, w, h, M, Flag;
int MAP[101][101], MF[101][101];
int main(void) {
  int xs, ys, n;
  while (1) {
    Flag = 0;
    memset(MAP, 0, sizeof(MAP));
    memset(MF, 0, sizeof(MF));
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d %d %d %d", &xs, &ys, &xg, &yg);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &c, &d, &x, &y);
      haichi();
    }
    /*for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                    printf("%d",MAP[i][j]);
            }
            printf("\\n");
    }*/
    M = MAP[ys][xs]; //
    if (Block(xs, ys))
      printf("OK\\n");
    else
      printf("NG\\n");
  }
  return 0;
}

void haichi(void) {
  if (d == 0) {
    MAP[y][x] = c;
    MAP[y][x + 1] = c;
    MAP[y][x + 2] = c;
    MAP[y][x + 3] = c;
    MAP[y + 1][x] = c;
    MAP[y + 1][x + 1] = c;
    MAP[y + 1][x + 2] = c;
    MAP[y + 1][x + 3] = c;
  } else if (d == 1) {
    MAP[y][x] = c;
    MAP[y][x + 1] = c;
    MAP[y + 1][x] = c;
    MAP[y + 1][x + 1] = c;
    MAP[y + 2][x] = c;
    MAP[y + 2][x + 1] = c;
    MAP[y + 3][x] = c;
    MAP[y + 3][x + 1] = c;
  }
}

int Block(int X, int Y) {

  // printf("%d",M);
  printf("%d %d\\n", X, Y);
  if (X == xg && Y == yg)
    return 1;
  if (MAP[Y][X] != M || MF[Y][X] == 1 || X < 1 || Y < 1 || X > w || Y > h)
    return 0;
  MF[Y][X] = 1;
  return Block(X, Y - 1) || Block(X, Y + 1) || Block(X - 1, Y) ||
         Block(X + 1, Y);
}