alexjercan/bugnet huggingface dataset:
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int Board[102][102]; // Board[j, x, w][i, y, h]
bool isOK;           // 迷路がOKかNGか
int xs, ys, xg, yg;  // StartとGoalの座標

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true; // Goalなら迷路はOK
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;                   // 初期値をNGに
    for (int i = 0; i < 102; i++) { // Boardの初期化
      for (int j = 0; j < 102; j++)
        Board[j][i] = -1;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2 + (!d * 2); // d=0なら横長
      int hb = 2 + (d * 2);  // d=1なら縦長
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    DFS(Board[xs][ys], xs, ys);

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

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int Board[102][102]; // Board[j, x, w][i, y, h]
bool isOK;           // 迷路がOKかNGか
int xs, ys, xg, yg;  // StartとGoalの座標

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true; // Goalなら迷路はOK
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;                   // 初期値をNGに
    for (int i = 0; i < 102; i++) { // Boardの初期化
      for (int j = 0; j < 102; j++)
        Board[j][i] = 0;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2 + (!d * 2); // d=0なら横長
      int hb = 2 + (d * 2);  // d=1なら縦長
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    DFS(Board[xs][ys], xs, ys);

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

  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int Board[102][102]; // Board[j, x, w][i, y, h]
bool isOK;
int xs, ys, xg, yg;

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true;
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

// void Showboard(int w, int h){
//     for(int i=0;i<=h;i++){
//         for(int j=0;j<=w;j++)
//             cout << Board[j][i] << " ";
//         cout << endl;
//     }
//     cout << endl;
// }

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++)
        Board[j][i] = 9;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2 + (!d * 2);
      int hb = 2 + (d * 2);
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    // Showboard(w, h);

    DFS(Board[xs][ys], xs, ys);

    // Showboard(w, h);

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

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int Board[102][102]; // Board[j, x][i, y]
bool isOK;
int xs, ys, xg, yg;

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true;
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++)
        Board[j][i] = 0;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2, hb = 2;
      d ? hb : wb = 4;
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    DFS(Board[xs][ys], xs, ys);

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

  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ck(a, b) 0 < a &&a <= b
using namespace std;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int w, h, xs, ys, xg, yg, n, c, d, x, y, f[101][101];
bool dfs(int y, int x) {
  f[y][x] = 0;
  if (y == yg && x == xg)
    return 1;
  bool r = 0;
  rep(d, 4) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ck(ny, h) && ck(nx, w) && f[ny][nx] == c)
      r = r || dfs(ny, nx);
  }
  return r;
}
int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) f[i + 1][j + 1] = 0;
    cin >> xs >> ys >> xg >> yg >> n;
    rep(i, n) {
      cin >> c >> d >> x >> y;
      rep(j, (d ? 2 : 4)) rep(k, (d ? 4 : 2)) f[y + k][x + j] = c;
    }
    c = f[ys][xs];
    cout << (c > 0 && dfs(ys, xs) ? "OK" : "NG") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ck(a, b) 0 <= a &&a < b
using namespace std;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int w, h, xs, ys, xg, yg, n, c, d, x, y, f[100][100];
bool dfs(int y, int x) {
  f[y][x] = 0;
  if (y == yg && x == xg)
    return 1;
  bool r = 0;
  rep(d, 4) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ck(ny, h) && ck(nx, w) && f[ny][nx] == c)
      r = r || dfs(ny, nx);
  }
  return r;
}
int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) f[i][j] = 0;
    cin >> xs >> ys >> xg >> yg >> n;
    rep(i, n) {
      cin >> c >> d >> x >> y;
      rep(j, (d ? 2 : 4)) rep(k, (d ? 4 : 2)) f[y + k][x + j] = c;
    }
    c = f[ys][xs];
    cout << (c > 0 && dfs(ys, xs) ? "OK" : "NG") << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int mass[102][102];
int gx, gy;
bool goal_flag;
void DFS(int x, int y, int c) {
  if (mass[y][x] != c || mass[y][x] == 0)
    return;
  if (x == gx && y == gy) {
    goal_flag = true;
    return;
  }
  mass[y][x] = 0;
  DFS(x + 1, y, c);
  DFS(x - 1, y, c);
  DFS(x, y + 1, c);
  DFS(x, y - 1, c);
}

int main() {
  int h, w;
  int start_x, start_y;

  int n;
  int c, d, x, y;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> start_x >> start_y;
    cin >> gx >> gy;
    cin >> n;
    for (int i = 0; i < h + 2; i++) {
      for (int j = 0; j < w + 2; j++) {
        mass[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 4; k++) {
          if (d == 0) {
            mass[y + j][x + k] = c;
          } else if (d == 1) {
            mass[y + k][x + j] = c;
          }
        }
      }
    }
    goal_flag = false;
    DFS(start_x, start_y, mass[start_y][start_x]);
    if (goal_flag)
      cout << "OK";
    else
      cout << "NG";
    cout << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

struct board {
  int mass[102][102];
};
int gx, gy;
bool goal_flag;
void DFS(int x, int y, board b, int c) {

  if (b.mass[y][x] != c || b.mass[y][x] == 0)
    return;
  if (x == gx && y == gy) {
    goal_flag = true;
    return;
  }
  b.mass[y][x] = 0;
  DFS(x + 1, y, b, c);
  DFS(x - 1, y, b, c);
  DFS(x, y + 1, b, c);
  DFS(x, y - 1, b, c);
}

int main() {
  int h, w;
  int start_x, start_y;

  int n;
  int c, d, x, y;
  board b;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> start_x >> start_y;
    cin >> gx >> gy;
    cin >> n;
    for (int i = 0; i < h + 2; i++) {
      for (int j = 0; j < w + 2; j++) {
        b.mass[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 4; k++) {
          if (d == 0) {
            b.mass[y + j][x + k] = c;
          } else if (d == 1) {
            b.mass[y + k][x + j] = c;
          }
        }
      }
    }
    goal_flag = false;
    DFS(start_x, start_y, b, b.mass[start_y][start_x]);
    if (goal_flag)
      cout << "OK";
    else
      cout << "NG";
    cout << endl;
  }
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int w, h, xs, ys, n, xg, yg, c, d, x, y, area[105][105], c_area[105][105];
int x_p[4] = {-1, 0, 1, 0}, y_p[4] = {0, -1, 0, 1};

bool flag = false;
void solve(int x, int y, int c);

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = y; j < y + 2; j++) {
          for (int k = x; k < x + 4; k++) {
            area[k][j] = c;
          }
        }
      } else {
        for (int j = y; j < y + 4; j++) {
          for (int k = x; k < x + 2; k++) {
            area[k][j] = c;
          }
        }
      }
    }
    if (area[xs][ys] != area[xg][yg])
      ;
    else {
      solve(xs, ys, area[xs][ys]);
    }
    if (flag == true)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
    flag = false;
    fill(area[0], area[104], 0);
  }

  return 0;
}

void solve(int x, int y, int c) {

  if (x == xg && y == yg) {
    flag = true;
    return;
  } else {
    for (int i = 0; i < 4; i++) {
      if (area[x + x_p[i]][y + y_p[i]] == c) {
        // c_area[x][y]=area[x][y];
        area[x][y] = 0;
        solve(x + x_p[i], y + y_p[i], c);
        // area[x][y]=c_area[x][y];
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int w, h, xs, ys, n, xg, yg, c, d, x, y, area[105][105], c_area[105][105];
int x_p[4] = {-1, 0, 1, 0}, y_p[4] = {0, -1, 0, 1};

bool flag = false;
void solve(int x, int y, int c);

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = y; j < y + 2; j++) {
          for (int k = x; k < x + 4; k++) {
            area[k][j] = c;
          }
        }
      } else {
        for (int j = y; j < y + 4; j++) {
          for (int k = x; k < x + 2; k++) {
            area[k][j] = c;
          }
        }
      }
    }
    if (area[xs][ys] != area[xg][yg])
      ;
    else {
      solve(xs, ys, area[xs][ys]);
    }
    if (flag == true)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
    flag = false;
    fill(area[0], area[104], 0);
  }

  return 0;
}

void solve(int x, int y, int c) {

  if (x == xg && y == yg) {
    flag = true;
    return;
  } else {
    for (int i = 0; i < 4; i++) {
      if (area[x + x_p[i]][y + y_p[i]] == c) {
        c_area[x][y] = area[x][y];
        area[x][y] = 0;
        solve(x + x_p[i], y + y_p[i], c);
        area[x][y] = c_area[x][y];
      }
    }
  }
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
using namespace std;

int map[100][100];
bool visited[100][100];
int w, h;
int xs, ys;
int xg, yg;
int n;

int dfs(int x, int y) {
  static int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  if (x == xg && y == yg)
    return 1;
  visited[x][y] = true;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && map[x][y] == map[nx][ny] &&
        !visited[nx][ny]) {
      if (dfs(nx, ny))
        return 1;
    }
  }
  return 0;
}

int main() {
  int c, d, x, y;

  while (1) {
    memset(map, 0, sizeof(map));
    memset(visited, 0, sizeof(visited));
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d %d", &xs, &ys);
    xs--;
    ys--;
    scanf("%d %d", &xg, &yg);
    xg--;
    yg--;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &c, &d, &x, &y);
      x--;
      y--;
      int X, Y;
      if (d == 1) {
        X = 2;
        Y = 4;
      } else {
        X = 4;
        Y = 2;
      }
      for (int j = 0; j < X; j++) {
        for (int k = 0; k < Y; k++) {
          map[x + j][y + k] = c;
        }
      }
    }
    printf("%s\\n", dfs(xs, ys) ? "OK" : "NG");
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

int map[100][100];
int w, h;
int xs, ys;
int xg, yg;
int n;
int c, d, x, y;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int dfs(int x, int y, int cl) {
  if (x == xg - 1 && y == yg - 1)
    return 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] == cl) {
      if (dfs(nx, ny, cl))
        return 1;
    }
  }
  return 0;
}

int main() {
  while (1) {
    memset(map, 0, sizeof(map));
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d %d", &xs, &ys);
    scanf("%d %d", &xg, &yg);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &c, &d, &x, &y);
      int dx, dy;
      if (d)
        dx = 2, dy = 4;
      else
        dx = 4, dy = 2;
      for (int i = 0; i < dx; i++) {
        for (int j = 0; j < dy; j++) {
          map[x - 1 + i][y - 1 + j] = c;
        }
      }
    }
    if (map[xs - 1][ys - 1] != map[xg - 1][yg - 1]) {
      printf("NG\\n");
    } else {
      printf("%s\\n", dfs(xs - 1, ys - 1, c) ? "OK" : "NG");
    }
  }
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for (int i = j; i < k; ++i)

using namespace std;

int w, h, xs, ys, xg, yg, n, c, d, x, y, b[101][101];
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};

bool dfs(int x, int y) {

  int nx, ny;
  b[x][y] = 0;
  if (x == xg && y == yg)
    return true;
  bool r = 0;
  rep(i, 4) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx > 0 && nx <= h && ny > 0 && ny <= w && b[nx][ny] == c)
      r = r || dfs(nx, ny);
  }
  return r;
}

int main() {
  while (scanf("%d %d", &w, &h) && w && h) {
    memset(b, 0, sizeof(b));
    scanf("%d%d%d%d", &ys, &xs, &yg, &xg);
    scanf("%d", &n);
    rep(i, n) {
      scanf("%d%d%d%d", &c, &d, &y, &x);
      rep(j, (d ? 4 : 2)) rep(k, (d ? 2 : 4)) b[x + j][y + k] = c;
    }
    /*    rep(i, h){
      rep(j, w) printf("%d ", b[i][j]);
      puts("");
      }*/
    c = b[xs][ys];
    //    printf("%d\\n", c);
    //    fprintf(stderr, "error\\n");
    if (c > 0 && dfs(xs, ys))
      puts("OK");
    else
      puts("NG");
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for (int i = j; i < k; ++i)

using namespace std;

int w, h, xs, ys, xg, yg, n, c, d, x, y, b[101][101];
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};

bool dfs(int x, int y) {
  int nx, ny;
  b[x][y] = 0;
  if (x == xg && y == yg)
    return true;
  bool r = 0;
  rep(i, 4) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx > 0 && nx <= h && ny > 0 && ny <= w && b[nx][ny] == c)
      r = r || dfs(nx, ny);
  }
  return r;
}

int main() {
  while (scanf("%d %d", &w, &h) && w && h) {
    memset(b, 0, sizeof(b));
    scanf("%d%d%d%d", &ys, &xs, &yg, &xg);
    scanf("%d", &n);
    rep(i, n) {
      scanf("%d%d%d%d", &c, &d, &y, &x);
      rep(j, (d ? 4 : 2)) rep(k, (d ? 2 : 4)) b[x + j][y + k] = c;
    }

    c = b[xs][ys];
    if (dfs(xs, ys))
      puts("OK");
    else
      puts("NG");
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef pair<int, int> P;

int main() {
  int w, h;
  while (cin >> w >> h, w, h) {
    char board[114][514];
    int pass[114][514];

    int xs, ys, xg, yg;
    cin >> xs >> ys >> xg >> yg;

    for (int i = 0; i <= 110; i++) {
      for (int j = 0; j <= 110; j++) {
        pass[i][j] = INF;
        board[i][j] = '#';
      }
    }
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      char a;
      int b, c, d;
      cin >> a >> b >> c >> d;

      if (b == 0) {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 4; k++)
            board[d + j][c + k] = a;
      } else {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 4; k++)
            board[d + k][c + j] = a;
      }
    }

    queue<P> Q;
    Q.push(P(ys, xs));
    pass[ys][xs] = 0;

    while (Q.size()) {
      P p = Q.front();
      Q.pop();

      if (p.first == yg && p.second == xg)
        break;

      int dir[2][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}};
      for (int i = 0; i < 4; i++) {
        int ny = p.first + dir[0][i];
        int nx = p.second + dir[1][i];

        if (0 <= ny && ny <= h && 0 <= nx && nx <= w &&
            board[ny][nx] == board[ys][xs] && pass[ny][nx] == INF) {
          Q.push(P(ny, nx));
          pass[ny][nx] = pass[p.first][p.second] + 1;
        }
      }
    }

    if (pass[yg][xg] != INF)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef pair<int, int> P;

int main() {
  int w, h;
  while (cin >> w >> h, w, h) {
    char board[114][514];
    int pass[114][514];

    int xs, ys, xg, yg;
    cin >> xs >> ys >> xg >> yg;

    for (int i = 0; i <= w; i++) {
      for (int j = 0; j <= h; j++) {
        pass[i][j] = INF;
        board[i][j] = '#';
      }
    }
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      char a;
      int b, c, d;
      cin >> a >> b >> c >> d;

      if (b == 0) {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 4; k++)
            board[d + j][c + k] = a;
      } else {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 4; k++)
            board[d + k][c + j] = a;
      }
    }

    queue<P> Q;
    Q.push(P(ys, xs));
    pass[ys][xs] = 0;

    while (Q.size()) {
      P p = Q.front();
      Q.pop();

      if (p.first == yg && p.second == xg)
        break;

      int dir[2][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}};
      for (int i = 0; i < 4; i++) {
        int ny = p.first + dir[0][i];
        int nx = p.second + dir[1][i];

        if (0 <= ny && ny <= h && 0 <= nx && nx <= w &&
            board[ny][nx] == board[ys][xs]) {
          Q.push(P(ny, nx));
          pass[ny][nx] = pass[p.first][p.second] + 1;
        }
      }
    }

    if (pass[yg][xg] != INF)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
const int MAX_W = 100, MAX_H = 100;

typedef std::pair<int, int> P;
int map[MAX_H + 1][MAX_W + 1];

int bfs(P sp, P gp, int w, int h, int c) {
  // ½±Æª é©ði[
  int f[MAX_H + 1][MAX_W + 1] = {{0}};
  std::queue<P> q;
  q.push(P(sp.first, sp.second));

  while (q.size()) {
    P p = q.front();
    q.pop();
    // S[Å«½ç¬÷
    if (p.first == gp.first && p.second == gp.second &&
        map[p.second][p.first] == c)
      return 1;

    int vy[4] = {-1, 1, 0, 0}, vx[4] = {0, 0, -1, 1};
    for (int i = 0; i < 4; i++) {
      int nx = p.first + vx[i], ny = p.second + vy[i];
      if (nx >= 0 && nx <= w && ny >= 0 && ny <= h && map[ny][nx] == c &&
          !f[ny][nx]) {
        q.push(P(nx, ny));
        f[ny][nx] = 1;
      }
    }
  }

  // ÅãÜÅS[Å«È¢Èç¸s
  return 0;
}

int main() {
  int w, h, xs, ys, xg, yg;
  while (std::cin >> w >> h, w && h) {
    memset(map, 0, sizeof(map));
    std::cin >> xs >> ys;
    std::cin >> xg >> yg;

    int n;
    std::cin >> n;

    // ubNÌÝu
    while (n--) {
      int c, d, x, y;
      std::cin >> c >> d >> x >> y;

      int mx, my;
      if (d) // w < h
      {
        my = 4, mx = 2;
      } else // w > h
      {
        mx = 4, my = 2;
      }
      for (int i = y; i < y + my; i++) {
        for (int j = x; j < x + mx; j++) {
          map[i][j] = c;
        }
      }
    }

    // bfsÅS[Å«é©mF
    P sp = P(xs, ys), gp = P(xg, yg);
    int c = map[ys][xs];
    if (bfs(sp, gp, w, h, c))
      std::cout << "OK" << std::endl;
    else {
      std::cout << "NG" << std::endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
int m[101][101], w, h, r, gx, gy;
void dfs(int x, int y, int c) {
  if (!(x - gx) && !(y - gy))
    r = 1;
  if (x > 1 && m[x - 1][y] == c)
    m[x - 1][y] = 0, dfs(x - 1, y, c);
  if (x <= w && m[x + 1][y] == c)
    m[x + 1][y] = 0, dfs(x + 1, y, c);
  if (y > 1 && m[x][y - 1] == c)
    m[x][y - 1] = 0, dfs(x, y - 1, c);
  if (y <= h && m[x][y + 1] == c)
    m[x][y + 1] = 0, dfs(x, y + 1, c);
  return;
}

int main() {
  // whÍ{[hÌå«³AnÍubNÌÂ
  int n, c, d, sx, sy, bx, by;

  while (std::cin >> w >> h, w && h) {
    for (int y = 0; y < 101; y++) {
      for (int x = 0; x < 101; x++) {
        m[x][y] = 0;
      }
    }
    r = 0;
    std::cin >> sx >> sy;
    std::cin >> gx >> gy;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
      std::cin >> c >> d >> bx >> by;
      int bw, bh;
      d ? bw = 2, bh = 4 : bh = 2, bw = 4;
      for (int y = by; y <= by + bh; y++) {
        for (int x = bx; x <= bx + bw; x++) {
          m[x][y] = c;
        }
      }
    }
    if (m[sx][sy])
      dfs(sx, sy, m[sx][sy]), m[sx][sy] = 0;
    if (r)
      std::cout << "OK" << std::endl;
    else {
      std::cout << "NG" << std::endl;
    }
  }
  return 0;
}