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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
int needleX[4] = {-1, 0, 1, 0};
int needleY[4] = {0, 1, 0, -1};
bool dfs(int start, int goal, int x, int y, int goalx, int goaly,
         std::vector<std::vector<bool>> &canGo) {
  if (x == goalx && y == goaly) {
    return true;
  }
  if (start == -1 || goal == -1) {
    return false;
  }
  if (start != goal) {
    return false;
  }
  canGo[y][x] = false;
  for (int i = 0; i < 4; i++) {
    int next_x = x + needleX[i];
    int next_y = y + needleY[i];

    if (canGo[next_y][next_x]) {
      bool flag = dfs(start, goal, next_x, next_y, goalx, goaly, canGo);
      if (flag)
        return true;
    }
  }
  return false;
}
int main(void) {
  int h, w, n;
  std::cin >> w >> h;
  std::vector<std::string> ans;
  while (h != 0 || w != 0) {
    int color = -1, orient, leftX, leftY;
    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0));
    std::vector<std::vector<bool>> canGo(h + 2,
                                         std::vector<bool>(w + 2, false));
    std::cin >> n;
    int start = -1, goal = -1;
    for (int i = 0; i < n; i++) {
      std::cin >> color >> orient >> leftX >> leftY;

      if (orient == 0) {
        for (int j = 0; j <= 3; j++) {
          for (int k = 0; k <= 1; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            } else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }

      } else {
        for (int j = 0; j <= 1; j++) {
          for (int k = 0; k <= 3; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            } else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }
      }
    }
    /*for (int i = 0; i <= w; i++) {
        for (int j = 0; j <= h; j++) {
            std::cout << board[i][j] << " ";
        }
        std::cout << std::endl;
    }*/
    if (start != -1) {
      for (int i = 0; i <= h + 1; i++) {
        for (int j = 0; j <= w + 1; j++) {
          if (board[i][j] == start) {
            canGo[i][j] = true;
          }
        }
      }
    }
    bool result = dfs(start, goal, sx, sy, gx, gy, canGo);
    if (result) {
      ans.push_back("OK");
    } else {
      ans.push_back("NG");
    }
    std::cin >> w >> h;
  }
  for (std::string i : ans) {
    std::cout << i << std::endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
int needleX[4] = {-1, 0, 1, 0};
int needleY[4] = {0, 1, 0, -1};
bool dfs(int start, int goal, int x, int y, int goalx, int goaly,
         std::vector<std::vector<bool>> &canGo) {
  if (x == goalx && y == goaly) {
    return true;
  }
  if (start == -1 || goal == -1) {
    return false;
  }
  if (start != goal) {
    return false;
  }
  canGo[y][x] = false;
  for (int i = 0; i < 4; i++) {
    int next_x = x + needleX[i];
    int next_y = y + needleY[i];

    if (canGo[next_y][next_x]) {
      bool flag = dfs(start, goal, next_x, next_y, goalx, goaly, canGo);
      if (flag)
        return true;
    }
  }
  return false;
}
int main(void) {
  int h, w, n;
  std::cin >> w >> h;
  std::vector<std::string> ans;
  while (h != 0 || w != 0) {
    int color = -1, orient, leftX, leftY;
    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0));
    std::vector<std::vector<bool>> canGo(h + 2,
                                         std::vector<bool>(w + 2, false));
    std::cin >> n;
    int start = -1, goal = -1;
    for (int i = 0; i < n; i++) {
      std::cin >> color >> orient >> leftX >> leftY;

      if (orient == 0) {
        for (int j = 0; j <= 3; j++) {
          for (int k = 0; k <= 1; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            } else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }

      } else {
        for (int j = 0; j <= 1; j++) {
          for (int k = 0; k <= 3; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            } else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }
      }
    }
    /*for (int i = 0; i <= w; i++) {
        for (int j = 0; j <= h; j++) {
            std::cout << board[i][j] << " ";
        }
        std::cout << std::endl;
    }*/
    if (start != -1) {
      for (int i = 0; i <= h + 1; i++) {
        for (int j = 0; j <= w + 1; j++) {
          if (board[i][j] == start) {
            canGo[i][j] = true;
          }
        }
      }
    }
    bool result = dfs(start, goal, sx, sy, gx, gy, canGo);
    if (result) {
      ans.push_back("OK");
    } else {
      ans.push_back("NG");
    }
    std::cin >> h >> w;
  }
  for (std::string i : ans) {
    std::cout << i << std::endl;
  }
  return 0;
}