alexjercan/bugnet huggingface dataset:
Problem p01478 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, vector<int>> P;

int main() {
  int n, c[10][10];
  map<vector<int>, int> d;
  priority_queue<P, vector<P>, greater<P>> q;
  vector<int> v;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    v.push_back(i);
    for (int j = 0; j < n; j++)
      cin >> c[i][j];
  }

  do {
    d[v] = 100000000;
  } while (next_permutation(v.begin(), v.end()));
  q.push(P(0, v));
  d[v] = 0;

  while (q.size()) {
    P p = q.top();
    q.pop();
    int dis = p.first;
    v = p.second;

    if (d[v] < dis)
      continue;
    ans = dis;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int cost = dis + c[v[i]][v[j]];
        swap(v[i], v[j]);
        if (cost < d[v]) {
          d[v] = cost;
          q.push(P(cost, v));
        }
        swap(v[i], v[j]);
      }
    }
  }

  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

typedef pair<int, vector<int>> P;

int main() {
  int n, c[10][10];
  map<vector<int>, int> d;
  priority_queue<P, vector<P>, greater<P>> q;
  vector<int> v;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    v.push_back(i);
    for (int j = 0; j < n; j++)
      cin >> c[i][j];
  }

  d.clear();
  q.push(P(0, v));

  while (q.size()) {
    P p = q.top();
    q.pop();
    int dis = p.first;
    v = p.second;

    if (d[v] && d[v] < dis)
      continue;
    ans = dis;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int cost = dis + c[v[i]][v[j]];
        swap(v[i], v[j]);
        if (!d[v] || cost < d[v]) {
          d[v] = cost;
          q.push(P(cost, v));
        }
        swap(v[i], v[j]);
      }
    }
  }

  cout << ans << endl;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int MAX_N = 8;
int N;
int G[MAX_N][MAX_N];
priority_queue<pair<int, int>> que;
set<int> vis;
int f[10];
int ten[10];

int make() {
  f[0] = f[1] = 1;
  for (int i = 2; i < 10; ++i)
    f[i] = f[i - 1] * i;
  ten[0] = 1;
  for (int i = 1; i < 10; ++i)
    ten[i] = ten[i - 1] * 10;
}

int getDigit(int s, int n) { return s / ten[n] % 10; }

int my_swap(int s, int a, int b) {
  int A = getDigit(s, a);
  int B = getDigit(s, b);
  return s - A * ten[a] - B * ten[b] + A * ten[b] + B * ten[a];
}

int solve() {
  int v = 0;
  for (int i = 0; i < N; ++i)
    v += i * ten[i];
  que.push(make_pair(0, v));

  int ans = 0;
  while (!que.empty() && vis.size() < f[N]) {
    int cost = que.top().first;
    v = que.top().second;
    que.pop();
    if (vis.find(v) != vis.end())
      continue;
    vis.insert(v);
    ans = min(ans, cost);

    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        int nv = my_swap(v, i, j);
        if (vis.find(nv) == vis.end()) {
          que.push(make_pair(cost - G[i][j], nv));
        }
      }
    }
  }
  return -ans;
}

int main() {
  make();
  cin >> N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> G[i][j];
    }
  }
  cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int MAX_N = 8;
int N;
int G[MAX_N][MAX_N];
priority_queue<pair<int, string>> que;
set<string> vis;
int f[10];

int make() {
  f[0] = f[1] = 1;
  for (int i = 2; i < 10; ++i)
    f[i] = f[i - 1] * i;
}

int solve() {
  string v(N, ' ');
  int ans = 0;
  for (char i = 1; i <= N; ++i)
    v[i] = i;
  que.push(make_pair(0, v));

  while (!que.empty() && vis.size() < f[N]) {
    int cost = que.top().first;
    v = que.top().second;
    que.pop();
    if (vis.find(que.top().second) != vis.end())
      continue;

    vis.insert(v);
    ans = min(ans, cost);

    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        swap(v[i], v[j]);
        if (vis.find(v) == vis.end()) {
          que.push(make_pair(cost - G[i][j], v));
        }
        swap(v[i], v[j]);
      }
    }
  }
  return -ans;
}

int main() {
  make();
  cin >> N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> G[i][j];
    }
  }
  cout << solve() << endl;
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  int n;
  cin >> n;
  int c[8][8];
  rep(i, n) rep(j, n) cin >> c[i][j];
  int v = 0;
  rep(i, n) v = v * 10 + i;
  map<int, int> mp;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, v));
  while (!que.empty()) {
    pair<int, int> now = que.top();
    que.pop();
    if (mp.count(now.second) > 0 && mp[now.second] <= now.first)
      continue;
    mp[now.second] = now.first;
    rep(i, n) rep(j, n) {
      int vv = now.second;
      int a = now.second / (int)pow(10, i) % 10;
      int b = now.second / (int)pow(10, j) % 10;
      vv -= a * pow(10, i);
      vv += a * pow(10, j);
      vv -= b * pow(10, j);
      vv += b * pow(10, i);
      que.push(make_pair(now.first + c[i][j], vv));
    }
  }
  int res = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
    res = max(res, it->second);
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  int n;
  cin >> n;
  int c[8][8];
  rep(i, n) rep(j, n) cin >> c[i][j];
  int v = 0;
  rep(i, n) v = v * 10 + i;
  map<int, int> mp;
  priority_queue<int, vector<int>, greater<int>> que;
  que.push(v);
  mp[v] = 0;
  while (!que.empty()) {
    int now = que.top();
    que.pop();
    rep(i, n) rep(j, n) {
      int vv = now;
      int a = now / (int)pow(10, i) % 10;
      int b = now / (int)pow(10, j) % 10;
      vv -= a * pow(10, i);
      vv += a * pow(10, j);
      vv -= b * pow(10, j);
      vv += b * pow(10, i);
      if (mp.count(vv) > 0 && mp[vv] <= mp[now] + c[i][j])
        continue;
      mp[vv] = mp[now] + c[i][j];
      que.push(vv);
    }
  }
  int res = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
    res = max(res, it->second);
  }
  cout << res << endl;
  return 0;
}
Problem p01479 (C++) — buggy submission, verdict: Runtime Error

// 59
#include <iostream>

using namespace std;

int main() {
  char s[7 * 1001];
  cin >> s;
  char p = 0;
  int l = 0;
  char r;
  int rl = -1;
  for (int i = 0; s[i]; i += (p == 'e') ? 3 : 7) {
    if (p == s[i]) {
      l = 0;
    }
    l++;
    p = s[i];
    if (l > rl) {
      rl = l;
      r = p;
    }
  }
  cout << ((r == 'e') ? "egg" : "chicken") << endl;
  return 0;
}

Error: 0

Fixed submission:

// 59
#include <iostream>

using namespace std;

int main() {
  char s[1001];
  cin >> s;
  char p = 0;
  int l = 0;
  char r;
  int rl = -1;
  for (int i = 0; s[i]; i += (p == 'e') ? 3 : 7) {
    if (p == s[i]) {
      l = 0;
    }
    l++;
    p = s[i];
    if (l > rl) {
      rl = l;
      r = p;
    }
  }
  cout << ((r == 'e') ? "egg" : "chicken") << endl;
  return 0;
}
Problem p01479 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  None,
  Egg,
  Chiken,
};

int main() {

  Type pre = None, ans;

  int depth = 0, max_depth = 0;

  char buf[10001];
  scanf("%s", buf);

  for (int i = 0; buf[i] != '\\0';) {
    if (buf[i] == 'e') {
      if (pre == Egg) { //???????????????
        depth = 0;
        pre = None;
      } else {
        depth++;
        if (depth > max_depth) {
          max_depth = depth;
          ans = Egg;
        }
        pre = Egg;
        i += 3;
      }
    } else {
      if (pre == Chiken) { //???????????????
        depth = 0;
        pre = None;
      } else {
        depth++;
        if (depth > max_depth) {
          max_depth = depth;
          ans = Chiken;
        }
        pre = Chiken;
        i += 7;
      }
    }
  }

  if (ans == Egg) {
    printf("egg\\n");
  } else {
    printf("chicken\\n");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  None,
  Egg,
  Chiken,
};

int main() {

  Type pre = None, ans;

  int depth = 0, max_depth = 0;

  char buf[1001];
  scanf("%s", buf);

  for (int i = 0; buf[i] != '\\0';) {
    if (buf[i] == 'e') {
      if (pre == Egg) {
        depth = 0;
        pre = None;
      } else {
        depth++;
        if (depth > max_depth) {
          max_depth = depth;
          ans = Egg;
        }
        pre = Egg;
        i += 3;
      }
    } else {
      if (pre == Chiken) {
        depth = 0;
        pre = None;
      } else {
        depth++;
        if (depth > max_depth) {
          max_depth = depth;
          ans = Chiken;
        }
        pre = Chiken;
        i += 7;
      }
    }
  }

  if (ans == Egg) {
    printf("egg\\n");
  } else {
    printf("chicken\\n");
  }

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

#include <stdio.h>
#include <string.h>

int main(void) {
  int i, j, k, ans = 1, bef = 0, num = 1, mnum = 1;
  char s[10010];
  scanf("%s", s);
  for (i = 0; i < strlen(s);) {
    if (s[i] == 'e') {
      if (!i) {
        bef = 0;
        i += 3;
        continue;
      }
      if (bef == 1) {
        num++;
        bef = 0;
        if (mnum < num)
          mnum = num, ans = 0;
      } else {
        bef = 0;
        num = 1;
      }
      i += 3;
    } else {
      if (!i) {
        bef = 1;
        i += 7;
        continue;
      }
      if (bef == 0) {
        num++;
        bef = 1;
        if (mnum < num)
          mnum = num, ans = 1;
      } else {
        bef = 1;
        num = 1;
      }
      i += 7;
    }
  }
  if (ans == -1)
    ans = bef;
  if (ans)
    printf("chicken\\n");
  else
    printf("egg\\n");
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {
  int i, j, k, ans = -1, bef = 0;
  char s[1010];
  scanf("%s", s);
  for (i = 0; i < strlen(s);) {
    if (s[i] == 'e') {
      if (i && bef == 0 && i != 3) {
        ans = 0;
        break;
      } else {
        i += 3;
        bef = 0;
      }
    } else {
      if (i && bef == 1 && i != 7) {
        ans = 1;
        break;
      } else {
        i += 7;
        bef = 1;
      }
    }
  }
  if (ans == -1)
    ans = bef;
  if (ans)
    printf("chicken\\n");
  else
    printf("egg\\n");
  return 0;
}
Problem p01479 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <stdio.h>
#include <vector>
using namespace std;
struct S {
  int length, ancestor;
  bool operator>(const S &s) const { return length > s.length; }
};

vector<S> v;
S s;
void F(int id) {
  if (s.ancestor == id) {
    v.push_back(s);
    s.length = 1;
    s.ancestor = id;
  } else {
    ++s.length;
    s.ancestor = id;
  }
}
int main() {
  char str[8192];
  scanf("%s", str);
  s.length = 0;
  s.ancestor = -1;
  for (int i = 0; str[i];) {
    if (str[i] == 'e') {
      F(0);
      i += 3;
    } else {
      F(1);
      i += 7;
    }
  }
  if (s.ancestor >= 0)
    v.push_back(s);
  stable_sort(v.begin(), v.end(), greater<S>());
  puts(v[0].ancestor ? "chicken" : "egg");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
#include <vector>
using namespace std;
struct S {
  int length, ancestor;
  bool operator>(const S &s) const { return length > s.length; }
};

vector<S> v;
S s;
void F(int id) {
  if (s.ancestor == id) {
    v.push_back(s);
    s.length = 1;
    s.ancestor = id;
  } else {
    ++s.length;
    s.ancestor = id;
  }
}
int main() {
  char str[1024];
  scanf("%s", str);
  s.length = 0;
  s.ancestor = -1;
  for (int i = 0; str[i];) {
    if (str[i] == 'e') {
      F(0);
      i += 3;
    } else {
      F(1);
      i += 7;
    }
  }
  if (s.ancestor >= 0)
    v.push_back(s);
  stable_sort(v.begin(), v.end(), greater<S>());
  puts(v[0].ancestor ? "chicken" : "egg");
}
Problem p01479 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

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

// 2362
int main() {
  string s;
  cin >> s;
  string ec[2] = {"egg", "chicken"};
  vii v;
  vi u;
  int last = -1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2; j++) {
      if (s[i] == ec[j][0]) {
        if (j == last) {
          v.pb(u);
          u.clear();
          u.pb(j);
        } else {
          u.pb(j);
          last = j;
        }
        i += ec[j].size() - 1;
      }
    }
  }
  v.pb(u);
  int m = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[m].size() < v[i].size())
      m = i;
  }

  cout << ec[v[m][v[m].size() - 1]] << endl;
}

Error: -11

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

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

// 2362
int main() {
  string s;
  cin >> s;
  string ec[2] = {"egg", "chicken"};
  vii v;
  vi u;
  int last = -1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2; j++) {
      if (s[i] == ec[j][0]) {
        if (j == last) {
          v.pb(u);
          u.clear();
          u.pb(j);
        } else {
          u.pb(j);
          last = j;
        }
        i += ec[j].size() - 1;
      }
    }
  }

  int m = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[m].size() < v[i].size())
      m = i;
  }

  cout << ec[v[m][v[m].size() - 1]] << endl;
}
Problem p01481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
using namespace std;
int n, m, h, w, x2, y2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string s[1001];
bool b[1001][1001];
bool bfs(int y3, int x3) {
  int y = 0, x = 0;
  b[0][0] = 1;
  queue<pair<int, int>> q;
  q.push(mk(x3, y3));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int xx = p.first, yy = p.second;
    r(i, 4) {
      int x1 = xx + dx[i], y1 = yy + dy[i];
      if (x1 < 0 || y1 < 0 || x1 >= w || y1 >= h)
        continue;
      if (!b[y1][x1] && s[y1][x1] != '#') {
        if (s[y1][x1] == 't')
          return 1;
        b[y1][x1] = 1;
        q.push(mk(x1, y1));
      } else
        b[y1][x1] = 1;
    }
  }
  return 0;
}
int main() {
  cin >> w >> h;
  r(i, h) cin >> s[i];
  cin >> m;
  if (bfs(0, 0)) {
    cout << 0 << endl;
    return 0;
  }
  r(i, m) {
    cin >> x2 >> y2;
    if (s[y2][x2] == '.')
      continue;
    s[y2][x2] = '.';
    if (b[y2][x2])
      if (bfs(y2, x2)) {
        cout << i + 1 << endl;
        return 0;
      }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
using namespace std;
int n, m, h, w, x2, y2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string s[1001];
bool b[1001][1001];
bool bfs() {
  int y = 0, x = 0;
  memset(b, 0, sizeof(b));
  b[0][0] = 1;
  queue<pair<int, int>> q;
  q.push(mk(0, 0));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int xx = p.first, yy = p.second;
    r(i, 4) {
      int x1 = xx + dx[i], y1 = yy + dy[i];
      if (x1 < 0 || y1 < 0 || x1 >= w || y1 >= h)
        continue;
      if (!b[x1][y1] && s[y1][x1] != '#') {
        if (s[y1][x1] == 't')
          return 1;
        b[x1][y1] = 1;
        q.push(mk(x1, y1));
      }
    }
  }
  return 0;
}
int main() {
  cin >> w >> h;
  r(i, h) cin >> s[i];
  cin >> m;
  r(i, m) {
    cin >> x2 >> y2;
    s[y2][x2] = '.';
    if (bfs()) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p01481 (C++) — buggy submission, verdict: Runtime Error

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

int W, H;
char G[1001][1001];
int vis[1001][1001];

bool bfs(int x, int y) {
  const static int dx[] = {1, 0, -1, 0};
  const static int dy[] = {0, 1, 0, -1};
  queue<int> que;
  que.push(x + y * W);
  while (!que.empty()) {
    x = que.front() % W;
    y = que.front() / W;
    que.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= W)
        continue;
      if (ny < 0 || ny >= H)
        continue;
      if (vis[ny][nx])
        continue;
      if (G[ny][nx] == '#') {
        vis[ny][nx] = 1;
        continue;
      }

      if (G[ny][nx] == 't')
        return true;
      que.push(nx + ny * W);
      vis[ny][nx] = 2;
    }
  }
  return false;
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
    }
  }

  int N;
  cin >> N;
  int x, y;
  x = y = 0;
  for (int i = 0; i < H; ++i)
    fill(vis[i], vis[i] + W, 0);
  vis[0][0] = 1;
  int ans = -1;
  for (int i = 0; i <= N; ++i) {
    if (ans == -1 && vis[y][x] == 1 && bfs(x, y))
      ans = i;
    if (i != N) {
      cin >> x >> y;
      if (G[y][x] == '#')
        G[y][x] = '.';
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int W, H;
char G[1001][1001];
int vis[1001][1001];

bool dfs(int x, int y) {
  const static int dx[] = {1, 0, -1, 0};
  const static int dy[] = {0, 1, 0, -1};
  vis[y][x] = 2;
  if (G[y][x] == 't')
    return true;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= W)
      continue;
    if (ny < 0 || ny >= H)
      continue;
    if (vis[ny][nx])
      continue;
    if (G[ny][nx] == '#') {
      vis[ny][nx] = 1;
      continue;
    }
    if (dfs(nx, ny))
      return true;
  }
  return false;
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
    }
  }

  int N;
  cin >> N;
  int x, y;
  x = y = 0;
  for (int i = 0; i < H; ++i)
    fill(vis[i], vis[i] + W, 0);
  vis[0][0] = 1;
  int ans = -1;
  for (int i = 0; i <= N; ++i) {
    if (ans == -1 && vis[y][x] == 1 && dfs(x, y))
      ans = i;
    if (i != N) {
      cin >> x >> y;
      if (G[y][x] == '#')
        G[y][x] = '.';
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> Pt;
typedef pair<int, Pt> P;
#define fr first
#define sc second
Pt gl;
int h, w, d[] = {0, 1, 0, -1};
bool used[1000][1000];
char mas[1000][1000];
vector<Pt> vc;
bool bfs(int n) {
  mas[vc[n].fr][vc[n].sc] = '.';
  bool ok = false;
  if (n) {
    for (int i = 0; i < 4; i++) {
      int nx = vc[n].fr + d[i], ny = vc[n].sc + d[3 - i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
        if (used[nx][ny])
          ok = true;
      }
    }
  } else
    ok = true;
  if (ok) {
    queue<Pt> que;
    que.push(Pt(vc[n]));
    used[vc[n].fr][vc[n].sc] = true;
    while (!que.empty()) {
      Pt p = que.front();
      que.pop();
      if (p == gl)
        return true;
      for (int i = 0; i < 4; i++) {
        int nx = p.fr + d[i], ny = p.sc + d[3 - i];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && !used[nx][ny] &&
            mas[nx][ny] != '#') {
          que.push(Pt(nx, ny));
          used[nx][ny] = true;
        }
      }
    }
  }
  return false;
}
int main() {
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> mas[i][j];
      if (mas[i][j] == 't')
        gl = Pt(i, j);
    }
  }
  int n;
  cin >> n;
  vc.resize(n + 1);
  vc[0] = Pt(0, 0);
  for (int i = 1; i <= n; i++) {
    cin >> vc[i].sc >> vc[i].fr;
  }

  int i = 0;
  for (i = 0; i <= n; i++) {
    if (bfs(i)) {
      cout << i << endl;
      break;
    }
  }
  if (i == n + 1)
    cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> Pt;
typedef pair<int, Pt> P;
#define fr first
#define sc second
Pt gl;
int h, w, d[] = {0, 1, 0, -1};
bool used[1000][1000];
char mas[1000][1000];
vector<Pt> vc;
bool bfs(int n) {
  memset(used, false, sizeof(used));
  mas[vc[n].fr][vc[n].sc] = '.';
  queue<Pt> que;
  que.push(Pt(0, 0));
  while (!que.empty()) {
    Pt p = que.front();
    que.pop();
    if (p == gl)
      return true;
    for (int i = 0; i < 4; i++) {
      int nx = p.fr + d[i], ny = p.sc + d[3 - i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w && !used[nx][ny] &&
          mas[nx][ny] != '#') {
        que.push(Pt(nx, ny));
        used[nx][ny] = true;
      }
    }
  }
  return false;
}
int main() {
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> mas[i][j];
      if (mas[i][j] == 't')
        gl = Pt(i, j);
    }
  }
  int n;
  cin >> n;
  vc.resize(n + 1);
  vc[0] = Pt(0, 0);
  for (int i = 1; i <= n; i++) {
    cin >> vc[i].sc >> vc[i].fr;
  }

  int i = 0;
  for (i = 0; i <= n; i++) {
    if (bfs(i)) {
      cout << i << endl;
      break;
    }
  }
  if (i == n + 1)
    cout << -1 << endl;
}