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

#include <iostream>
using namespace std;
#define MAX_H 100
#define MAX_W 100
#define INF 1000;
int h, w;
int field[MAX_H + 1][MAX_W + 1];
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
void dfs(int y, int x, const int sc) {
  if (y <= 0 || x <= 0 || y > h || x > w)
    return;
  if (field[y][x] == sc) {
    field[y][x] = 0;
    for (int i = 0; i < 4; i++)
      dfs(y + dy[i], x + dx[i], sc);
  }
  return;
}
int main() {
  int sx, sy, gx, gy, n;
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> sx >> sy >> gx >> gy >> n;
    for (int i = 0; i < MAX_H; i++) {
      for (int j = 0; j < MAX_W; j++) {
        field[i][j] = INF;
      }
    }
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          if (d)
            field[y + j][x + i] = c;
          else
            field[y + i][x + j] = c;
        }
      }
    }
    dfs(sy, sx, field[sy][sx]);
    if (field[gy][gx])
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX_H 100
#define MAX_W 100
#define INF 1000;
int h, w;
int field[MAX_H][MAX_W];
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
void dfs(int y, int x, const int sc) {
  if (y <= 0 || x <= 0 || y > h || x > w)
    return;
  if (field[y][x] == sc) {
    field[y][x] = 0;
    for (int i = 0; i < 4; i++)
      dfs(y + dy[i], x + dx[i], sc);
  }
  return;
}
int main() {
  int sx, sy, gx, gy, n;
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> sx >> sy >> gx >> gy >> n;
    for (int i = 0; i < MAX_H; i++) {
      for (int j = 0; j < MAX_W; j++) {
        field[i][j] = INF;
      }
    }
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          if (d)
            field[y + j][x + i] = c;
          else
            field[y + i][x + j] = c;
        }
      }
    }
    dfs(sy, sx, field[sy][sx]);
    if (field[gy][gx])
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int W, H, sx, sy, gx, gy, N, c, d, x, y, r[111][111], vis[111][111];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  while (cin >> W >> H, W | H) {
    cin >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    memset(r, 0, sizeof(r));
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> c >> d >> x >> y;
      x--, y--;
      int zx = (d ? 2 : 4), zy = (d ? 4 : 2);
      for (int j = 0; j < zy; j++) {
        for (int k = 0; k < zx; k++) {
          r[y + j][x + k] = c;
        }
      }
    }
    memset(vis, 0, sizeof(vis));
    vis[sy][sx] = 1;
    queue<pair<int, int>> que;
    que.push(make_pair(sx, sy));
    while (!que.empty()) {
      pair<int, int> v = que.front();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int tx = v.first + dx[i], ty = v.second + dy[i];
        if (0 <= tx && tx < W && 0 <= ty && ty < H && !vis[ty][tx] &&
            r[ty][tx] == r[v.second][v.first]) {
          vis[ty][tx] = 1;
          que.push(make_pair(tx, ty));
        }
      }
    }
    cout << (vis[gy][gx] ? "OK" : "NG") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int W, H, sx, sy, gx, gy, N, c, d, x, y, r[111][111], vis[111][111];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  while (cin >> W >> H, W | H) {
    cin >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    memset(r, 0, sizeof(r));
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> c >> d >> x >> y;
      x--, y--;
      int zx = (d ? 2 : 4), zy = (d ? 4 : 2);
      for (int j = 0; j < zy; j++) {
        for (int k = 0; k < zx; k++) {
          r[y + j][x + k] = c;
        }
      }
    }
    memset(vis, 0, sizeof(vis));
    vis[sy][sx] = 1;
    queue<pair<int, int>> que;
    que.push(make_pair(sx, sy));
    while (!que.empty()) {
      pair<int, int> v = que.front();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int tx = v.first + dx[i], ty = v.second + dy[i];
        if (0 <= tx && tx < W && 0 <= ty && ty < H &&
            r[ty][tx] == r[v.second][v.first]) {
          vis[ty][tx] = 1;
          que.push(make_pair(ty, tx));
        }
      }
    }
    cout << (vis[gy][gx] ? "OK" : "NG") << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

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

// bool b;
// int xg , yg;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int board[105][105];
int sc;
/*
void dfs(int x , int y ,int w , int h){
        if(x==xg&&y==yg) {
                b=true;
                return;
        }
        for(int i=0;i<4;++i){
                int tx=x+dx[i];
                int ty=y+dy[i];
                if(tx<=0 || ty<=0 || tx>w || ty>h || board[tx][ty]!=sc ||
board[tx][ty]==0 || b ) continue; dfs( tx , ty , w , h );
        }
}
*/

void dfs(int x, int y, int w, int h) {
  if (x <= 0 || y <= 0 || x > w || y > h)
    return;
  if (board[x][y] == sc) {
    board[x][y] = 10;
    for (int i = 0; i < 4; ++i) {
      dfs(x + dx[i], y + dy[i], w, h);
    }
  }
}
int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int xs, ys, n;
    int xg, yg;
    memset(board, 0, sizeof(board));
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; ++i) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (!d) {
        for (int k = y; k < y + 2; ++k) {
          for (int j = x; j < x + 4; ++j) {
            board[j][k] = c;
          }
        }
      } else {
        for (int k = y; k < y + 4; ++k) {
          for (int j = x; j < x + 2; ++j) {
            board[j][k] = c;
          }
        }
      }
    }
    sc = board[xs][ys];
    dfs(xs, ys, w, h);
    cout << (board[xg][yg] == 10 ? "OK" : "NG") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

bool b;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int board[105][105];
int xg, yg, sc;

void dfs(int x, int y, int w, int h) {
  if (x == xg && y == yg) {
    b = true;
    return;
  }
  for (int i = 0; i < 4; ++i) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx <= 0 || ty <= 0 || tx > w || ty > h || board[tx][ty] != sc ||
        board[tx][ty] == 0 || b)
      continue;
    dfs(tx, ty, w, h);
  }
}

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int xs, ys, n;
    b = false;
    memset(board, 0, sizeof(board));
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; ++i) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (!d) {
        for (int k = y; k < y + 2; ++k) {
          for (int j = x; j < x + 4; ++j) {
            board[j][k] = c;
          }
        }
      } else {
        for (int k = y; k < y + 4; ++k) {
          for (int j = x; j < x + 2; ++j) {
            board[j][k] = c;
          }
        }
      }
    }
    sc = board[xs][ys];
    int gc = board[xg][yg];
    if (sc != gc)
      ;
    else
      dfs(xs, ys, w, h);
    cout << (b ? "OK" : "NG") << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

int w, h, xs, ys, xg, yg;
int mat[102][102];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};

void init() {
  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      mat[i][j] = 0;
    }
  }
}

bool bfs() {
  if (mat[xs][ys] == 0 || mat[xg][yg] == 0)
    return false;

  int c = mat[xs][ys];
  stack<pair<int, int>> s;
  s.push({xs, ys});
  bool visit[102][102] = {};

  while (!s.empty()) {
    pair<int, int> now = s.top();
    s.pop();
    if (now.first == xg && now.second == yg) {
      return true;
    }

    visit[now.first][now.second] = true;

    for (int i = 0; i < 4; i++) {
      int nx = now.first + dx[i];
      int ny = now.second + dy[i];

      if (mat[nx][ny] == c && !visit[nx][ny]) {
        s.push({nx, ny});
      }
    }
  }

  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    cin >> ys >> xs >> yg >> xg;

    init();

    int n;
    cin >> n;

    int c, d, x, y;
    while (n-- > 0) {
      cin >> c >> d >> y >> x;
      if (d == 0) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 4; j++) {
            mat[x + i][y + j] = c;
          }
        }
      } else if (d == 1) {
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            mat[x + i][y + j] = c;
          }
        }
      }
    }

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

  return 0;
}


Error: -11

Fixed submission:

// WA.
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

int w, h, xs, ys, xg, yg;
int mat[102][102];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};

void init() {
  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      mat[i][j] = 0;
    }
  }
}

bool bfs() {
  int c = mat[xs][ys];
  stack<pair<int, int>> s;
  s.push({xs, ys});
  bool visit[102][102] = {};

  while (!s.empty()) {
    pair<int, int> now = s.top();
    s.pop();

    if (now.first == xg && now.second == yg) {
      return true;
    }

    visit[now.first][now.second] = true;

    for (int i = 0; i < 4; i++) {
      int nx = now.first + dx[i];
      int ny = now.second + dy[i];

      if (mat[nx][ny] == c && !visit[nx][ny]) {
        s.push({nx, ny});
      }
    }
  }

  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    cout << w << " " << h << endl;
    if (w + h == 0)
      break;
    cin >> xs >> ys >> xg >> yg;

    init();

    int n;
    cin >> n;

    int c, d, x, y;
    while (n-- > 0) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 4; j++) {
            mat[x + i][y + j] = c;
          }
        }
      } else if (d == 1) {
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            mat[x + i][y + j] = c;
          }
        }
      }
    }

    if (bfs())
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int f[102][102];

void DFS(int x, int y, int c) {
  if (f[x][y] != c)
    return;

  f[x][y] = 10;
  DFS(x + 1, y, c);
  DFS(x - 1, y, c);
  DFS(x, y + 1, c);
  DFS(x, y - 1, c);
}

void print(int w, int h) {
  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= h; j++) {
      cout << f[i][j] << ' ';
    }
    cout << endl;
  }
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  while (true) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        f[i][j] = -1;
      }
    }
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          for (int k = 0; k < 2; k++) {
            f[x + j][y + k] = c;
          }
        }
      } else {
        for (int j = 0; j < 2; j++) {
          for (int k = 0; k < 4; k++) {
            f[x + j][y + k] = c;
          }
        }
      }
    }
    // print(w,h);
    DFS(xs, ys, f[xs][ys]);
    // print(w,h);
    if (f[xg][yg] == 10) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int f[102][102];

void draw(int c, int d, int x, int y) {
  if (d) {
    for (int i = x; i < x + 2; i++) {
      for (int j = y; j < y + 4; y++) {
        f[i][j] = c;
      }
    }
  } else {
    for (int i = x; i < x + 4; i++) {
      for (int j = y; j < y + 2; y++) {
        f[i][j] = c;
      }
    }
  }
}

bool judge(int c, int x, int y) {
  if (c == f[x][y]) {
    return true;
  }
  return false;
}

bool DFS(int xs, int ys, int xg, int yg) {
  if (xs != xg || ys != yg) {
    if (judge(f[xs][ys], xs + 1, ys)) {
      if (DFS(xs + 1, ys, xg, yg)) {
        return true;
      }
    }

    if (judge(f[xs][ys], xs - 1, ys)) {
      if (DFS(xs - 1, ys, xg, yg)) {
        return true;
      }
    }
    if (judge(f[xs][ys], xs, ys + 1)) {
      if (DFS(xs, ys + 1, xg, yg)) {
        return true;
      }
    }
    if (judge(f[xs][ys], xs, ys - 1)) {
      if (DFS(xs, ys, xg, yg - 1)) {
        return true;
      }
    }
    if (!(judge(f[xs][ys], xs + 1, ys) || judge(f[xs][ys], xs - 1, ys) ||
          judge(f[xs][ys], xs, ys + 1) || judge(f[xs][ys], xs, ys - 1))) {
      return false;
    }
  }
  return true;
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c;
  int d, x, y;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      draw(c, d, x, y);
    }
    if (DFS(xs, ys, xg, yg)) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int F[102][102];

void DFS(int Y, int X, int A) {
  if (F[Y][X] != A)
    return;

  F[Y][X] = 7;

  DFS(Y - 1, X, A);
  DFS(Y, X + 1, A);
  DFS(Y + 1, X, A);
  DFS(Y, X - 1, A);
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c, d, x, y;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int a = 0; a < 4; a++) {
          for (int b = 0; b < 2; b++) {
            F[y + b][x + a] = c;
          }
        }

      } else {
        for (int a = 0; a < 2; a++) {
          for (int b = 0; b < 4; b++) {
            F[y + b][x + a] = c;
          }
        }
      }
    }

    if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 ||
        F[ys][xs] == 4 || F[ys][xs] == 5) {
      DFS(ys, xs, F[ys][xs]);
    }

    if (F[yg][xg] == F[ys][xs]) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }

    /*for (int y = 0; y < h; y++)
        {
          for (int x = 0; x < w; x++)
            {
              cout << F[y][x];
            }
         cout << endl;
        }*/

    /*for (int y = 1; y <= h; y++)
        {
          for (int x = 1; x <= w; x++)
            {
              F[y][x] = 0;
            }
        }*/
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int F[102][102];

void DFS(int Y, int X, int A) {
  if (F[Y][X] != A)
    return;

  F[Y][X] = 7;

  DFS(Y - 1, X, A);
  DFS(Y, X + 1, A);
  DFS(Y + 1, X, A);
  DFS(Y, X - 1, A);
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c, d, x, y;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int a = 0; a < 4; a++) {
          for (int b = 0; b < 2; b++) {
            F[y + b][x + a] = c;
          }
        }

      } else {
        for (int a = 0; a < 2; a++) {
          for (int b = 0; b < 4; b++) {
            F[y + b][x + a] = c;
          }
        }
      }
    }

    if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 ||
        F[ys][xs] == 4 || F[ys][xs] == 5) {
      DFS(ys, xs, F[ys][xs]);
    }

    if (F[yg][xg] == F[ys][xs]) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        F[y][x] = 0;
      }
    }
  }

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

#include <bits/stdc++.h>

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

using namespace std;

int field[MAX][MAX];
bool went[MAX][MAX];
int w, h;
int sx, sy, gx, gy;
int scol;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

bool dfs(int x, int y) {
  if (field[y][x] == 0)
    return false;
  if (x == gx && y == gy)
    return true;
  int ncol = field[y][x];
  field[y][x] = 0;
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 < nx && nx <= w && 0 < ny && ny <= h && ncol == field[ny][nx]) {
      /*rep(j, h) {
              rep(k, w) cout << field[j][k] << " ";
              cout << endl;
      }*/
      if (dfs(nx, ny))
        return true;
    }
  }
  return false;
}

int main() {
  while (cin >> w >> h, w || h) {
    memset(field, 0, sizeof(field));
    memset(went, false, sizeof(went));
    int n;
    cin >> sx >> sy >> gx >> gy;
    cin >> n;
    rep(i, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      rep(i, (d ? 4 : 2)) rep(j, (d ? 2 : 4)) field[y + i][x + j] = c;
    }
    /*rep(j, h) {
            rep(k, w) cout << field[j][k] << " ";
            cout << endl;
    }*/
    if (dfs(sx, sy))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

int field[MAX][MAX];
int w, h;
int sx, sy, gx, gy;
int scol;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

bool dfs(int x, int y) {
  field[y][x] = 0;
  if (x == gx && y == gy)
    return true;
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx <= w && nx > 0 && ny <= h && ny > 0) {
      if (field[ny][nx] == scol)
        dfs(nx, ny);
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    memset(field, 0, sizeof(field));
    int n;
    cin >> sx >> sy >> gx >> gy;
    cin >> n;
    rep(i, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      rep(i, (d ? 4 : 2)) rep(j, (d ? 2 : 4)) field[y + i][x + j] = c;
    }
    rep(i, h) {
      rep(j, w) cout << field[i][j] << " ";
      cout << endl;
    }
    scol = field[sy][sx];
    if (dfs(sx, sy))
      cout << "OK" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int a[1000][1000];
int xg, yg, w, h, ans;

void solve(int xs, int ys) {

  if (xs == xg && ys == yg) {
    ans = 1;
    return;
  }

  if (a[ys][xs] != a[yg][xg])
    return;
  a[ys][xs] = 100;

  if (xs + 1 <= w)
    solve(xs + 1, ys);
  if (ys + 1 <= h)
    solve(xs, ys + 1);
  if (xs - 1 > 0)
    solve(xs - 1, ys);
  if (ys - 1 > 0)
    solve(xs, ys - 1);
}

int main() {

  int xs, ys;
  int n;

  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> xs >> ys >> xg >> yg >> n;

    memset(a, 0, sizeof(a));
    ans = 0;

    int c, d, x, y;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          a[y][x + j] = c;
          a[y + 1][x + j] = c;
        }
      }

      else if (d == 1) {
        for (int j = 0; j < 4; j++) {
          a[y + j][x] = c;
          a[y + j][x + 1] = c;
        }
      }
    }

    solve(xs, ys);

    if (ans == 1)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int xs, ys, c, a[105][105], memo[105][105], ans, p, w, h;

int solve(int yg, int xg) {

  if (a[yg][xg] != p)
    return 0;

  a[yg][xg] = 0;

  if (xs == xg && ys == yg)
    return ans = 1;

  else if (yg - 1 > 0 && a[yg - 1][xg] == p)
    solve(yg - 1, xg);
  else if (xg - 1 > 0 && a[yg][xg - 1] == p)
    solve(yg, xg - 1);
  else if (yg + 1 <= w && a[yg + 1][xg] == p)
    solve(yg + 1, xg);
  else if (xg + 1 <= h && a[yg][xg + 1] == p)
    solve(yg, xg + 1);
}

int main() {
  int xg, yg, n, d, x, y;

  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;

    memset(a, 0, sizeof(a));
    memset(memo, 0, sizeof(memo));
    ans = 0;

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int i = y; i < y + 2; i++) {
          for (int j = x; j < x + 4; j++)
            a[i][j] = c;
        }
      }

      else if (d == 1) {
        for (int i = y; i < y + 4; i++) {
          for (int j = x; j < x + 2; j++)
            a[i][j] = c;
        }
      }
    }

    p = a[yg][xg];
    solve(yg, xg);

    if (ans == 1)
      cout << "OK" << endl;
    else if (ans == 0)
      cout << "NG" << endl;
  }
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W;
  while (cin >> W >> H, H || W) {
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    int M;
    cin >> M;
    vector<vector<int>> d(W + 1, vector<int>(H + 1));
    vector<vector<int>> used(W + 1, vector<int>(H + 1));

    for (int i = 0; i < M; i++) {
      int c, dir, x, y;
      cin >> c >> dir >> x >> y;
      if (!dir) {
        for (int j = 0; j < 4; j++) {
          for (int k = 0; k < 2; k++) {
            d[x + j][y + k] = c;
          }
        }
      } else {
        for (int j = 0; j < 2; j++) {
          for (int k = 0; k < 4; k++) {
            d[x + j][y + k] = c;
          }
        }
      }
    }

    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};

    queue<int> qx, qy;
    qx.push(sx);
    qy.push(sy);

    while (qx.size()) {
      int x = qx.front();
      qx.pop();
      int y = qy.front();
      qy.pop();

      if (used[x][y])
        continue;
      used[x][y] = 1;

      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 < nx && nx <= W && 0 < ny && ny <= H && !used[nx][ny] &&
            d[x][y] == d[nx][ny]) {
          qx.push(nx);
          qy.push(ny);
        }
      }
    }

    if (d[gx][gy] > 0 && used[gx][gy])
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W;
  while (cin >> W >> H, H || W) {
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    // sx--, sy--, gx--, gy--;
    int M;
    cin >> M;
    vector<vector<int>> d(H + 1, vector<int>(W + 1));
    vector<vector<int>> used(H + 1, vector<int>(W + 1));

    for (int i = 0; i < M; i++) {
      int c, dir, x, y;
      cin >> c >> dir >> x >> y;
      // x--, y--;
      if (dir) {
        for (int j = 0; j < 2; j++) {
          for (int k = 0; k < 4; k++) {
            d[x + j][y + k] = c;
          }
        }
      } else {
        for (int j = 0; j < 4; j++) {
          for (int k = 0; k < 2; k++) {
            d[x + j][y + k] = c;
          }
        }
      }
    }

    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};

    queue<int> qx, qy;
    qx.push(sx);
    qy.push(sy);

    while (qx.size()) {
      int x = qx.front();
      qx.pop();
      int y = qy.front();
      qy.pop();

      if (used[x][y])
        continue;
      used[x][y] = 1;

      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx <= H && 0 <= ny && ny <= W && !used[nx][ny] &&
            d[x][y] == d[nx][ny]) {
          qx.push(nx);
          qy.push(ny);
        }
      }
    }

    if (used[gx][gy])
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }

  return 0;
}