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

#include <iostream>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

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

void put_b(int a[][105]) {
  int X, Y;
  if (d == 0)
    X = 4, Y = 2;
  else
    X = 2, Y = 4;
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      a[y + i][x + j] = c;
}

bool bfs(int a[][105]) {
  int cs = a[ys][xs];
  if (cs != a[yg][xg])
    return false;

  // cout << ys << xs << endl;
  queue<P> que;
  que.push(P(xs, ys));
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == xg && p.second == yg)
      return true;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 < nx && nx <= w && 0 < ny && ny <= h && a[ny][nx] == cs) {
        que.push(P(nx, ny));
        a[ny][nx] = 0;
      }
    }
  }
  return false;
}
int main() {
  while (cin >> w >> h, w != 0 || h != 0) {
    int a[105][105] = {};
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    while (n--) {
      cin >> c >> d >> x >> y;
      put_b(a);
    }
    /*
     for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++)
      cout << a[i][j];
      cout << endl;
      }
    */
    if (bfs(a) == true)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

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

void put_b(int a[][101]) {
  int X, Y;
  if (d == 0)
    X = 4, Y = 2;
  else
    X = 2, Y = 4;
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      a[y + i][x + j] = c;
}

bool bfs(int a[][101]) {
  int cs = a[ys][xs];
  if (cs != a[yg][xg])
    return false;
  queue<P> que;
  que.push(P(xs, ys));
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == xg && p.second == yg)
      return true;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h && a[ny][nx] == cs)
        que.push(P(nx, ny));
    }
  }
  return false;
}
int main() {
  while (cin >> w >> h, w != 0 || h != 0) {
    int a[101][101] = {};
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    while (n--) {
      cin >> c >> d >> x >> y;
      put_b(a);
    }
    /*
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++)
        cout << a[i][j];
      cout << endl;
      }*/
    if (bfs(a) == true)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i < (k); i++)
typedef long long ll;
int stage[128][128];
bool mem[128][128];
int xg, yg;
int w, h;
void dfs(int x, int y, int col) {
  if (x < 1 || y < 1 || x > w || y > h)
    return;
  if (mem[y][x])
    return;
  if (stage[y][x] != col)
    return;
  mem[y][x] = true;
  rep(i, 4) {
    if (col == stage[y + dy[i]][x + dx[i]]) {
      dfs(x + dx[i], y + dy[i], col);
    }
  }
  return;
}
int main() {
  while (scanf("%d%d", &w, &h), w | h) {
    memset(stage, 0, sizeof(stage));
    rep(i, 128) rep(j, 128) mem[i][j] = false;
    int xs, ys;
    scanf("%d%d", &xs, &ys);
    scanf("%d%d", &xg, &yg);

    int n;
    scanf("%d", &n);

    rep(q, n) {
      int c, d, x, y;
      scanf("%d%d%d%d", &c, &d, &x, &y);
      if (d == 0) {
        reps(i, x, x + 4) {
          reps(j, y, y + 2) { stage[j][i] = c; }
        }
      } else {
        reps(i, x, x + 2) {
          reps(j, y, y + 4) { stage[j][i] = c; }
        }
      }
    }
    dfs(xs, ys, stage[ys][xs]);
    bool f = mem[yg][xg];
    (f) ? puts("OK") : puts("NG");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i < (k); i++)
typedef long long ll;
int stage[128][128];
int xg, yg;
int w, h;
bool dfs(int x, int y, int col) {
  if (x == xg && y == yg && stage[x][y] == col) {
    return true;
  }
  rep(i, 4) {
    if (stage[x + dx[i]][y + dy[i]] == 0)
      continue;
    if (col == stage[x + dx[i]][y + dy[i]]) {
      return dfs(x + dx[i], y + dy[i], col);
    }
  }
  return false;
}
int main() {
  while (scanf("%d%d", &w, &h), w | h) {
    memset(stage, 0, sizeof(stage));
    int xs, ys;
    scanf("%d%d", &xs, &ys);
    scanf("%d%d", &xg, &yg);

    int n;
    scanf("%d", &n);

    rep(q, n) {
      int c, d, x, y;
      scanf("%d%d%d%d", &c, &d, &x, &y);
      if (d == 0) {
        reps(i, x, x + 4) {
          reps(j, y, y + 2) { stage[j][i] = c; }
        }

      } else {
        reps(i, x, x + 2) {
          reps(j, y, y + 4) { stage[j][i] = c; }
        }
      }
    }
    (dfs(xs, ys, stage[xs][ys])) ? puts("OK") : puts("NG");
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int map[110][110];
int com[110][110];
int lx, ly, sx, sy, gx, gy;
int py[4] = {1, 0, -1, 0};
int px[4] = {0, 1, 0, -1};
int fl;
int mei(int zx, int zy, int cei[][110]) {
  int i;
  if (zx == gx && zy == gy) {
    fl = 1;
    return 0;
  }
  cei[zy][zx] = 1;
  for (i = 0; i < 4; i++) {
    if (cei[zy + py[i]][zx + px[i]] == 0 &&
        map[sy][sx] == map[zy + py[i]][zx + px[i]]) {
      mei(zx + px[i], zy + py[i], cei);
      if (fl)
        return 0;
    }
  }
  return 0;
}
int main() {
  int n, col, muk, vx, vy;
  int i, j, t;
  while (cin >> lx >> ly, lx != 0 && ly != 0) {
    fl = 0;
    cin >> sx >> sy >> gx >> gy >> n;
    memset(map, 0, sizeof(map));
    memset(com, 0, sizeof(com));
    for (i = 0; i < n; i++) {
      cin >> col >> muk >> vx >> vy;
      if (muk == 0) {
        for (j = vy; j < 2 + vy; j++) {
          for (t = vx; t < 4 + vx; t++) {
            map[j][t] = col;
          }
        }
      } else {
        for (j = vy; j < 4 + vy; j++) {
          for (t = vx; t < 2 + vx; t++) {
            map[j][t] = col;
          }
        }
      }
    }
    if (map[sy][sx] != map[gy][gx]) {
      cout << "NG" << endl;
      continue;
    }
    mei(sx, sy, com);
    if (fl)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int map[110][110];
int com[110][110];
int lx, ly, sx, sy, gx, gy;
int py[4] = {1, 0, -1, 0};
int px[4] = {0, 1, 0, -1};
int fl;
int mei(int zx, int zy, int cei[][110]) {
  int i;
  if (zx == gx && zy == gy) {
    fl = 1;
    return 0;
  }
  cei[zy][zx] = 1;
  for (i = 0; i < 4; i++) {
    if (cei[zy + py[i]][zx + px[i]] == 0 &&
        map[sy][sx] == map[zy + py[i]][zx + px[i]]) {
      mei(zx + px[i], zy + py[i], cei);
      if (fl)
        return 0;
    }
  }
  cei[zy][zx] = 0;
  return 0;
}
int main() {
  int n, col, muk, vx, vy;
  int i, j, t;
  while (cin >> lx >> ly, lx != 0 && ly != 0) {
    fl = 0;
    cin >> sx >> sy >> gx >> gy >> n;
    memset(map, 0, sizeof(map));
    memset(com, 0, sizeof(com));
    for (i = 0; i < n; i++) {
      cin >> col >> muk >> vx >> vy;
      if (muk == 0) {
        for (j = vy; j < 2 + vy; j++) {
          for (t = vx; t < 4 + vx; t++) {
            map[j][t] = col;
          }
        }
      } else {
        for (j = vy; j < 4 + vy; j++) {
          for (t = vx; t < 2 + vx; t++) {
            map[j][t] = col;
          }
        }
      }
    }
    if (map[sy][sx] != map[gy][gx]) {
      cout << "NG" << endl;
      continue;
    }
    mei(sx, sy, com);
    if (fl)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Runtime Error

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

bool dfs(int x, int y, int color);
int w, h;
int xs, ys;
int xg, yg;
int board[111][111];
int cheak[111][111];
int xd[] = {0, -1, 0, 1};
int yd[] = {-1, 0, 1, 0};
int main() {
  int n;
  int c, d, x, y;
  int a[] = {1, 0, 1, 0, 1, 0, 1};
  int b[] = {0, 1, 1, 2, 2, 3, 3};
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    memset(board, 0, sizeof(board));
    memset(cheak, 0, sizeof(cheak));
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    xs--, ys--, xg--, yg--;
    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      x--, y--;
      board[y][x] = c;
      for (int j = 0; j < 7; j++) {
        if (d == 0)
          board[y + a[j]][x + b[j]] = c;
        else
          board[y + b[j]][x + a[j]] = c;
      }
    }
    if (board[ys][xs] != 0) {
      if (dfs(xs, ys, board[ys][xs]) == true)
        cout << "OK" << endl;
      else
        cout << "NG" << endl;
    } else
      cout << "NG" << endl;
  }
}

bool dfs(int x, int y, int color) {
  board[y][x] = 0;
  if (x == xg && y == yg)
    return true;
  for (int i = 0; i < 4; i++) {
    int xn = x + xd[i];
    int yn = y + yd[i];
    if (xn >= 0 && xn < w && yn >= 0 && yn < h && board[yn][xn] == color &&
        board[yn][xn] != 0) {
      if (dfs(xn, yn, board[yn][xn]) == true)
        return true;
    }
  }
  return false;
}

Error: WA

Fixed submission:

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

bool dfs(int x, int y);
int w, h;
int xs, ys;
int xg, yg;
int board[111][111];
int cheak[111][111];
int xd[] = {0, -1, 0, 1};
int yd[] = {-1, 0, 1, 0};
int main() {
  int n;
  int c, d, x, y;
  int a[] = {1, 0, 1, 0, 1, 0, 1};
  int b[] = {0, 1, 1, 2, 2, 3, 3};
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        board[i][j] = 0;
        cheak[i][j] = 0;
        cout << board[i][j];
      }
      cout << endl;
    }
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      board[y - 1][x - 1] = c;
      for (int j = 0; j < 7; j++) {
        if (d == 0)
          board[y + a[j] - 1][x + b[j] - 1] = c;
        else
          board[y + b[j] - 1][x + a[j] - 1] = c;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cout << board[i][j];
      }
      cout << endl;
    }
    if (dfs(xs - 1, ys - 1) == true)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

bool dfs(int x, int y) {
  cheak[y][x] = 1;
  if (x == xg - 1 && y == yg - 1)
    return true;
  for (int i = 0; i < 4; i++) {
    int xn = x + xd[i];
    int yn = y + yd[i];
    if (x >= 0 && x < w && y >= 0 && y < h && board[yn][xn] == board[ys][xs] &&
        cheak[yn][xn] == 0) {
      if (dfs(xn, yn) == true)
        return true;
    }
  }
  return false;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int w, h, xs, ys, xg, yg, n, cc;
int field[100][100];
bool flag;

void dfs(int x, int y, int c) {
  if (flag)
    return;
  if (x == xg && y == yg) {
    flag = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) {
      field[nx][ny] = 0;
      dfs(nx, ny, c);
    }
  }
}

int main() {
  while (1) {
    flag = false;
    cin >> w >> h;
    if (!w)
      break;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++)
        field[i][j] = 0;
    }
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 1) {
        for (int k = 0; k < 2; k++) {
          for (int j = 0; j < 4; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      } else {
        for (int k = 0; k < 4; k++) {
          for (int j = 0; j < 2; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      }
    }
    if (field[xs][ys] == 0) {
      cout << "NG" << endl;
      continue;
    }
    cc = field[xs][ys];
    field[xs][ys] = 0;
    dfs(xs, ys, cc);
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int w, h, xs, ys, xg, yg, n, cc;
int field[100][100];
bool flag;

void dfs(int x, int y, int c) {
  if (flag)
    return;
  if (x == xg && y == yg) {
    flag = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) {
      field[nx][ny] = 0;
      dfs(nx, ny, c);
      field[nx][ny] = c;
    }
  }
}

int main() {
  while (1) {
    flag = false;
    cin >> w >> h;
    if (!w)
      break;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++)
        field[i][j] = 0;
    }
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 1) {
        for (int k = 0; k < 2; k++) {
          for (int j = 0; j < 4; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      } else {
        for (int k = 0; k < 4; k++) {
          for (int j = 0; j < 2; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      }
    }
    if (field[xs][ys] == 0) {
      cout << "NG" << endl;
      continue;
    }
    cc = field[xs][ys];
    field[xs][ys] = 0;
    dfs(xs, ys, cc);
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int BA = 1000;
const int INFTY = (1 << 21);
int xg, yg, w, h, gc;
int maps[101][101];
int solve(int ys, int xs);
int main() {
  int tate[2] = {2, 4};
  int yoko[2] = {4, 2};
  int xs, ys, n;
  int c[101], d[101], x[101], y[101];
  while (1) {
    fill(maps[0], maps[101], 0);
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
      x[i]--;
      y[i]--;
    }
    for (int i = 0; i < n; i++) {
      for (int j = y[i]; j < y[i] + tate[d[i]]; j++) {
        for (int k = x[i]; k < x[i] + yoko[d[i]]; k++) {
          if (j == ys && k == xs)
            gc = c[i];
          maps[j][k] = c[i];
        }
      }
    }
    /* for(int i=0;i<h;i++){
       for(int j=0;j<w;j++){
       cout << maps[i][j];
       }
       cout << endl;
       }*///マップ確認用
    if (solve(ys, xs))
      cout << "OK\\n";
    else
      cout << "NG\\n";
  }
}
int solve(int ys, int xs) {
  if (xs < 0 || ys < 0 || xs > w - 1 || ys > h - 1 || maps[ys][xs] == 0 ||
      maps[ys][xs] != gc) {
    return 0;
  } else if (ys == yg && xs == xg) {
    return 1;
  }
  maps[ys][xs] = 0;
  return solve(ys + 1, xs) + solve(ys, xs + 1) + solve(ys - 1, xs) +
         solve(ys, xs - 1);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int BA = 1000;
const int INFTY = (1 << 21);
int xg, yg, w, h;
int maps[101][101];
int solve(int xs, int ys);
int main() {
  int tate[2] = {2, 4};
  int yoko[2] = {4, 2};
  int xs, ys, n;
  int c[101], d[101], x[101], y[101];
  while (1) {
    fill(maps[0], maps[101], 0);
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = y[i]; j < y[i] + tate[d[i]]; j++) {
        for (int k = x[i]; k < x[i] + yoko[d[i]]; k++) {
          maps[j - 1][k - 1] = c[i];
        }
      }
    }
    /* for(int i=0;i<h;i++){
       for(int j=0;j<w;j++){
       cout << maps[i][j];
       }
       cout << endl;
       }*///マップ確認用
    if (solve(xs - 1, ys - 1))
      cout << "OK\\n";
    else
      cout << "NG\\n";
  }
}
int solve(int xs, int ys) {
  if (xs < 0 || ys < 0 || xs > w - 1 || ys > h - 1 || maps[xs][ys] == 0) {
    return 0;
  } else if (maps[xs][ys] == maps[xg - 1][yg - 1]) {
    return 1;
  }
  if (maps[xs - 1][ys] == maps[xs][ys]) {
    return solve(xs - 1, ys);
  }
  if (maps[xs][ys - 1] == maps[xs][ys]) {
    return solve(xs, ys - 1);
  }
  if (maps[xs + 1][ys] == maps[xs][ys]) {
    return solve(xs + 1, ys);
  }
  if (maps[xs][ys + 1] == maps[xs][ys]) {
    return solve(xs, ys + 1);
  }
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int block[100][100];
int xs, ys, xg, yg, n, w, h;

bool dfs(int x, int y, int c) {
  if (block[x][y] != c || block[x][y] == 0)
    return false;
  if (x == xg && y == yg)
    return true;
  bool b = false;
  block[x][y] = 0;
  if (x < w - 1)
    b |= dfs(x + 1, y, c);
  if (x > 0 && !b)
    b |= dfs(x - 1, y, c);
  if (y < h - 1 && !b)
    b |= dfs(x, y + 1, c);
  if (y > 0 && !b)
    b |= dfs(x, y - 1, c);
  return b;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
      if (d == 0)
        x += 2;
      else
        y += 2;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
    }
    if (dfs(xs, ys, block[xs][ys]))
      cout << "OK\\n";
    else
      cout << "NG\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int block[100][100];
int xs, ys, xg, yg, n, w, h;

bool dfs(int x, int y, int c) {
  if (block[x][y] != c || block[x][y] == 0)
    return false;
  if (x == xg && y == yg)
    return true;
  bool b = false;
  block[x][y] = 0;
  if (x < w - 1)
    b |= dfs(x + 1, y, c);
  if (x > 0 && !b)
    b |= dfs(x - 1, y, c);
  if (y < h - 1 && !b)
    b |= dfs(x, y + 1, c);
  if (y > 0 && !b)
    b |= dfs(x, y - 1, c);
  block[x][y] = c;
  return b;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
      if (d == 0)
        x += 2;
      else
        y += 2;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
    }
    if (dfs(xs, ys, block[xs][ys]))
      cout << "OK\\n";
    else
      cout << "NG\\n";
  }
}
Problem p00207 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 100
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, -1, 0, 1};
int w, h, sx, sy, gx, gy, G[MAX + 2][MAX + 2];
bool dfs(int x, int y, int color) {
  if (color == 0 || G[x][y] != color)
    return false;
  if (x == gx && y == gy)
    return true;
  G[x][y] = 0;
  for (int r = 0; r < 4; r++) {
    if (dfs(x + dx[r], y + dy[r], color))
      return true;
  }
  return false;
}
main() {
  int n, c, d, px, py, ww, hh;
  while (cin >> w >> h && w) {
    cin >> sx >> sy >> gx >> gy >> n;
    for (int y = 0; y < h + 2; y++)
      for (int x = 0; x < w + 2; x++)
        G[x][y] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> c >> d >> px >> py;
      if (d == 0) {
        ww = 4;
        hh = 2;
      } else {
        ww = 2;
        hh = 4;
      }
      for (int y = 0; y <= hh - 1; y++)
        for (int x = 0; x <= ww - 1; x++)
          G[px + x][py + y] = c;
    }
    if (dfs(sx, sy, G[sx][sy]))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

Error: TLE

Fixed submission:

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

int w, h;
vector<vector<int>> block_map;
vector<vector<bool>> visited;

int xs, ys;
int xg, yg;

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

bool dfs(int x, int y) {
  if (block_map[y][x] != block_map[yg][xg])
    return false;
  if (x == xg && y == yg)
    return true;

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h)
      continue;

    if (visited[ny][nx] == false && block_map[ny][nx] == block_map[ys][xs]) {
      visited[ny][nx] = true;
      if (dfs(nx, ny)) {
        return true;
      }
      visited[ny][nx] = false;
    }
  }

  return false;
}

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

    block_map = vector<vector<int>>(h, vector<int>(w, 0));
    visited = vector<vector<bool>>(h, vector<bool>(w, false));

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

    xs--;
    ys--;
    xg--;
    yg--;

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

      if (d == 0) {
        for (int by = 0; by < 2; by++) {
          for (int bx = 0; bx < 4; bx++) {
            block_map[y + by][x + bx] = c;
          }
        }
      } else {
        for (int by = 0; by < 4; by++) {
          for (int bx = 0; bx < 2; bx++) {
            block_map[y + by][x + bx] = c;
          }
        }
      }
    }

    visited[ys][xs] = true;
    if (dfs(xs, ys))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}