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

#include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1005][1005];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1005][1005];
int BFS(int u) {
  queue<P> que;
  ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
  que.push(P(gx[u], gy[u]));
  FAR[gx[u]][gy[u]] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx[u + 1] && p.second == gy[u + 1])
      break;
    ROP(i, 4) {
      int nx = p.first + DX[i], ny = p.second + DY[i];
      if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
          FAR[nx][ny] == INF) {
        que.push(P(nx, ny));
        FAR[nx][ny] = FAR[p.first][p.second] + 1;
      }
    }
  }
  return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
  int res = 0;
  ROP(i, G) { res += BFS(i); }
  printf("%d\\n", res);
}
int main() {
  scanf("%d %d %d", &N, &M, &G);
  ROPP(i, N) {
    ROP(j, M + 1) {
      scanf("%c", &MAP[j][i]);
      if (MAP[j][i] == 'S')
        gx[0] = j, gy[0] = i;
      ROPP(k, G) {
        char l = '0' + k;
        if (MAP[j][i] == l)
          gx[k] = j, gy[k] = i;
      }
    }
  }
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1000][1000 + 1];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1000][1000 + 1];
int BFS(int u) {
  queue<P> que;
  ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
  que.push(P(gx[u], gy[u]));
  FAR[gx[u]][gy[u]] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx[u + 1] && p.second == gy[u + 1])
      break;
    ROP(i, 4) {
      int nx = p.first + DX[i], ny = p.second + DY[i];
      if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
          FAR[nx][ny] == INF) {
        que.push(P(nx, ny));
        FAR[nx][ny] = FAR[p.first][p.second] + 1;
      }
    }
  }
  return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
  int res = 0;
  ROP(i, G) { res += BFS(i); }
  printf("%d\\n", res);
}
int main() {
  scanf("%d %d %d", &N, &M, &G);
  ROPP(i, N) {
    ROP(j, M + 1) {
      scanf("%c", &MAP[j][i]);
      if (MAP[j][i] == 'S')
        gx[0] = j, gy[0] = i;
      ROPP(k, G) {
        char l = '0' + k;
        if (MAP[j][i] == l)
          gx[k] = j, gy[k] = i;
      }
    }
  }
  solve();
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

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

#define INF 20000000

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

typedef pair<int, int> P;

int main() {
  char map[1002][1002];
  static int smp[1002][1002];
  int h, w, n, c = 0, x, y, ans = 0;
  queue<P> q;
  cin >> h >> w >> n;
  for (int i = 0; i < h + 2; ++i)
    for (int j = 0; j < w + 2; ++j) {
      map[i][j] = 'X';
      smp[i][j] = INF;
    }
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      cin >> map[i][j];
      smp[i][j] = INF;
      if (map[i][j] == 'S') {
        x = j;
        y = i;
      }
    }
  q.push(P(x, y));
  smp[y][x] = 0;
  while (1) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (map[y][x] == '1' + c) {
      ans += smp[y][x];
      c++;
      if (c == n)
        break;
      while (!q.empty())
        q.pop();
      for (int i = 1; i <= h; ++i)
        for (int j = 1; j <= w; ++j)
          smp[i][j] = INF;
      smp[y][x] = 0;
    }
    for (int i = 0; i < 4; ++i) {
      if (map[y + dy[i]][x + dx[i]] != 'X' &&
          smp[y + dy[i]][x + dx[i]] == INF) {
        q.push(P(x + dx[i], y + dy[i]));
        smp[y + dy[i]][x + dx[i]] = smp[y][x] + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

struct POS {
  int x, y;
};

int main() {
  char map[1002][1002];
  static int smp[1002][1002];
  int h, w, n, c = 0, ans, x, y;
  POS p;
  queue<pair<int, POS>> q;
  cin >> h >> w >> n;
  for (int i = 0; i < h + 2; ++i)
    for (int j = 0; j < w + 2; ++j) {
      map[i][j] = 'X';
      smp[i][j] = 100;
    }
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      cin >> map[i][j];
      smp[i][j] = 0;
      if (map[i][j] == 'S') {
        map[i][j] = '.';
        x = j;
        y = i;
      }
    }
  p.x = x;
  p.y = y;
  q.push(pair<int, POS>(0, p));
  while (c < n) {
    ans = q.front().first;
    x = q.front().second.x;
    y = q.front().second.y;
    q.pop();
    smp[y][x] = c + 1;
    if (map[y][x] == '1' + c) {
      c++;
      while (!q.empty())
        q.pop();
    }
    for (int i = 0; i < 4; ++i) {
      if (map[y + dy[i]][x + dx[i]] != 'X' &&
          smp[y + dy[i]][x + dx[i]] < c + 1) {
        p.x = x + dx[i];
        p.y = y + dy[i];
        q.push(pair<int, POS>(ans + 1, p));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int MAX_H = 1005;
const int MAX_W = 1005;

string all[MAX_H];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int h, w;
int n;
int sx, sy;
typedef pair<int, int> P;

int bfs(char target) {
  set<P> s;
  queue<P> q1;
  queue<int> q2;
  q1.push(P(sy, sx));
  s.insert(P(sy, sx));
  q2.push(0);
  while (!q1.empty()) {
    P p = q1.front();
    q1.pop();
    int t = q2.front();
    q2.pop();
    // cout << p.first << " " << p.second <<" "<<t<< endl;
    for (int i = 0; i < 4; ++i) {
      int y = p.first + dy[i], x = p.second + dx[i];
      if (x >= 0 && x < w && y >= 0 && y < h && all[y][x] != 'X') {
        if (s.count(P(y, x))) {
          continue;
        }
        if (all[y][x] == target) {
          all[y][x] = '.';
          sx = x, sy = y;
          return t + 1;
        } else {
          q1.push(P(y, x));
          q2.push(t + 1);
          s.insert(P(y, x));
        }
      }
    }
  }
}

void solve() {
  int ans = 0;
  char t = '1';
  for (int i = 0; i < n; ++i) {
    ans += bfs(t);
    ++t;
  }
  cout << ans << endl;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; ++i) {
    cin >> all[i];
    for (int j = 0; j < w; ++j) {
      if (all[i][j] == 'S') {
        sx = j, sy = i;
        all[i][j] = '.';
      }
    }
  }
  solve();
  return 0;
}

Error: MLE

Fixed submission:

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

const int MAX_H = 1005;
const int MAX_W = 1005;

string all[MAX_H];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int h, w;
int n;
int sx, sy;
typedef pair<int, int> P;

int bfs(char target) {
  set<P> s;
  queue<P> q1;
  queue<int> q2;
  q1.push(P(sy, sx));
  q2.push(0);
  while (!q1.empty()) {
    P p = q1.front();
    q1.pop();
    int t = q2.front();
    q2.pop();
    s.insert(p);
    // cout << p.first << " " << p.second <<" "<<t<< endl;
    for (int i = 0; i < 4; ++i) {
      int y = p.first + dy[i], x = p.second + dx[i];
      if (x >= 0 && x < w && y >= 0 && y < h && all[y][x] != 'X') {
        if (s.count(P(y, x))) {
          continue;
        }
        if (all[y][x] == target) {
          all[y][x] = '.';
          sx = x, sy = y;
          return t + 1;
        } else {
          q1.push(P(y, x));
          q2.push(t + 1);
        }
      }
    }
  }
}

void solve() {
  int ans = 0;
  char t = '1';
  for (int i = 0; i < n; ++i) {
    ans += bfs(t);
    ++t;
  }
  cout << ans << endl;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; ++i) {
    cin >> all[i];
    for (int j = 0; j < w; ++j) {
      if (all[i][j] == 'S') {
        sx = j, sy = i;
        all[i][j] = '.';
      }
    }
  }
  solve();
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#define INF 10000000
using namespace std;

typedef pair<int, int> P;

int H, W, N;
int d[1000][1000];
char cell[1000][1000];
int si, sj, sum = 0;

void bfs(P start, int search) {
  // 4????????¨
  int vi[4] = {0, 0, -1, 1}, vj[4] = {-1, 1, 0, 0};

  int gi, gj;

  //???????????¢???INF?????????????????????????????´???????????§?¨??????????
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
      if (cell[i][j] == search + '0') {
        gi = i;
        gj = j;
      }
    }
  }

  // start??????????????¢0????????????
  d[start.first][start.second] = 0;

  queue<P> que;
  que.push(start);

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    //?????´??§break
    if (p.first == gi && p.second == gj)
      break;

    // next(i, j)
    int ni, nj;

    // 4?????????
    for (int i = 0; i < 4; i++) {
      ni = p.first + vi[i];
      nj = p.second + vj[i];

      if (ni >= 0 && ni < H && nj >= 0 && nj < W && cell[ni][nj] != 'X' &&
          d[ni][nj] == INF) {
        que.push(P(ni, nj));
        d[ni][nj] = d[p.first][p.second] + 1;
      }
    }
  }

  sum += d[gi][gj];

  if (search != N)
    bfs(P(gi, gj), search + 1);
}

int main() {
  cin >> H >> W >> N;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> cell[i][j];
      if (cell[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }

  bfs(P(si, sj), 1);

  cout << sum << endl;
}

Error: MLE

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 INF (1 << 30)
#define mp make_pair

using namespace std;

typedef pair<int, int> P;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int h, w, n;
int ans = 0;
int sx, sy, gx, gy;
char field[1010][1010];
bool went[1010][1010];

/*struct state{
        int x;
        int y;
        int count;
};*/

int bfs(char tmp) {
  queue<pair<int, P>> q;
  memset(went, false, sizeof(went));
  q.push(mp(0, mp(sx, sy)));

  while (!q.empty()) {
    pair<int, P> p = q.front();
    q.pop();
    // cout << p.first << " " << p.second.first << " " << p.second.second <<
    // endl;
    if ((field[p.second.second][p.second.first]) == tmp) {
      sx = p.second.first;
      sy = p.second.second;
      return p.first;
    }
    went[p.second.second][p.second.first] = true;
    rep(i, 4) {
      int nx = p.second.first + dx[i];
      int ny = p.second.second + dy[i];
      if (nx >= 0 && ny >= 0 && nx < w && ny < h && !went[ny][nx] &&
          field[ny][nx] != 'X') {
        q.push(mp(p.first + 1, mp(nx, ny)));
      }
    }
  }
  return 0;
}

int main() {
  memset(field, 0, sizeof(field));
  cin >> h >> w >> n;
  rep(i, h) rep(j, w) {
    cin >> field[i][j];
    if (field[i][j] == 'S')
      sx = j, sy = i;
    if (field[i][j] == 'G')
      gx = j, gy = i;
  }
  /*rep(i, h) {
          rep(j, w) cout << field[i][j];
          cout << endl;
  }*/
  for (int i = 1; i <= n; ++i) {
    ans += bfs('0' + i);
  }
  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

char ma[1001][1001];
bool visited[1001][1001];
int h, w, n;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int sx, sy;
vector<pair<int, int>> vec(10);
queue<pair<pair<int, int>, int>> que;
int bfs(int d) {
  while (que.size()) {
    pair<pair<int, int>, int> q = que.front();
    que.pop();
    int x = q.first.first;
    int y = q.first.second;
    if (visited[y][x])
      continue;
    if (ma[y][x] - '0' == d) {
      return q.second;
    }
    visited[y][x] = 1;
    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 && ma[ny][nx] != 'X' &&
          !visited[ny][nx]) {
        que.push(make_pair(make_pair(nx, ny), q.second + 1));
      }
    }
  }
  return 0;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < w; j++) {
      ma[i][j] = str[j];
      if (ma[i][j] > '0' && ma[i][j] <= '9') {
        vec[ma[i][j] - '0'] = make_pair(j, i);
      }
      if (ma[i][j] == 'S') {
        vec[0] = make_pair(j, i);
      }
    }
  }
  int c = 0;
  for (int i = 1; i <= n; i++) {
    while (que.size())
      que.pop();
    memset(visited, 0, sizeof(visited));
    que.push(make_pair(make_pair(vec[i - 1].first, vec[i - 1].second), 0));
    c += bfs(i);
  }
  cout << c << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

char ma[1001][1001];
bool visited[1001][1001];
int h, w, n;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int sx, sy;
vector<pair<int, int>> vec(10);
queue<pair<pair<int, int>, int>> que;
int bfs(int d) {
  while (que.size()) {
    pair<pair<int, int>, int> q = que.front();
    que.pop();
    int x = q.first.first;
    int y = q.first.second;
    if (ma[y][x] - '0' == d) {
      return q.second;
    }
    visited[y][x] = 1;
    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 && ma[ny][nx] != 'X' &&
          !visited[ny][nx]) {
        que.push(make_pair(make_pair(nx, ny), q.second + 1));
      }
    }
  }
  return 0;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ma[i][j];
      if (ma[i][j] > '0' && ma[i][j] <= '9') {
        vec[ma[i][j] - '0'] = make_pair(j, i);
      }
      if (ma[i][j] == 'S') {
        vec[0] = make_pair(j, i);
      }
    }
  }
  int c = 0;
  for (int i = 1; i <= n; i++) {
    while (que.size())
      que.pop();
    memset(visited, 0, sizeof(visited));
    que.push(make_pair(make_pair(vec[i - 1].first, vec[i - 1].second), 0));
    c += bfs(i);
  }
  cout << c << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct ST {
  int x;
  int y;
  int cnt = 0;
};
bool came[1000][1000] = {{0}};
char map[1000][1000];
int main() {
  ST m, s;
  queue<ST> que;
  int n;
  int w, h;
  int mx[4] = {0, 0, 1, -1};
  int my[4] = {1, -1, 0, 0};
  cin >> h >> w >> n;
  char c = '0';
  bool a = true;
  for (int i = 0; i < h; i++) {
    cin >> map[i];
    if (a) {
      for (int j = 0; j < w; j++ && a) {
        if (map[i][j] == 'S') {
          m.x = j;
          m.y = i;
          a = false;
        }
      }
    }
  }
  que.push(m);
  int cnt = 0;
  while (n) {
    n--;
    c++;
    while (!que.empty()) {
      m = que.front();
      que.pop();
      if (came[m.y][m.x] == 1)
        continue;
      came[m.y][m.x] = 1;
      if (map[m.y][m.x] == c) {
        while (!que.empty()) {
          que.pop();
        }
        break;
      }
      for (int i = 0; i < 4; i++) {
        if (m.x + mx[i] > -1 && m.x + mx[i] < w && m.y + my[i] > -1 &&
            m.y + my[i] < h) {
          s = m;
          s.x += mx[i];
          s.y += my[i];
          s.cnt++;
          if (map[s.y][s.x] != 'X' && came[s.y][s.x] == 0) {
            que.push(s);
          }
        }
      }
    }
    memset(came, 0, sizeof(came));
    que.push(m);
  }
  cout << m.cnt << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

int mx[4] = {0, 0, 1, -1};
int my[4] = {1, -1, 0, 0};
char map[1000][1000];
char c = '1';

struct M {
  int x;
  int y;
  int cnt;
};

M s, v;

int run(int h, int w) {
  int came[1000][1000] = {0};
  queue<M> que;
  s.cnt = 0;
  que.push(s);
  while (1) {
    s = que.front();
    que.pop();
    came[s.y][s.x] = s.cnt;
    if (map[s.y][s.x] == c) {
      while (!que.empty())
        que.pop();
      c++;
      return s.cnt;
    }
    for (int i = 0; i < 4; i++) {
      if (s.x + mx[i] > -1 && s.x + mx[i] < w && s.y + my[i] > -1 &&
          s.y + my[i] < h) {
        v = s;
        v.x += mx[i];
        v.y += my[i];
        v.cnt++;
        if (map[v.y][v.x] != 'X' && came[v.y][v.x] == 0) {
          que.push(v);
        }
      }
    }
  }
}

int main() {
  s.x = -1;
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    cin >> map[i];
    for (int j = 0; j < w && s.x < 0; j++) {
      if (map[i][j] == 'S') {
        s.x = j;
        s.y = i;
        break;
      }
    }
  }
  int ans = 0;
  while (n) {
    n--;
    ans += run(h, w);
  }
  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

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

#include <iostream>
#include <queue>

using namespace std;

#define y .first
#define x .second

pair<int, int> p, cpy;

char route[1000 + 1][1000 + 1];

int came[1000 + 1][1000 + 1];

bool check(pair<int, int> now, int h, int w);

#define size sizeof came

int mx[] = {1, 0, 0, -1};
int my[] = {0, 1, -1, 0};

int main() {
  int h, w, n;
  scanf("%d %d %d", &h, &w, &n);

  char end = '0' + n, cheese = '1';

  scanf("%c", &route[0][0]);

  int i, j;
  for (i = 0; i < h; ++i) {
    for (j = 0; j <= w; ++j) {
      scanf("%c", &route[i][j]);

      if (route[i][j] == 'S') {
        p y = i;
        p x = j;
      }
    }
  }

  int ans = 0;

  do {
    queue<pair<int, int>> que;

    memset(came, 0, size);

    while (route[p y][p x] != cheese) {
      int i;
      for (i = 0; i < 4; ++i) {
        cpy y = p y + my[i];
        cpy x = p x + mx[i];

        if (route[cpy y][cpy x] != 'X' && check(cpy, h, w) == true) {
          came[cpy y][cpy x] = came[p y][p x] + 1;

          que.push(cpy);
        }
      }

      came[p y][p x] = 1;

      p = que.front();

      que.pop();
    }

    ++cheese;

    ans += came[p y][p x];

  } while (cheese <= end);

  printf("%d\\n", ans);

  return 0;
}

bool check(pair<int, int> now, int h, int w) {
  if (now y >= h || now x >= w || now y < 0 || now x < 0)
    return false;

  else if (came[now y][now x] > 0)
    return false;

  return true;
}

Error: 0

Fixed submission:

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

#include <iostream>
#include <queue>

using namespace std;

#define y .first
#define x .second

pair<int, int> p, cpy;

char route[1000][1000];

int came[1000][1000];

bool check(pair<int, int> now, int h, int w);

#define size sizeof came

int mx[] = {1, 0, 0, -1};
int my[] = {0, 1, -1, 0};

int main() {
  int h, w, n;
  scanf("%d %d %d", &h, &w, &n);

  char end = '0' + n, cheese = '1';

  scanf("%c", &route[0][0]);

  int i, j;
  for (i = 0; i < h; ++i) {
    for (j = 0; j <= w; ++j) {
      scanf("%c", &route[i][j]);

      if (route[i][j] == 'S') {
        p y = i;
        p x = j;
      }
    }
  }

  int ans = 0;

  do {
    queue<pair<int, int>> que;

    memset(came, 0, size);

    while (route[p y][p x] != cheese) {
      int i;
      for (i = 0; i < 4; ++i) {
        cpy y = p y + my[i];
        cpy x = p x + mx[i];

        if (route[cpy y][cpy x] != 'X' && check(cpy, h, w) == true) {
          came[cpy y][cpy x] = came[p y][p x] + 1;

          que.push(cpy);
        }
      }

      came[p y][p x] = 1;

      p = que.front();

      que.pop();
    }

    ++cheese;

    ans += came[p y][p x];

  } while (cheese <= end);

  printf("%d\\n", ans);

  return 0;
}

bool check(pair<int, int> now, int h, int w) {
  if (now y >= h || now x >= w || now y < 0 || now x < 0)
    return false;

  else if (came[now y][now x] > 0)
    return false;

  return true;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
int m[1002][1002] = {0}, p[1002][1002] = {0};
struct r {
  int g, g2;
  unsigned long long l;
};
int main() {
  int h, w, n, i, j, a, b[2], l2 = 1;
  b[0] = 1;
  b[1] = -1;
  char c[2001];
  r gl;
  queue<r> q;
  cin >> h >> w >> n;
  for (i = 0; i <= h + 1; i++) {
    m[i][0] = -1;
    m[i][w + 1] = -1;
  }
  for (i = 0; i <= w + 1; i++) {
    m[0][i] = -1;
    m[h + 1][i] = -1;
  }
  for (i = 1; i <= h; i++) {
    scanf("%s", c);
    for (j = 1; j <= w; j++) {
      a = c[j - 1];
      if (a >= 49 && a <= 57) {
        m[i][j] = a - 48;
      }
      if (c[j - 1] == 'X') {
        m[i][j] = -1;
      }
      if (c[j - 1] == 'S') {
        gl.g = i;
        gl.g2 = j;
        gl.l = 0;
        q.push(gl);
      }
    }
  }
  while (1) {
    gl = q.front();
    q.pop();
    if (m[gl.g][gl.g2] == l2) {
      l2++;
      if (l2 == n + 1) {
        break;
      }
      while (!q.empty()) {
        q.pop();
      }
    }
    gl.l++;
    p[gl.g][gl.g2] = l2;
    for (i = 0; i < 2; i++) {
      if (m[gl.g + b[i]][gl.g2] != -1 && p[gl.g + b[i]][gl.g2] != l2) {
        p[gl.g + b[i]][gl.g2] = l2;
        gl.g += b[i];
        q.push(gl);
        gl.g -= b[i];
      }
      if (m[gl.g][gl.g2 + b[i]] != -1 && p[gl.g][gl.g2 + b[i]] != l2) {
        p[gl.g][gl.g2 + b[i]] = l2;
        gl.g2 += b[i];
        q.push(gl);
        gl.g2 -= b[i];
      }
    }
  }
  cout << gl.l << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
int m[1002][1002] = {0}, p[1002][1002] = {0};
struct r {
  int g, l, g2, l2;
};
int main() {
  int h, w, n, i, j, a, b[2];
  b[0] = 1;
  b[1] = -1;
  char c[2001];
  r gl;
  queue<r> q;
  cin >> h >> w >> n;
  for (i = 0; i <= h + 1; i++) {
    m[i][0] = -1;
    m[i][w + 1] = -1;
  }
  for (i = 0; i <= w + 1; i++) {
    m[0][i] = -1;
    m[h + 1][i] = -1;
  }
  for (i = 1; i <= h; i++) {
    scanf("%s", c);
    for (j = 1; j <= w; j++) {
      a = c[j - 1];
      if (a >= 49 && a <= 57) {
        m[i][j] = a - 48;
      }
      if (c[j - 1] == 'X') {
        m[i][j] = -1;
      }
      if (c[j - 1] == 'S') {
        gl.g = i;
        gl.g2 = j;
        gl.l = 0;
        gl.l2 = 1;
        q.push(gl);
      }
    }
  }
  while (!q.empty()) {
    gl = q.front();
    q.pop();
    if (m[gl.g][gl.g2] == gl.l2) {
      gl.l2++;
      if (gl.l2 == n + 1) {
        break;
      }
      while (!q.empty())
        q.pop();
    }
    p[gl.g][gl.g2] = gl.l2;
    gl.l++;
    for (i = 0; i < 2; i++) {
      if (m[gl.g + b[i]][gl.g2] != -1 && p[gl.g + b[i]][gl.g2] != gl.l2) {
        gl.g += b[i];
        q.push(gl);
        gl.g -= b[i];
      }
      if (m[gl.g][gl.g2 + b[i]] != -1 && p[gl.g][gl.g2 + b[i]] != gl.l2) {
        gl.g2 += b[i];
        q.push(gl);
        gl.g2 -= b[i];
      }
    }
  }
  cout << gl.l << endl;
  return 0;
}