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

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
typedef int Weight;
Weight INF = 1000000000;

struct Edge {
  int src, dest;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void add_edge(Graph &g, int src, int dest) {
  g[src].push_back((Edge){src, dest});
}

struct Recipe {
  int g;
  vector<int> s;
};

Weight weight(Recipe r, Array &ary) {
  vector<int> vs;
  for (int s : r.s) {
    vs.emplace_back(ary[s]);
  }
  sort(ALL(vs), greater<int>());
  int mx = 0;
  REP(i, vs.size()) { mx = max(mx, vs[i] + i); }
  return mx;
}

int main() {
  int n, e, t;
  cin >> n >> e >> t;
  --t;
  queue<int> que;
  Array d(n, INF);
  Graph graph(n);
  REP(i, n) {
    que.push(i);
    int w;
    cin >> w;
    if (w)
      d[i] = 1;
  }
  vector<vector<Recipe>> gr(n);
  REP(i, e) {
    Recipe r;
    int c;
    cin >> r.g >> c;
    --r.g;
    REP(j, c) {
      int s;
      cin >> s;
      --s;
      add_edge(graph, s, r.g);
      r.s.push_back(s);
    }
    gr[r.g].push_back(r);
  }
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    bool update = false;
    for (auto r : gr[v]) {
      if (weight(r, d) < d[v]) {
        d[v] = weight(r, d);
        update = true;
      }
    }
    if (update) {
      REP(i, graph[v].size()) { que.push(graph[v][i].dest); }
    }
  }
  if (d[t] < INF) {
    cout << d[t] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

const int INF = 10000000;

int dfs(vector<int> &dep, const vector<pair<int, vector<int>>> &cmp,
        const vector<int> &esy, int i, int rec = 0) {
  if (dep[i] >= 0)
    return dep[i];
  if (esy[i] == 1)
    return dep[i] = 1;
  auto itr = lower_bound(begin(cmp), end(cmp), make_pair(i, vector<int>()));
  auto itr2 =
      lower_bound(begin(cmp), end(cmp), make_pair(i + 1, vector<int>()));
  if (itr == end(cmp) || itr->first != i)
    return dep[i] = INF;
  int ming = INF;
  for (auto k = itr; k != itr2; ++k) {
    vector<int> nums;
    for (int j : k->second) {
      nums.push_back(dfs(dep, cmp, esy, j));
    }
    sort(begin(nums), end(nums));
    int maxe = 0;
    int n = nums.size();
    REP(j, n) { maxe = max(maxe, nums[j] + (n - j - 1)); }
    ming = min(ming, max(maxe, n));
  }
  return dep[i] = ming;
}

int main() {
  int n, e, t;
  cin >> n >> e >> t;
  --t;
  vector<int> esy(n);
  REP(i, n) cin >> esy[i];
  vector<pair<int, vector<int>>> cmp;
  REP(i, e) {
    int g, c;
    cin >> g >> c;
    --g;
    vector<int> cld(c);
    REP(j, c) {
      cin >> cld[j];
      --cld[j];
    }
    cmp.emplace_back(g, cld);
  }
  sort(begin(cmp), end(cmp));
  vector<int> dep(n, -1);
  dfs(dep, cmp, esy, t);
  if (dep[t] < INF)
    cout << dep[t] << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p01435 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, e, t, g[1000], dp[300];
bool ok[300];
vi s[1000];

int main() {
  cin >> n >> e >> t;
  rep(i, n) cin >> ok[i];
  rep(i, e) {
    int k, a;
    cin >> g[i] >> k;
    g[i]--;
    rep(j, k) cin >> a, s[i].pb(a - 1);
  }
  rep(i, n) dp[i] = ok[i] ? 1 : inf;
  rep(it, n) rep(i, e) {
    int dp2[300], res = 0;
    rep(j, s[i].size()) dp2[j] = dp[s[i][j]];
    sort(dp2, dp2 + s[i].size(), greater<int>());
    rep(j, s[i].size()) res = max(res, j + dp2[j]);
    dp[g[i]] = min(dp[g[i]], res);
  }
  cout << (dp[t - 1] >= inf ? -1 : dp[t - 1]) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, e, t;
bool ok[300];

int main() {
  map<int, vector<vi>> mm;

  cin >> n >> e >> t;
  rep(i, n) cin >> ok[i];
  rep(i, e) {
    int g, k;
    cin >> g >> k;
    vi v(k);
    rep(j, k) cin >> v[j], v[j]--;
    mm[g - 1].pb(v);
  }
  t--;

  if (ok[t]) {
    cout << 1 << endl;
    return 0;
  }

  set<vi> s;
  vi v(1, t);
  priority_queue<pair<int, vi>> q;
  q.push(mp(-1, v));
  while (!q.empty()) {
    v = q.top().second;
    int co = q.top().first;
    q.pop();
    if (s.count(v))
      continue;
    s.insert(v);

    if (v.empty()) {
      cout << -co << endl;
      return 0;
    }

    rep(i, v.size()) each(j, mm[v[i]]) {
      vi nv = v;
      each(k, *j) if (!ok[*k]) nv.pb(*k);

      swap(nv[i], nv.back());
      nv.erase(nv.end() - 1);
      sort(all(nv));
      int nco = v.size() + j->size() - 1;
      nco = min(-nco, co);
      if (!s.count(nv))
        q.push(mp(nco, nv));
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p01437 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const string tmp = "NESW";
const int vy[] = {-1, 0, 1, 0}, vx[] = {0, 1, 0, -1};
int H, W;
int64 L;
string C[100];
int sx, sy, sv;
int64 nxt[100][100][4];
void dfs(int x, int y, int v, int64 step) {
  int nx = x + vx[v], ny = y + vy[v], nv = (v + 1) % 4;
  if (step == 0) {
    cout << y + 1 << " " << x + 1 << " " << tmp[v] << endl;
    return;
  } else if (0 <= nx && 0 <= ny && nx < W && ny < H && C[ny][nx] != '#') {
    int64 loop = ~nxt[nx][ny][v] ? nxt[x][y][v] - nxt[nx][ny][v] + 1 : step;
    nxt[nx][ny][v] = nxt[x][y][v] + 1;
    return (dfs(nx, ny, v, (step - 1) % loop));
  } else {
    nxt[x][y][nv] = nxt[x][y][v];
    return (dfs(x, y, nv, step));
  }
}
int main() {
  while (cin >> H >> W >> L, H) {
    memset(nxt, -1, sizeof(nxt));
    for (int i = 0; i < H; i++) {
      cin >> C[i];
      for (int j = 0; j < W; j++) {
        int p = tmp.find(C[i][j]);
        if (p != string::npos)
          sx = j, sy = i, sv = p;
      }
    }
    nxt[sx][sy][sv] = 0;
    dfs(sx, sy, sv, L);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const string tmp = "NEST";
const int vy[] = {-1, 0, 1, 0}, vx[] = {0, 1, 0, -1};

int H, W;
int64 L;
string C[100];
int sx, sy, sv;
int nxt[100][100][4];
void dfs(int x, int y, int v, int step) {
  int nx = x + vx[v], ny = y + vy[v], nv = (v + 1) % 4;
  if (step == 0) {
    cout << y + 1 << " " << x + 1 << " " << tmp[v] << endl;
    return;
  } else if (0 <= nx && 0 <= ny && nx < W && ny < H && C[ny][nx] != '#') {
    int loop = ~nxt[nx][ny][v] ? nxt[x][y][v] - nxt[nx][ny][v] : step;
    nxt[nx][ny][v] = nxt[x][y][v] + 1;
    return (dfs(nx, ny, v, (step - 1) % loop));
  } else {
    nxt[x][y][nv] = nxt[x][y][v];
    return (dfs(x, y, nv, step));
  }
}
int main() {
  while (cin >> H >> W >> L, H) {
    memset(nxt, -1, sizeof(nxt));
    for (int i = 0; i < H; i++) {
      cin >> C[i];
      for (int j = 0; j < W; j++) {
        int p = tmp.find(C[i][j]);
        if (p != string::npos)
          sx = j, sy = i, sv = p;
      }
    }
    nxt[sx][sy][sv] = 0;
    dfs(sx, sy, sv, L);
  }
}
Problem p01437 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int todir[256];
char tochr[4];

int h, w;
long long l;
string c[110];
int vis[110][110][4];

int main() {
  for (int i = 0; i < 4; i++) {
    todir["NESW"[i]] = i;
    tochr[i] = "NESW"[i];
  }

  while (cin >> h >> w >> l, h) {
    memset(vis, -1, sizeof(vis));
    c[0] = c[h + 1] = string(w + 2, '#');
    for (int i = 1; i <= h; i++) {
      cin >> c[i];
      c[i] = "#" + c[i] + "#";
    }
    int x, y, dir;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (c[i][j] != '.' && c[i][j] != '#') {
          x = i;
          y = j;
          dir = todir[c[i][j]];
          c[i][j] = '.';
        }
      }
    }
    for (long long t = 0; t < l; t++) {
      while (true) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (c[nx][ny] == '.')
          break;
        dir = (dir + 1) % 4;
      }
      if (~vis[x][y][dir]) {
        t += (l - t - 1) / (t - vis[x][y][dir]) * (t - vis[x][y][dir]);
      } else
        vis[x][y][dir] = t;
      x += dx[dir];
      y += dy[dir];
    }
    cout << x << " " << y << " " << tochr[dir] << endl;
  }
}

Error: TLE

Fixed submission:

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

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

int todir[256];
char tochr[4];

int h, w;
long long l;
string c[110];
int vis[110][110][4];

int main() {
  for (int i = 0; i < 4; i++) {
    todir["NESW"[i]] = i;
    tochr[i] = "NESW"[i];
  }

  while (cin >> h >> w >> l, h) {
    memset(vis, -1, sizeof(vis));
    c[0] = c[h + 1] = string(w + 2, '#');
    for (int i = 1; i <= h; i++) {
      cin >> c[i];
      c[i] = "#" + c[i] + "#";
    }
    int x, y, dir;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (c[i][j] != '.' && c[i][j] != '#') {
          x = i;
          y = j;
          dir = todir[c[i][j]];
          c[i][j] = '.';
        }
      }
    }
    for (long long t = 0; t < l; t++) {
      while (true) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (c[nx][ny] == '.')
          break;
        dir = (dir + 1) % 4;
      }
      if (~vis[x][y][dir]) {
        t += (l - t - 1) / (t - vis[x][y][dir]) * (t - vis[x][y][dir]);
      }
      x += dx[dir];
      y += dy[dir];
    }
    cout << x << " " << y << " " << tochr[dir] << endl;
  }
}
Problem p01437 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
int main() {
  ll h, w, l;
  ll i, j, k;
  ll dp[200][200][4] = {{{}}};
  string st[200];
  char c[] = {'E', 'S', 'W', 'N'};
  ll rx, ry, rd;
  ll ax[] = {1, 0, -1, 0};
  ll ay[] = {0, 1, 0, -1};
  bool f;
  ll s;
  while (cin >> h >> w >> l, h || w || l) {
    for (i = 0; i <= h + 1; i++)
      st[i] = "";
    for (i = 0; i < w + 2; i++)
      st[0] += "#";
    for (i = 1; i <= h; i++) {
      cin >> st[i];
      st[i] = "#" + st[i] + "#";
    }
    for (i = 0; i < w + 2; i++)
      st[h + 1] += "#";
    rx = 0;
    ry = 0;
    rd = 0;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        for (k = 0; k < 4; k++) {
          if (st[i][j] == c[k]) {
            st[i][j] = '.';
            ry = i;
            rx = j;
            rd = k;
          }
        }
      }
    }
    fill(dp[0][0], dp[104][104], -1);
    f = true;
    for (i = 1; i <= l; i++) {
      while (st[ry + ay[rd]][rx + ax[rd]] == '#') {
        rd = (rd + 1) % 4;
      }
      if (st[ry + ay[rd]][rx + ax[rd]] == '.') {
        rx += ax[rd];
        ry += ay[rd];
      }
      if (dp[ry][rx][rd] == -1) {
        dp[ry][rx][rd] = i;
      } else if (f) {
        s = i - dp[ry][rx][rd];
        l = i + (l - i) % s;
        f = false;
      }
    }
    cout << ry << ' ' << rx << ' ' << c[rd] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
int main() {
  ll h, w, l;
  while (cin >> h >> w >> l, h || w || l) {

    string st[105];
    ll i, j, k;
    for (i = 0; i <= h + 1; i++)
      st[i] = "";
    for (i = 0; i < w + 2; i++)
      st[0] += "#";
    for (i = 1; i <= h; i++) {
      cin >> st[i];
      st[i] = "#" + st[i] + "#";
    }
    for (i = 0; i < w + 2; i++)
      st[h + 1] += "#";

    char c[] = {'E', 'S', 'W', 'N'};

    ll rx, ry, rd;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        for (k = 0; k < 4; k++) {
          if (st[i][j] == c[k]) {
            st[i][j] = '.';
            ry = i;
            rx = j;
            rd = k;
          }
        }
      }
    }

    ll ax[] = {1, 0, -1, 0};
    ll ay[] = {0, 1, 0, -1};
    ll dp[105][105][4] = {{{}}};

    bool f = true;

    for (i = 1; i <= l; i++) {
      if (st[ry + ay[rd]][rx + ax[rd]] == '.') {
        rx += ax[rd];
        ry += ay[rd];
      }
      if (i == l)
        break;
      while (st[ry + ay[rd]][rx + ax[rd]] == '#') {
        rd = (rd + 1) % 4;
      }
      /*
      if(dp[ry][rx][rd]==0){
        dp[ry][rx][rd]=i;
      }else if(f){
        ll s = i-dp[ry][rx][rd];
        if((l-i)%s!=0){
          l=i+(l-i)%s;
          f=false;
        }
      }
      */
    }
    cout << ry << " " << rx << " " << c[rd] << endl;
  }
  return 0;
}
Problem p01437 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}, H, W;
string dir[] = {"N", "E", "S", "W"};

bool in_range(int a, int b) { return a >= 0 && a < b; }

string dfs(vector<string> &C, vector<vector<vector<long long int>>> &T, int x,
           int y, long long int l, int d) {
  string ret = "!";
  while (ret == "!") {
    if (T[x][y][d] > 0) {
      l %= (T[x][y][d] - l);
    } else {
      T[x][y][d] = l;
    }

    if (l == 0) {
      ret = to_string(x + 1) + " " + to_string(y + 1) + " " + dir[d];
    }

    for (int i = 0; i < 4; ++i) {
      int d_ = (d + i) % 4, x_ = x + dx[d_], y_ = y + dy[d_];
      //--l;
      if (in_range(x_, H) && in_range(y_, W) && C[x_][y_] != '#') {
        x = x_;
        y = y_;
        --l;
        d = d_;
        break;
      }
    }
  }
  return ret;
}

int main() {
  long long L;
  while (cin >> H >> W >> L, H) {
    vector<string> C(H);
    int s_x, s_y, d;
    for (int i = 0; i < H; ++i) {
      cin >> C[i];
      for (int j = 0; j < W; ++j) {
        if (C[i][j] != '.' && C[i][j] != '#') {
          s_x = i;
          s_y = j;
          if (C[i][j] == 'N')
            d = 0;
          if (C[i][j] == 'E')
            d = 1;
          if (C[i][j] == 'S')
            d = 2;
          if (C[i][j] == 'W')
            d = 3;
        }
      }
    }
    vector<vector<vector<long long int>>> T(
        H, vector<vector<long long int>>(W, vector<long long int>(4, -1ll)));

    cout << dfs(C, T, s_x, s_y, L, d) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}, H, W;
string dir[] = {"N", "E", "S", "W"};

bool in_range(int a, int b) { return a >= 0 && a < b; }

string dfs(vector<string> &C, vector<vector<vector<int>>> &T, int x, int y,
           long long int l, int d) {
  string ret = "!";
  while (ret == "!") {
    if (T[x][y][d] > 0) {
      l %= (T[x][y][d] - l);
    } else {
      T[x][y][d] = l;
    }

    if (l == 0) {
      ret = to_string(x + 1) + " " + to_string(y + 1) + " " + dir[d];
    }

    for (int i = 0; i < 4; ++i) {
      int d_ = (d + i) % 4, x_ = x + dx[d_], y_ = y + dy[d_];
      //--l;
      if (in_range(x_, H) && in_range(y_, W) && C[x_][y_] != '#') {
        x = x_;
        y = y_;
        --l;
        d = d_;
        break;
      }
    }
  }
  return ret;
}

int main() {
  long long L;
  while (cin >> H >> W >> L, H) {
    vector<string> C(H);
    int s_x, s_y, d;
    for (int i = 0; i < H; ++i) {
      cin >> C[i];
      for (int j = 0; j < W; ++j) {
        if (C[i][j] != '.' && C[i][j] != '#') {
          s_x = i;
          s_y = j;
          if (C[i][j] == 'N')
            d = 0;
          if (C[i][j] == 'E')
            d = 1;
          if (C[i][j] == 'S')
            d = 2;
          if (C[i][j] == 'W')
            d = 3;
        }
      }
    }
    vector<vector<vector<int>>> T(H,
                                  vector<vector<int>>(W, vector<int>(4, -1)));
    cout << dfs(C, T, s_x, s_y, L, d) << endl;
  }
  return 0;
}
Problem p01437 (C++) — buggy submission, verdict: Runtime Error

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

#define for_(i, a, b) for (int i = a; i < b; ++i)

typedef long long lint;

int H, W, dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
lint L;
string grid[110], dir = "NESW";

int visit[110][110][4], px[41000], py[41000], pd[41000];

bool able(int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H)
    return false;
  return grid[y][x] == '.';
}

int main() {
  while (cin >> H >> W >> L, H) {
    int x = 0, y = 0, d = 0;

    for_(i, 0, H) {
      cin >> grid[i];
      for_(j, 0, W) if (grid[i][j] != '.' && grid[i][j] != '#') {
        x = j;
        y = i;
        d = dir.find(grid[i][j]);
        grid[i][j] = '.';
      }
    }

    memset(visit, -1, sizeof(visit));

    lint s = 0;
    while (visit[y][x][d] < 0) {
      visit[y][x][d] = s;
      px[s] = x;
      py[s] = y;
      pd[s] = d;
      while (!able(x + dx[d], y + dy[d]))
        d = (d + 1) % 4;
      x += dx[d];
      y += dy[d];
      ++s;
    }

    lint t = visit[y][x][d], id = L;
    if (s < L)
      id = t + (L - t) % (s - t);

    cout << py[id] + 1 << " " << px[id] + 1 << " " << dir[pd[id]] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

int H, W, L, dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
string grid[110];

int ctd(char c) {
  int res = 0;
  if (c == 'E')
    res = 1;
  if (c == 'S')
    res = 2;
  if (c == 'W')
    res = 3;
  return res;
}

char dtc(int d) {
  char res = 'N';
  if (d == 1)
    res = 'E';
  if (d == 2)
    res = 'S';
  if (d == 3)
    res = 'W';
  return res;
}

bool able(int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H)
    return false;
  return grid[y][x] == '.';
}

int main() {
  while (cin >> H >> W >> L, H) {
    int x = 0, y = 0, d = 0;

    for_(i, 0, H) {
      cin >> grid[i];
      for_(j, 0, W) if (grid[i][j] != '.' && grid[i][j] != '#') {
        x = j;
        y = i;
        d = ctd(grid[i][j]);
        grid[i][j] = '.';
      }
    }

    set<piii> visit;
    vector<piii> root;
    while (visit.find(piii(pii(y, x), d)) == visit.end()) {
      visit.insert(piii(pii(y, x), d));
      root.push_back(piii(pii(y, x), d));
      while (!able(x + dx[d], y + dy[d]))
        d = (d + 1) % 4;
      x += dx[d];
      y += dy[d];
    }

    int ind = -1, roots = root.size();
    for_(i, 0, roots) if (root[i] == piii(pii(y, x), d)) {
      ind = i;
      break;
    }

    int id = ((L - ind) % (roots - ind)) + ind;
    piii ans = root[id];
    cout << ans.first.first + 1 << " " << ans.first.second + 1 << " "
         << dtc(ans.second) << endl;
  }

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

#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int h, w;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
string mp[101], str = "NESW";
;
ll n;

void solve(int x, int y, int dir) {
  ll dis[4][101][101] = {}, cost = 0;
  memset(dis, -1, sizeof(dis));
  while (n != 0) {
    if (dis[dir][y][x] != -1 && n % (cost - dis[dir][y][x]) != 0) {
      n %= (cost - dis[dir][y][x]);
      memset(dis, -1, sizeof(dis));
    }

    dis[dir][y][x] = cost;

    x += dx[dir];
    y += dy[dir];
    if (x < 0 || y < 0 || x >= w || y >= h || mp[y][x] == '#') {
      x -= dx[dir];
      y -= dy[dir];
      dir = (dir + 1) % 4;
    } else
      n--, cost++;
  }
  cout << y + 1 << " " << x + 1 << " " << str[dir] << endl;
}

int main() {
  while (1) {
    cin >> h >> w >> n;
    if (!h && !w && !n)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    int dir;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < mp[i].size(); j++)
        if (mp[i][j] != '.' && mp[i][j] != '#') {
          if (mp[i][j] == 'N')
            dir = 0;
          if (mp[i][j] == 'E')
            dir = 1;
          if (mp[i][j] == 'S')
            dir = 2;
          if (mp[i][j] == 'W')
            dir = 3;
          solve(j, i, dir);
        }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
string mp[101];
int h, w;
ll n;

void move(int x, int y, int dir) {
  string str = "NESW";
  int visited[101][101];
  ll dis[101][101];
  memset(visited, -1, sizeof(visited));
  visited[y][x] = dir;
  dis[y][x] = 0;
  if (h == 1 && w == 1)
    n = 0;

  while (n != 0) {
    int nx = x + dx[dir];
    int ny = y + dy[dir];
    int flg = 1;

    if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx] == '#') {
      flg = 0;
      dir = (dir + 1) % 4;
      nx = x, ny = y;
    }

    if (visited[ny][nx] == dir) {
      if (dis[y][x] - dis[ny][nx] + 1 == 0)
        break;
      n %= (dis[y][x] - dis[ny][nx] + 1);
      memset(visited, -1, sizeof(visited));
    }

    dis[ny][nx] = dis[y][x] + flg;
    n -= flg;
    visited[ny][nx] = dir;
    x = nx, y = ny;
  }
  cout << y + 1 << " " << x + 1 << " " << str[dir] << endl;
}

int main() {
  while (1) {
    cin >> h >> w >> n;
    cout << n << endl;
    if (!h && !w && !n)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    int dir, sx, sy;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < mp[i].size(); j++) {
        if (mp[i][j] == '.' || mp[i][j] == '#')
          continue;
        sx = j, sy = i;
        if (mp[i][j] == 'N')
          dir = 0;
        if (mp[i][j] == 'E')
          dir = 1;
        if (mp[i][j] == 'S')
          dir = 2;
        if (mp[i][j] == 'W')
          dir = 3;
      }
    move(sx, sy, dir);
  }
  return 0;
}