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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#define cdebug()                                                               \\
  freopen("C:\\\\Users\\\\netmaster.ASUS\\\\Desktop\\\\123.txt", "r", stdin)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rev(i, a, b) for (int i = (a); i >= (b); i--)
#define clr(a, x) memset(a, x, sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

const int mod = 1e9 + 7;
const int maxn = 2005;
const int maxm = 4005;

int done[8][8][8][8][8][8][8];
struct node {
  int d[8], w, dir;
  node() {}
  node(int dd[], int di, int ww) {
    rep(i, 0, 8) d[i] = dd[i];
    dir = di;
    w = ww;
  }
};
inline int &gbool(int dd[]) {
  return done[dd[0]][dd[1]][dd[2]][dd[3]][dd[4]][dd[5]][dd[6]];
}
int dx[][3] = {1, 4, -1, 0, 2, 5, 1, 3, 6, 2, 7, -1,
               0, 5, -1, 1, 4, 6, 2, 5, 7, 3, 6, -1};
void bfs() {
  int dd[8];
  queue<node> q;
  clr(done, 0);
  rep(i, 0, 8) dd[i] = i;
  q.push(node(dd, 0, 0));
  gbool(dd) = 1;
  while (!q.empty()) {
    node x = q.front();
    q.pop();
    rep(i, 0, 3) if (dx[x.dir][i] != -1) {
      node cur = x;
      swap(cur.d[x.dir], cur.d[dx[x.dir][i]]);
      cur.dir = dx[x.dir][i];
      cur.w++;
      if (!gbool(cur.d)) {
        gbool(cur.d) = x.w + 1;
        q.push(cur);
      }
    }
  }
}
int da[8];
int main() {
  bfs();
  while (~scanf("%d", &da[0])) {
    int sum = 0;
    rep(i, 1, 8) scanf("%d", &da[i]), sum += abs(i - da[i]);

    if (!sum) {
      puts("0");
      continue;
    }
    printf("%d\\n", gbool(da));
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef pair<string, int> P;
const int INF = 100000000;
string a;
int op[4] = {-1, 1, -4, 4};

map<string, int> dp; // 菫晏ュ倅サ市tring蜿伜芦"01234567"逧?nt

// 隶。邂嶺サ?01234567"霓ャ謐「蛻ー蜈カ莉門コ丞?謇?怙逧?怙蟆乗ュ・謨ー
void bfs() {
  // 蛻晏ァ句喧
  queue<P> que;
  que.push(P("01234567", 0));
  dp["01234567"] = 0;
  // 螳ス蠎ヲ莨伜?謳懃エ「
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    string s = p.first;
    int cur = p.second;
    for (int i = 0; i < 4; i++) {
      // 譫??荳倶ク?ャ。莠、謐「
      int next = cur + op[i];
      string str = s;
      swap(str[cur], str[next]);
      map<string, int>::iterator it = dp.find(str);
      // 蛻、譁ュ譏ッ蜷ヲ蜿ッ遘サ蜉ィ莉・蜿頑弍蜷ヲ隶ソ髣ョ霑?            if(0
      // <= next && next < 8&& !(cur == 3 && next == 4) && !(cur == 4 && next ==
      // 3)&& it == dp.end())
      {
        que.push(P(str, next));
        dp[str] = dp[s] + 1;
      }
    }
  }
}

void solve() {
  // 蛻?勁遨コ譬シ
  a.erase(remove(a.begin(), a.end(), ' '), a.end());
  cout << dp[a] << endl;
}

int main() {
  // 蜈磯?蜷第桷騾?園譛画ュ蜀オ?悟錘逶エ謗・隸サ蜿冶セ灘?逕ィ萓狗噪扈捺棡
  bfs();
  while (getline(cin, a)) {
    solve();
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#define N 1100
#define INF 2 << 30
using namespace std;
map<string, int> mp;
void bfs() {
  int p;
  mp.clear();
  string ss = "01234567";
  queue<string> q;
  int dir[4] = {1, -1, 4, -4};
  q.push(ss);
  mp[ss] = 0;
  while (!q.empty()) {
    string now = q.front();
    q.pop();
    for (int i = 0; i < 8; i++) {
      if (now[i] == '0') {
        p = i;
        break;
      }
    }
    for (int k = 0; k <= 3; k++) {
      int tx = p + dir[k];
      if ((k == 1 && p == 4) || (k == 0 && p == 3))
        continue;
      string next = now;
      if (tx >= 0 && tx < 8) {
        swap(next[tx], next[p]);
        if (mp[next] == 0) {
          mp[next] = mp[now] + 1;
          q.push(next);
        }
      }
    }
  }
}
int main() {
  string s, str;
  bfs();
  while (getline(cin, s)) {
    str.clear();
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != ' ')
        str = str + s[i];
    }
    cout << mp[str] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

//
// Created by Tnecesoc on 2017/3/26.
//

#include <map>
#include <queue>
#include <stdio.h>

using namespace std;

typedef struct {
  int s, t;
} Node;

const int ds[]{4, 1, -4, -1};
const int INF = 0x3fffffff;

int es = 0x01234567;

void swap_bit(int *a, int bit_at_a, int *b, int bit_at_b) {
  int b1 = (*a >> (bit_at_a)) & 1;
  int b2 = (*b >> (bit_at_b)) & 1;
  *a &= ~(1 << bit_at_a);
  *b &= ~(1 << bit_at_b);
  *a |= (b2 << (bit_at_a));
  *b |= (b1 << (bit_at_b));
}

int bfs(int fs) {
  Node p, tp;
  map<int, bool> vis;

  queue<Node> q;
  q.push({fs, 0});
  vis.insert(make_pair(fs, true));

  while (q.size()) {
    p = q.front();
    q.pop();

    if (p.s == es) {
      vis.clear();
      while (q.size()) {
        q.pop();
      }
      return p.t;
    }

    for (int i = 0, mask = 0x07; i < 8; ++i) {

      if ((p.s & mask) == 0) {
        for (int j = 0; j < 4; ++j) {
          int k = i + ds[j];
          if (k >= 0 && k < 8 && !(i == 3 && k == 4 || i == 4 && k == 3)) {
            tp.s = p.s;
            for (int l = 0; l < 4; ++l) {
              swap_bit(&tp.s, 4 * i + l, &tp.s, 4 * k + l);
            }
            tp.t = p.t + 1;
            if (!vis.count(tp.s)) {
              q.push(tp);
              vis.insert(make_pair(tp.s, true));
            }
          }
        }

        break;
      }

      mask <<= 4;
    }
  }

  vis.clear();
  while (q.size()) {
    q.pop();
  }

  return INF;
}

int main() {

  int digit[8], fs;

  while (~scanf("%d", &digit[7])) {
    for (int i = 6; i >= 0; --i) {
      scanf("%d", &digit[i]);
    }

    fs = 0;
    for (int i = 0; i < 8; ++i) {
      fs |= digit[i] << (i << 2);
    }

    int ans = bfs(fs);
    printf("%d\\n", ans == INF ? -1 : ans);
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int p[8];
int d[4] = {-1, 1, -4, 4};
map<string, int> m;

int dfs() {
  string s = "01234567";
  int count = 0;
  int pos;

  queue<string> q;
  q.push(s);
  m[s] = count;
  while (q.size()) {
    s = q.front();
    q.pop();
    count = m[s];
    pos = s.find('0');

    for (int i = 0; i < 4; i++) {
      if (0 <= pos + d[i] && pos + d[i] < 8 && !(pos == 3 && d[i] == 1) &&
          !(pos == 4 && d[i] == -1)) {
        string s_next = s;
        swap(s_next[pos], s_next[pos + d[i]]);
        if (m.find(s_next) == m.end()) {
          q.push(s_next);
          m[s_next] = count + 1;
        }
      }
    }
  }

  return 0;
}

int main() {
  dfs();

  while (cin >> p[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> p[i];
    }
    stringstream ss;

    for (int i = 0; i < 8; i++) {
      ss << p[i];
    }
    string s = ss.str();

    cout << m[s] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;
typedef pair<string, int> P;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int p[8];
int d[4] = {-1, 1, -4, 4};

int return_pos(string s) {
  for (int i = 0; i < 8; i++) {
    if (s[i] == '0')
      return i;
  }
  return 0;
}

vector<int> change_place(vector<int> vec, int i, int j) {
  int tmp = vec[i];
  vec[i] = vec[j];
  vec[j] = tmp;
  return vec;
}

int solve() {
  map<string, int> m;
  stringstream ss;
  for (int i = 0; i < 8; i++) {
    ss << p[i];
  }
  string s = ss.str();
  int count = 0;
  int pos;

  queue<P> q;
  q.push(P(s, count));
  m[s] = count;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s = p.first;
    count = p.second;
    pos = return_pos(s);

    if (s == "01234567")
      break;

    for (int i = 0; i < 4; i++) {
      if (0 <= pos + d[i] && pos + d[i] < 8) {
        string s_next = s;
        swap(s_next[pos], s_next[pos + d[i]]);
        if (m.find(s_next) == m.end()) {
          q.push(P(s_next, count + 1));
          m[s_next] = count + 1;
        }
      }
    }
  }

  cout << count << endl;
  return 0;
}

int main() {
  while (cin >> p[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> p[i];
    }
    solve();
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
#define f first
#define s second
using namespace std;
int t[8][8][8][8][8][8][8][8];
int dx[] = {1, -1, 4, -4};
void plu(string x, int p) {
  t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]] = p;
}
bool ret(string x) { return t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]]; }
int get(int x[8]) { return t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]]; }
int main() {
  string v, a, x;
  r(i, 8) v += i;
  queue<pair<string, int>> q;
  plu(v, 0);
  q.push(mk(v, 0));
  while (!q.empty()) {
    pair<string, int> p = q.front();
    q.pop();
    x = p.f;
    plu(p.f, p.s);
    r(i, 8) if (!x[i]) r(j, 4) {
      if (j == 0 && i == 3)
        continue;
      if (j == 1 && i == 4)
        continue;
      int y = i + dx[j];
      if (y >= 0 && y < 8) {
        a = x;
        swap(a[i], a[y]);
        if (!ret(a)) {
          q.push(mk(a, p.s + 1));
          plu(a, p.s + 1);
        }
      }
    }
  }
  int qq[8];
  while (cin >> qq[0]) {
    r(i, 7) cin >> qq[i + 1];
    cout << get(qq) << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
#define f first
#define s second
using namespace std;
int t[8][8][8][8][8][8][8][8];
int dx[] = {1, -1, 4, -4};
void plu(string x, int p) {
  t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]] = p;
}
bool ret(string x) { return t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]]; }
int get(int x[8]) { return t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]]; }
int main() {
  string v = "01234567", a, x;
  queue<pair<string, int>> q;
  plu(v, 0);
  q.push(mk(v, 0));
  while (!q.empty()) {
    pair<string, int> p = q.front();
    q.pop();
    x = p.f;
    plu(p.f, p.s);
    r(i, 8) if (!x[i]) r(j, 4) {
      if (j == 0 && i == 3)
        continue;
      if (j == 1 && i == 4)
        continue;
      int y = i + dx[j];
      if (y >= 0 && y < 8) {
        a = x;
        if (p.s > 5)
          return 0;
        swap(a[i], a[y]);
        if (!ret(a)) {
          q.push(mk(a, p.s + 1));
          plu(a, p.s + 1);
        }
      }
    }
  }
  int qq[8];
  while (cin >> qq[0]) {
    r(i, 7) cin >> qq[i + 1];
    cout << get(qq) << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int toInt(const string &s) {
  int res = 0;
  for (int i = 0; i < s.size(); ++i) {
    res = res * 10 + (s[i] - '0');
  }
  return res;
}

map<int, int> cost;

void bfs() {
  const string s = "01234567";
  static int di[] = {0, 1, 0, -1};
  static int dj[] = {1, 0, -1, 0};
  queue<string> que;
  que.push(s);
  cost[toInt(s)] = 0;
  while (que.size()) {
    const string n = que.front();
    const int in = toInt(n);
    que.pop();
    const int p = n.find('0');
    const int pi = p / 4;
    const int pj = p % 4;
    for (int k = 0; k < 4; ++k) {
      int qi = pi + di[k];
      int qj = pj + dj[k];
      if (qi < 0 || qi >= 2)
        continue;
      if (qj < 0 || qj >= 4)
        continue;
      int q = qi * 4 + qj;
      string nn = n;
      swap(nn[p], nn[q]);
      int inn = toInt(nn);
      if (cost.count(inn))
        continue;
      que.push(nn);
      cost[inn] = cost[in] + 1;
    }
  }
}

int main() {
  bfs();
  while (1) {
    string s(8, ' ');
    for (int i = 0; i < 8; ++i) {
      if (cin >> s[i])
        ;
      else
        return 0;
    }
    cout << cost[toInt(s)] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int toInt(const string &s) {
  int res = 0;
  for (int i = 0; i < s.size(); ++i) {
    res = res * 10 + (s[i] - '0');
  }
  return res;
}

int bfs(const string &s, const string &t) {
  static int di[] = {0, 1, 0, -1};
  static int dj[] = {1, 0, -1, 0};
  if (s == t)
    return 0;
  queue<string> que;
  map<int, int> cost;
  que.push(s);
  cost[toInt(s)] = 0;
  while (que.size()) {
    const string n = que.front();
    const int in = toInt(n);
    que.pop();
    const int p = n.find('0');
    const int pi = p / 4;
    const int pj = p % 4;
    for (int k = 0; k < 4; ++k) {
      int qi = pi + di[k];
      int qj = pj + dj[k];
      if (qi < 0 || qi >= 2)
        continue;
      if (qj < 0 || qj >= 4)
        continue;
      int q = qi * 4 + qj;
      string nn = n;
      swap(nn[p], nn[q]);
      int inn = toInt(nn);
      if (cost.count(inn))
        continue;
      que.push(nn);
      cost[inn] = cost[in] + 1;
      if (nn == t)
        return cost[inn];
    }
  }
  return -1;
}

int main() {
  while (1) {
    string s(8, ' ');
    for (int i = 0; i < 8; ++i) {
      if (cin >> s[i])
        ;
      else
        return 0;
    }
    cout << bfs(s, "01234567") << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>

using namespace std;

int dd[] = {1, -1, 4, -4};
map<string, int> done;

void dfs() {
  // その時のカードの状態をqueueに突っ込む
  queue<string> que;

  que.push("01234567");
  while (!que.empty()) {
    string q = que.front();
    que.pop();
    int x0 = q.find("0");
    for (int i = 0; i < 4; i++) {
      string t = q;
      int nx = x0 + dd[i];
      if (nx < 0 || 7 < nx || (x0 == 3 && i == 0) || (x0 == 4 && i == 1))
        continue;
      t[x0] = t[nx];
      t[nx] = '0';
      if (done.count(t))
        continue;
      done[t] = done[q] + 1;
      que.push(t);
    }
  }
}

int main() {
  string t;
  dfs();
  while (getline(cin, t)) {
    t.erase(remove(t.begin(), t.end(), ' '), t.end());
    cout << done[t] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>

using namespace std;

int dd[] = {1, -1, 4, -4};

int dfs(string cards) {
  // その時のカードの状態をqueueに突っ込む
  map<string, int> done;
  queue<string> que;

  if (cards == "01234567")
    return 0;

  que.push(cards);
  while (!que.empty()) {
    string q = que.front();
    que.pop();
    int x0 = q.find("0");
    for (int i = 0; i < 4; i++) {
      string t = q;
      int nx = x0 + dd[i];
      if (nx < 0 || 7 < nx || (x0 == 3 && i == 0) || (x0 == 4 && i == 1))
        continue;
      t[x0] = t[nx];
      t[nx] = '0';
      if (done.count(t))
        continue;
      done[t] = done[q] + 1;
      if (t == "01234567")
        return done[t];
      que.push(t);
    }
  }
  return 0;
}

int main() {
  string t;
  while (getline(cin, t)) {
    t.erase(remove(t.begin(), t.end(), ' '), t.end());
    cout << dfs(t) << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>

using namespace std;

const int MAX_N = 8;
const int RULE_NUM = 3;
const int RULE[MAX_N][RULE_NUM] = {{1, 4, -1}, {0, 2, 5},  {1, 3, 6},
                                   {2, 7, -1}, {0, 5, -1}, {1, 4, 6},
                                   {2, 5, 7},  {3, 6, -1}};

map<string, int> BFS() {
  string goal_numbers;
  for (size_t i = 0; i < MAX_N; i++) {
    goal_numbers.push_back(i + '0');
  }

  map<string, int> distances;
  queue<string> que;
  que.push(goal_numbers);
  distances[goal_numbers] = 0;

  while (que.size()) {
    string now_numbers = que.front();
    que.pop();

    auto zero_pos = now_numbers.find('0');
    for (size_t i = 0; i < RULE_NUM; i++) {
      int new_zero_pos = RULE[zero_pos][i];
      if (new_zero_pos < 0) {
        continue;
      }
      string new_numbers = now_numbers;
      swap(new_numbers[zero_pos], new_numbers[new_zero_pos]);
      if (distances.find(new_numbers) == distances.end()) {
        que.push(new_numbers);
        distances[new_numbers] = distances[now_numbers] + 1;
      }
    }
  }

  return distances;
}

int main() {
  map<string, int> distances = BFS();

  string line;
  while (getline(cin, line)) {
    stringstream ss;
    ss << line;
    string numbers;
    for (size_t i = 0; i < MAX_N; i++) {
      char c;
      ss >> c;
      numbers += c;
    }
    cout << distances[numbers] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <utility>

using namespace std;

const int MAX_N = 8;
const string GOAL_NUMBERS = "01234567";
const int RULE_NUM = 3;
const int RULE[MAX_N][RULE_NUM] = {{1, 4, -1}, {0, 2, 5},  {1, 3, 6},
                                   {2, 7, -1}, {0, 5, -1}, {1, 4, 6},
                                   {2, 5, 7},  {3, 6, -1}};
const int FACTORS[MAX_N] = {1, 1, 2, 6, 24, 120, 720, 5040};

int BFS(string numbers) {
  map<string, int> dists;
  dists[numbers] = 0;
  queue<string> que;
  que.push(numbers);

  while (que.size()) {
    auto now_numbers = que.front();
    que.pop();
    if (now_numbers == GOAL_NUMBERS) {
      break;
    }

    int now_zero_pos = now_numbers.find('0');
    for (size_t i = 0; i < RULE_NUM; i++) {
      int new_zero_pos = RULE[now_zero_pos][i];
      if (new_zero_pos < 0) {
        continue;
      }
      string new_numbers = now_numbers;
      swap(new_numbers[now_zero_pos], new_numbers[new_zero_pos]);
      if (dists.find(new_numbers) == dists.end()) {
        que.push(new_numbers);
        dists[new_numbers] = dists[now_numbers] + 1;
      }
    }
  }

  return dists[GOAL_NUMBERS];
}

int main() {
  string line;
  while (getline(cin, line)) {
    stringstream ss;
    ss << line;
    string numbers = "";
    for (size_t i = 0; i < MAX_N; i++) {
      char c;
      ss >> c;
      numbers += c;
    }
    cout << BFS(numbers) << endl;
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using Data = struct {
  int pos;
  string puzzle;
  int t;
};

constexpr int dx[] = {-1, 1, -4, 4};
const string correct{"01234567"};

int main() {
  map<string, int> min_root{{correct, 0}};

  queue<Data> q;
  q.push(Data{0, correct, 0});
  while (!q.empty()) {
    Data d = q.front();
    q.pop();

    for (int i = 0; i < 4; ++i) {
      int npos = d.pos + dx[i];

      if (npos < 0 || npos > 7 || (d.pos == 3 && npos == 4) ||
          (d.pos == 4 && npos == 3))
        continue;

      string npuzzle = d.puzzle;
      swap(npuzzle[d.pos], npuzzle[npos]);

      if (min_root.find(npuzzle) != end(min_root))
        continue;

      min_root.emplace(make_pair(npuzzle, d.t + 1));
      q.push(Data{npos, npuzzle, d.t + 1});
    }
  }

  cin.tie(0);
  ios::sync_with_stdio(false);

  string puzzle{"********"};

  while (cin >> puzzle[0] >> puzzle[1] >> puzzle[2] >> puzzle[3] >> puzzle[4] >>
         puzzle[5] >> puzzle[6] >> puzzle[7]) {
    cout << min_root[puzzle] << endl;
  }
}


Error: TLE

Fixed submission:

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

using Data = struct {
  int pos;
  string puzzle;
  int t;
};

constexpr int dx[] = {-1, 1, -4, 4};
const string correct{"01234567"};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string puzzle{"********"};
  int sp;
  Data cd;

  while (cin >> puzzle[0] >> puzzle[1] >> puzzle[2] >> puzzle[3] >> puzzle[4] >>
         puzzle[5] >> puzzle[6] >> puzzle[7]) {
    queue<Data> q;
    sp = distance(begin(puzzle), find(begin(puzzle), end(puzzle), '0'));
    q.push(Data{sp, puzzle, 0});

    unordered_map<string, bool> visited;

    while (!q.empty()) {
      cd = q.front();
      q.pop();

      if (cd.puzzle == correct) {
        cout << cd.t << endl;
        break;
      }

      for (int i = 0, npos; i < 4; ++i) {
        npos = cd.pos + dx[i];

        if (npos < 0 || npos > 7 || (cd.pos == 3 && npos == 4) ||
            (cd.pos == 4 && npos == 3))
          continue;

        string npuzzle = cd.puzzle;
        swap(npuzzle[cd.pos], npuzzle[npos]);

        try {
          if (visited.at(npuzzle) == true)
            continue;
        } catch (out_of_range &) {
          visited.emplace(make_pair(npuzzle, true));
        }

        q.push(Data{npos, npuzzle, cd.t + 1});
      }
    }
  }
}
Problem p00122 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int bfs(int sy, int sx, int n);
int d[22][22][22];
// 構造体
struct P {
  int y;
  int x;
  int c;
  P(int y, int x, int c) : y(y), x(x), c(c) {}
};
int dy[] = {0, 0, 0, 1, -1, -1, -1, 1, 1}; //    スプリンクラーの
int dx[] = {0, 1, -1, 0, 0, -1, 1, -1, 1}; // 　　座標
int ddy[] = {-2, -2, -2, -1, 0, 1, 2, 2, 2, 1, 0, -1};
int ddx[] = {-1, 0, 1, 2, 2, 2, 1, 0, -1, -2, -2, -2};
int main() {
  int sy, sx, y, x;
  int n;
  while (1) {
    memset(d, 0, sizeof(d));
    cin >> sx >> sy;
    if (!sy && !sx)
      break;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      for (int j = 0; j < 9; j++) {
        int ty = y + dy[j];
        int tx = x + dx[j];
        if (0 <= ty && ty < 10 && 0 <= tx && tx < 10) {
          d[ty][tx][i] = 1;
        }
      }
    }
    int ret = bfs(sy, sx, n);
    if (ret == 1) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
int bfs(int sy, int sx, int n) {
  queue<P> que;
  que.push(P(sy, sx, 0));

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.c == n) {
      return 1;
    }
    for (int i = 0; i < 12; i++) {
      int ty = p.y + ddy[i];
      int tx = p.x + ddx[i];
      if (0 <= ty && ty < 10 && 0 <= tx && tx < 10) {
        if (d[ty][tx][p.c] == 1) {
          que.push(P(ty, tx, p.c + 1));
        }
      }
    }
  }
  return -1;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct P {
  int y;
  int x;
  int c;
  P() {}
  P(int y, int x, int c) : y(y), x(x), c(c) {}
};
int n;
int f_dy[] = {2, 2, 2, -2, -2, -2, 0, 1, -1, 0, 1, -1};
int f_dx[] = {0, 1, -1, 0, 1, -1, 2, 2, 2, -2, -2, -2};
int dy[] = {0, 0, 0, 1, -1, -1, -1, 1, 1};
int dx[] = {0, 1, -1, 0, 0, -1, 1, -1, 1};
int d[11][11][11]; // 座標,何個目か
int dfs(int sy, int sx, int v);
int main() {
  int sy, sx;
  int a, b;
  while (1) {
    cin >> sy >> sx;
    if (!sy && !sx)
      break;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      for (int j = 0; j < 9; j++) {
        d[a + dy[j]][b + dx[j]][i] = 1;
      }
    }
    int ret = dfs(sy, sx, 0);
    if (ret == 1) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
int dfs(int sy, int sx, int v) {

  queue<P> que;
  que.push(P(sy, sx, v));

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.c == n - 1)
      return 1;

    for (int i = 0; i < 12; i++) {
      int ty = p.y + f_dy[i];
      int tx = p.x + f_dx[i];
      if (d[ty][tx][p.c + 1] == 1) {
        que.push(P(ty, tx, p.c + 1));
        d[ty][tx][p.c + 1] = 0;
      }
    }
  }
  return -1;
}