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

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

struct pz {
  int a[9];
  int t;

  bool operator<(const pz &p) const {
    for (int i = 0; i < 9; i++) {
      if (a[i] == p.a[i])
        continue;
      return a[i] > p.a[i];
    }
    return false;
  }
};

int main() {
  pz p;
  int n = -1;
  map<pz, bool> v;

  for (int i = 0; i < 9; i++)
    cin >> p.a[i];

  p.t = 0;

  queue<pz> que;
  que.push(p);
  v[p] = true;

  while (!que.empty()) {
    pz g = que.front();
    que.pop();

    bool f = 1;

    for (int i = 0; i < 8; i++) {
      if (g.a[i] != (i + 1)) {
        f = 0;
        break;
      }
    }

    if (f == 1) {
      n = g.t;
      break;
    }

    for (int i = 0; i < 9; i++) {
      if (g.a[i] == 0) {
        if (i % 3 != 0) {
          g.t++;
          swap(g.a[i], g.a[i - 1]);
          if (!v[g]) {
            v[g] = true;
            que.push(g);
          }
          swap(g.a[i], g.a[i - 1]);
          g.t--;
        }
        if (i % 3 != 2) {
          g.t++;
          swap(g.a[i], g.a[i + 1]);
          if (!v[g]) {
            v[g] = true;
            que.push(g);
          }
          swap(g.a[i], g.a[i + 1]);
          g.t--;
        }
        if (i / 3 != 0) {
          g.t++;
          swap(g.a[i], g.a[i - 3]);
          if (!v[g]) {
            v[g] = true;
            que.push(g);
          }
          swap(g.a[i], g.a[i - 3]);
          g.t--;
        }
        if (i / 3 != 2) {
          g.t++;
          swap(g.a[i], g.a[i + 3]);
          if (!v[g]) {
            v[g] = true;
            que.push(g);
          }
          swap(g.a[i], g.a[i + 3]);
          g.t--;
        }
      }
    }
  }

  cout << n << endl;

  return 0;
}


Error: MLE

Fixed submission:

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

struct pz {
  int a[9];
  int t;
};

int main() {
  pz p;
  int n = -1;

  for (int i = 0; i < 9; i++)
    cin >> p.a[i];

  p.t = 0;

  queue<pz> que;
  que.push(p);

  while (!que.empty()) {
    pz g = que.front();
    que.pop();

    bool f = 1;

    for (int i = 0; i < 8; i++) {
      if (g.a[i] != (i + 1)) {
        f = 0;
        break;
      }
    }

    if (f == 1) {
      n = g.t;
      break;
    }

    for (int i = 0; i < 9; i++) {
      if (g.a[i] == 0) {
        if (i % 3 != 0) {
          g.t++;
          swap(g.a[i], g.a[i - 1]);
          que.push(g);
          swap(g.a[i], g.a[i - 1]);
          g.t--;
        }
        if (i % 3 != 2) {
          g.t++;
          swap(g.a[i], g.a[i + 1]);
          que.push(g);
          swap(g.a[i], g.a[i + 1]);
          g.t--;
        }
        if (i / 3 != 0) {
          g.t++;
          swap(g.a[i], g.a[i - 3]);
          que.push(g);
          swap(g.a[i], g.a[i - 3]);
          g.t--;
        }
        if (i / 3 != 2) {
          g.t++;
          swap(g.a[i], g.a[i + 3]);
          que.push(g);
          swap(g.a[i], g.a[i + 3]);
          g.t--;
        }
      }
    }
  }

  cout << n << endl;

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

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
queue<pair<string, int>> Q;

void Sw(string st, int x) {
  int i;
  int pos, npos;
  int d[4] = {-3, 3, -1, 1};
  string st1;

  for (i = 0; i <= 8; i++) {
    if (st[i] == '0') {
      pos = i;
      break;
    }
  }
  for (i = 0; i <= 3; i++) {
    st1 = st;
    if (i == 2 && pos % 3 == 0)
      continue;
    if (i == 3 && pos % 3 == 2)
      continue;
    npos = pos + d[i];
    if (npos >= 9 || npos <= -1)
      continue;
    swap(st1[pos], st1[npos]);
    Q.push(pair<string, int>(st1, x + 1));
  }
}

int main() {
  string puzzle = "";
  string goal = "123456780";
  map<string, bool> M;
  int i, x;
  string P;

  for (i = 0; i <= 8; i++) {
    cin >> x;
    puzzle += '0' + x;
  }
  Q.push(pair<string, int>(puzzle, 0));
  while (1) {
    pair<string, int> p = Q.front();
    Q.pop();
    P = p.first;
    x = p.second;
    if (P == goal) {
      cout << x << endl;
      break;
    }
    if (M[P])
      continue;
    else
      M[P] = 1;
    Sw(P, x);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
queue<pair<string, int>> Q;

void Sw(string st, int x) {
  int i;
  int pos, npos;
  int d[4] = {-3, 3, -1, 1};
  string st1;

  for (i = 0; i <= 8; i++) {
    if (st[i] == '0') {
      pos = i;
      break;
    }
  }
  for (i = 0; i <= 3; i++) {
    st1 = st;
    if (i == 2 && pos % 3 == 0)
      continue;
    if (i == 3 && pos % 3 == 0)
      continue;
    npos = pos + d[i];
    if (npos >= 9 || npos <= -1)
      continue;
    swap(st1[pos], st1[npos]);
    Q.push(pair<string, int>(st1, x + 1));
  }
}

int main() {
  string puzzle = "";
  string goal = "123456780";
  map<string, bool> M;
  int i, x;
  string P;

  for (i = 0; i <= 8; i++) {
    cin >> x;
    puzzle += '0' + x;
  }
  Q.push(pair<string, int>(puzzle, 0));
  while (1) {
    pair<string, int> p = Q.front();
    Q.pop();
    P = p.first;
    x = p.second;
    if (P == goal) {
      cout << x << endl;
      break;
    }
    if (M[P])
      continue;
    else
      M[P] = 1;
    Sw(P, x);
  }
  return 0;
}
Problem p02245 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

#define N 3
#define N2 9

struct Puzzle {
  int f[N2];
  int space;
  string path;

  bool operator<(const Puzzle p) const {
    for (int i = 0; i < N2; ++i) {
      if (f[i] == p.f[i])
        continue;
      return f[i] > p.f[i];
    }
    return false;
  }
};

bool isComplete(Puzzle p) {
  for (int i = 0; i < N2; ++i) {
    if (p.f[i] != i + 1)
      return false;
  }
  return true;
}

const int dx[4]{-1, 0, 1, 0};
const int dy[4]{0, -1, 0, 1};
const char dir[4]{'u', 'l', 'd', 'r'};

string bfs(Puzzle p) {
  queue<Puzzle> Q;
  p.path = "";
  Q.push(p);
  Puzzle u, v;
  map<Puzzle, bool> M;
  M[p] = true;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    if (isComplete(u))
      return u.path;
    int sx = u.space / N;
    int sy = u.space % N;
    for (int r = 0; r < 4; ++r) {
      int tx = sx + dx[r];
      int ty = sy + dy[r];
      if (tx < 0 || ty < 0 || tx >= N || ty >= N)
        continue;
      v = u;
      swap(v.f[u.space], v.f[tx * N + ty]);
      v.space = tx * N + ty;
      if (!M[v]) {
        M[v] = true;
        v.path += dir[r];
        Q.push(v);
      }
    }
  }

  return "unresolved";
}

int main() {
  Puzzle p;
  for (int i = 0; i < N2; ++i) {
    scanf("%d", &p.f[i]);
    if (p.f[i] == 0) {
      p.f[i] = N2;
      p.space = i;
    }
  }
  string ans = bfs(p);
  cout << ans.size() << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

#define N 3
#define N2 9

struct Puzzle {
  int f[N2];
  int space;
  string path;

  //	bool operator <(const Puzzle p) const {
  //		for (int i = 0; i < N2; ++i) {
  //			if (f[i] == p.f[i])
  //				continue;
  //			return f[i] > p.f[i];
  //		}
  //		return false;
  //	}
};

bool isComplete(Puzzle p) {
  for (int i = 0; i < N2; ++i) {
    if (p.f[i] != i + 1)
      return false;
  }
  return true;
}

const int dx[4]{-1, 0, 1, 0};
const int dy[4]{0, -1, 0, 1};
const char dir[4]{'u', 'l', 'd', 'r'};

string bfs(Puzzle p) {
  queue<Puzzle> Q;
  p.path = "";
  Q.push(p);
  Puzzle u, v;
  map<Puzzle, bool> M;
  //	M[p] = true;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    if (isComplete(u))
      return u.path;
    int sx = u.space / N;
    int sy = u.space % N;
    for (int r = 0; r < 4; ++r) {
      int tx = sx + dx[r];
      int ty = sy + dy[r];
      if (tx < 0 || ty < 0 || tx >= N || ty >= N)
        continue;
      v = u;
      swap(v.f[u.space], v.f[tx * N + ty]);
      v.space = tx * N + ty;
      //			if (!M[v]) {
      //				M[v] = true;
      v.path += dir[r];
      Q.push(v);
      //			}
    }
  }

  return "unresolved";
}

int main() {
  Puzzle p;
  for (int i = 0; i < N2; ++i) {
    scanf("%d", &p.f[i]);
    if (p.f[i] == 0) {
      p.f[i] = N2;
      p.space = i;
    }
  }
  string ans = bfs(p);
  cout << ans.size() << endl;

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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <unordered_map>

int main() {
  std::unordered_map<std::string, int> ss;
  std::queue<std::pair<std::string, int>> q;
  q.push(std::make_pair("123456780", 0));
  ss["123456780"] = 0;
  while (q.size()) {
    auto p = q.front();
    q.pop();
    auto s = p.first;
    auto d = p.second;
    auto z = std::find(s.begin(), s.end(), '0') - s.begin();
    auto x = z % 3, y = z / 3;
    int dxs[] = {1, -1, 0, 0}, dys[] = {0, 0, 1, -1};
    for (size_t i = 0; i < 4; i++) {
      int a = x + dxs[i], b = y + dys[i];
      if (0 <= a && a < 3 && 0 <= b && b < 3) {
        std::swap(s[3 * y + x], s[3 * b + a]);
        if (ss.find(s) == ss.end()) {
          ss[s] = d + 1;
          q.push(std::make_pair(s, d + 1));
        }
        std::swap(s[3 * y + x], s[3 * b + a]);
      }
    }
  }
  std::string s;
  for (size_t i = 0; i < 9; i++) {
    int c;
    scanf("%d", &c);
    s.push_back('0' + c);
  }
  printf("%d\\n", ss[s]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

static int board[9], goal[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};

static bool rec(int n, int x, int y) {
  if (n == 0)
    return std::mismatch(board, board + 9, goal).first == board + 9;
  int dxs[] = {1, -1, 0, 0}, dys[] = {0, 0, 1, -1};
  for (size_t i = 0; i < 4; i++) {
    int p = x + dxs[i], q = y + dys[i];
    if (0 <= p && p < 3 && 0 <= q && q < 3) {
      std::swap(board[3 * y + x], board[3 * q + p]);
      if (rec(n - 1, p, q))
        return true;
      std::swap(board[3 * y + x], board[3 * q + p]);
    }
  }
  return false;
}

int main() {
  for (size_t i = 0; i < 9; i++)
    scanf("%d", board + i);
  int z = std::find(board, board + 9, 0) - board;
  int x = z % 3, y = z / 3;
  for (int n = 0;; n++)
    if (rec(n, x, y)) {
      printf("%d\\n", n);
      break;
    }
  return 0;
}
Problem p02245 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int N = 3;
const int N2 = 9;
const int x[4] = {0, 0, 1, -1};
const int y[4] = {-1, 1, 0, 0};
const char ch[4] = {'l', 'r', 'd', 'u'};

class node {
public:
  int f[N2];
  int space;
  string str;
  bool operator<(const node &n) const {
    for (int i = 0; i < N2; ++i) {
      if (n.f[i] == f[i])
        continue;
      return f[i] > n.f[i];
    }
    return false;
  }
};

bool Is_Return(const node &n) {
  for (int i = 0; i < N2; ++i) {
    if (n.f[i] != i + 1)
      return false;
  }
  return true;
}

string bfs(node &n) {
  queue<node> Q;
  map<node, bool> ans;
  Q.push(n);
  ans[n] = true;
  while (!Q.empty()) {
    node u = Q.front();
    Q.pop();
    if (Is_Return(u))
      return u.str;
    int tx = u.space / N;
    int ty = u.space % N;
    for (int i = 0; i < 4; ++i) {
      int sx = tx + x[i];
      int sy = ty + y[i];
      if (sx < 0 || sy < 0 || sx >= N || sy >= N)
        continue;
      node v = u;
      swap(v.f[v.space], v.f[sx * N + sy]);
      v.space = sx * N + sy;
      if (!ans[v]) {
        ans[v] = true;
        v.str += ch[i];
        Q.push(v);
      }
    }
  }
  return "unsolvable";
}

int main() {
  node n;
  for (int i = 0; i < N2; ++i) {
    cin >> n.f[i];
    if (n.f[i] == 0) {
      n.space = i;
      n.f[i] = N2;
    }
  }
  string s = bfs(n);
  cout << s.length() << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int N = 3;
const int N2 = 9;
const int x[4] = {0, 0, 1, -1};
const int y[4] = {-1, 1, 0, 0};
const char ch[4] = {'l', 'r', 'd', 'u'};

class node {
public:
  int f[N2];
  int space;
  string str;
  bool operator<(const node &n) const {
    for (int i = 0; i < N2; ++i) {
      if (n.f[i] == f[i])
        continue;
      return f[i] < n.f[i];
    }
    return true;
  }
};

bool Is_Return(const node &n) {
  for (int i = 0; i < N2; ++i) {
    if (n.f[i] != i + 1)
      return false;
  }
  return true;
}

string bfs(node &n) {
  queue<node> Q;
  map<node, bool> ans;
  Q.push(n);
  ans[n] = true;
  while (!Q.empty()) {
    node u = Q.front();
    Q.pop();
    if (Is_Return(u))
      return u.str;
    int tx = u.space / N;
    int ty = u.space % N;
    for (int i = 0; i < 4; ++i) {
      int sx = tx + x[i];
      int sy = ty + y[i];
      if (sx < 0 || sy < 0 || sx >= N || sy >= N)
        continue;
      node v = u;
      swap(v.f[v.space], v.f[sx * N + sy]);
      v.space = sx * N + sy;
      if (!ans[v]) {
        ans[v] = true;
        v.str += ch[i];
        Q.push(v);
      }
    }
  }
  return "unsolvable";
}

int main() {
  node n;
  for (int i = 0; i < N2; ++i) {
    cin >> n.f[i];
    if (n.f[i] == 0) {
      n.space = i;
      n.f[i] = N2;
    }
  }
  string s = bfs(n);
  cout << s.length() << endl;
  return 0;
}
Problem p02245 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int ans[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

bool match(int f[3][3]) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (f[i][j] != ans[i][j])
        return false;
    }
  }
  return true;
}

int dfs(int f[3][3], int x, int y, int v, int d, int l) {
  if (match(f))
    return d;
  if (d > l)
    return -1;
  for (int i = 0, res; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx > 2 || ny < 0 || ny > 2 || (i + 2) % 4 == v)
      continue;
    f[y][x] = f[ny][nx];
    f[ny][nx] = 0;
    if ((res = dfs(f, nx, ny, i, d + 1, l)) > 0)
      return res;
    f[ny][nx] = f[y][x];
    f[y][x] = 0;
  }
  return -1;
}

int main() {
  int f[3][3], x, y;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> f[i][j];
      if (f[i][j] == 0) {
        y = i;
        x = j;
      }
    }
  }

  int res;
  for (int i = 1; i < 50; ++i) {
    if ((res = dfs(f, x, y, -1, 0, i)) >= 0) {
      break;
    }
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int ans[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

bool match(int f[3][3]) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (f[i][j] != ans[i][j])
        return false;
    }
  }
  return true;
}

int dfs(int f[3][3], int x, int y, int d, int l) {
  if (match(f))
    return d;
  if (d > l)
    return -1;
  for (int i = 0, res; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx > 2 || ny < 0 || ny > 2)
      continue;
    f[y][x] = f[ny][nx];
    f[ny][nx] = 0;
    if ((res = dfs(f, nx, ny, d + 1, l)) > 0)
      return res;
    f[ny][nx] = f[y][x];
    f[y][x] = 0;
  }
  return -1;
}

int main() {
  int f[3][3], x, y;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> f[i][j];
      if (f[i][j] == 0) {
        y = i;
        x = j;
      }
    }
  }

  int res;
  for (int i = 1; i < 100; ++i) {
    if ((res = dfs(f, x, y, 0, i)) >= 0) {
      break;
    }
  }
  cout << res << endl;

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

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int d_x[4] = {-1, 0, 1, 0};
int d_y[4] = {0, 1, 0, -1};

int t[3][3];
int level = 0;

int countpath() {
  int ret = 0;
  int tmp;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (t[i][j] != 0) {
        tmp = t[i][j] - 1;
        ret += abs(tmp / 3 - i) + abs(tmp % 3 - j);
      }
    }
  }
  return ret;
}

bool check(int depth, int pre, int px, int py) {
  int w = countpath();
  if (w + depth > level)
    return false;
  if (w == 0)
    return true;
  int dx, dy;
  for (int i = 0; i < 4; i++) {
    if (abs(i - pre) != 2) {
      dx = px + d_x[i];
      dy = py + d_y[i];
      if (dx < 0 || dx == 3 || dy < 0 || dy == 3)
        continue;
      swap(t[dx][dy], t[px][py]);
      if (check(depth + 1, i, dx, dy))
        return true;
      swap(t[dx][dy], t[px][py]);
    }
  }
  return false;
}

void solution(int px, int py) {
  for (level = 0;; level++) {
    if (check(0, 9, px, py)) {
      cout << level << endl;
      return;
    }
  }
}

int main() {
  int px, py;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> t[i][j];
      if (t[i][j] == 0) {
        px = i;
        py = j;
      }
    }
  }
  solution(px, py);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int d_x[4] = {-1, 0, 1, 0};
int d_y[4] = {0, 1, 0, -1};

int t[3][3];
int px, py, level = 0;

int countpath() {
  int ret = 0;
  int tmp;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (t[i][j] != 0) {
        tmp = t[i][j] - 1;
        ret += abs(tmp / 3 - i) + abs(tmp % 3 - j);
      }
    }
  }
  return ret;
}

bool check(int depth, int pre) {
  int w = countpath();
  if (w + depth > level)
    return false;
  if (w == 0)
    return true;
  int dx, dy;
  for (int i = 0; i < 4; i++) {
    if (abs(i - pre) != 2) {
      dx = px + d_x[i];
      dy = py + d_y[i];
      if (dx < 0 || dx == 3 || dy < 0 || dy == 3)
        continue;
      swap(t[dx][dy], t[px][py]);
      if (check(depth + 1, i))
        return true;
      swap(t[dx][dy], t[px][py]);
    }
  }
  return false;
}

void solution() {
  for (level = 0;; level++) {
    if (check(0, 9)) {
      cout << level << endl;
      return;
    }
  }
}

int main() {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> t[i][j];
      if (t[i][j] == 0) {
        px = i;
        py = j;
      }
    }
  }
  solution();
  return 0;
}
Problem p02246 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

int pos[][2] = {3, 3, 0, 0, 0, 1, 0, 2, 0, 3, 1, 0, 1, 1, 1, 2,
                1, 3, 2, 0, 2, 1, 2, 2, 2, 3, 3, 0, 3, 1, 3, 2};

int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
char dir[] = {'R', 'D', 'U', 'L'};
int en[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
char path[100];
int puz[16];

int hstar() {
  int h = 0;
  for (int i = 0; i < 16; ++i) {
    int x = puz[i];
    if (x == 0)
      continue;
    h += abs(i / 4 - pos[x][0]) + abs(i % 4 - pos[x][1]);
  }
  return h;
}

bool dfs(int p, int pre, int d, int maxd) {
  if (d + hstar() > maxd)
    return false;

  if (d == maxd)
    return memcmp(puz, en, sizeof(en)) == 0;

  int x = p / 4, y = p % 4;
  for (int j = 0; j < 4; ++j) {
    if (pre + j == 3)
      continue;
    int nx = x + dx[j], ny = y + dy[j];
    int nz = 4 * nx + ny;
    if (nx >= 0 && nx < 4 && ny >= 0 && ny < 4) {
      swap(puz[p], puz[nz]);
      path[d] = dir[j];
      if (dfs(nz, j, d + 1, maxd))
        return true;
      swap(puz[p], puz[nz]);
    }
  }
  return false;
}

bool solvable() {
  int cnt = 0;
  for (int i = 0; i < 16; ++i) {
    if (puz[i] == 0)
      cnt += 3 - i / 4;
    else {
      for (int j = 0; j < i; ++j)
        if (puz[j] && puz[j] > puz[i])
          cnt++;
    }
  }
  return !(cnt & 1);
}

int main(void) {
  int T, p;
  for (int i = 0; i < 16; ++i) {
    scanf("%d", &puz[i]);
    if (puz[i] == 0)
      p = i;
  }
  if (solvable()) {
    int maxd = 0;
    for (; !dfs(p, -1, 0, maxd); ++maxd)
      ;
    path[maxd] = 0;
    printf("%d\\n", maxd);
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int dis[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int sx, sy;
string s;
struct node {
  int x;
  int y;
  int step;
  string s;
};
void bfs() {
  queue<node> Q;
  map<string, int> M;
  node p, q;
  p.s = s;
  p.x = sx;
  p.y = sy;
  p.step = 0;
  Q.push(p);
  while (!Q.empty()) {
    p = Q.front();
    // cout<<p.s<<"  "<<p.step<<" "<<p.x<<" "<<p.y<<endl;
    Q.pop();
    if (p.s == "BCDEFGHIJKLMNOPA") {
      printf("%d\\n", p.step);
      return;
    }
    for (int i = 0; i < 4; i++) {
      int tx = p.x + dis[i][0];
      int ty = p.y + dis[i][1];
      if (tx < 0 || ty < 0 || tx > 3 || ty > 3)
        continue;
      int term1 = tx * 4 + ty;
      int term2 = p.x * 4 + p.y;

      string str = p.s;
      char A = str[term1];
      str[term1] = 'A';
      str[term2] = A;
      if (!M[str]) {
        M[str] = 1;
        q.s = str;
        q.x = tx;
        q.y = ty;
        q.step = p.step + 1;
        Q.push(q);
      }
    }
  }
}
int main() {

  int k;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++) {
      scanf("%d", &k);
      if (k == 0) {
        sx = i;
        sy = j;
      }
      s += (char)(k + 'A');
    }
  bfs();
  return 0;
}
Problem p02247 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  string s, p;

  cin >> s >> p;

  n = s.length();
  m = p.length();
  for (int i = 0; i < n - m + 1; i++) {
    if (s.substr(i, m) == p)
      cout << i << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  string s, p;

  cin >> s >> p;

  for (int i = 0; i < s.length() - p.length() + 1; i++) {
    if (s.substr(i, p.length()) == p)
      cout << i << endl;
  }
  return 0;
}
Problem p02247 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int main() {

  string a, b;
  int tmp;
  cin >> a >> b;
  if (a.size() < b.size())
    return 0;
  for (int i = 0; i < a.size() - b.size() + 1; i++) {
    if (a[i] == b[0]) {
      tmp = 1;
      for (int j = 0; j < b.size(); j++)
        if (a[i + j] != b[j]) {
          tmp = 0;
          break;
        }
      if (tmp)
        cout << i << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {

  string a, b;
  int tmp;
  cin >> a >> b;
  for (int i = 0; i < a.size() - b.size() + 1; i++) {
    if (a[i] == b[0]) {
      tmp = 1;
      for (int j = 0; j < b.size(); j++)
        if (a[i + j] != b[j]) {
          tmp = 0;
          break;
        }
      if (tmp)
        cout << i << endl;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {

  string t, p;
  cin >> t >> p;
  if (t.size() < p.size())
    return 0;
  for (int i = 0; i <= t.size() - p.size(); i++) {
    bool f = true;
    for (int j = 0; j < p.size(); j++) {
      if (p[j] != t[i + j])
        f = false;
    }
    if (f)
      cout << i << endl;
  }

  return 0;
}

// EOF

Error: 0
0 0
1 1
0 1
1 2
0 2
1 3
0 3
1 4
0 4
1 5


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {

  string t, p;
  cin >> t >> p;
  for (int i = 0; i <= t.size() - p.size(); i++) {
    bool f = true;
    for (int j = 0; j < p.size(); j++) {
      cerr << j << " " << i + j << endl;
      if (p[j] != t[i + j])
        f = false;
    }
    if (f)
      cout << i << endl;
  }

  return 0;
}

// EOF