prithivMLmods/Coder-Stat huggingface dataset:
Problem p00118 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

const int MAX_H = 100, MAX_W = 100,
          v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::string map[MAX_H];
int W, H;

// x,y »ÝÌÊu c Á·¶
void dfs(int x, int y, char c) {
  map[y][x] = ' ';

  for (int i = 0; i < 4; i++) {
    int nx = x + v[i][0], ny = y + v[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c)
      dfs(nx, ny, c);
  }
  return;
}

int main() {
  // Input
  while (std::cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      std::cin >> map[i];
    }

    // Processing
    int res = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (map[i][j] != ' ')
          res++, dfs(j, i, map[i][j]);
      }
    }

    std::cout << res << std::endl;
  }
}

Fixed submission:
#include <iostream>

const int MAX_H = 100, MAX_W = 100,
          v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::string map[MAX_H];
int W, H;

// x,y »ÝÌÊu c Á·¶
void dfs(int x, int y, char c) {
  map[y][x] = ' ';

  for (int i = 0; i < 4; i++) {
    int nx = x + v[i][0], ny = y + v[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c)
      dfs(nx, ny, c);
  }
  return;
}

int main() {
  // Input
  while (std::cin >> H >> W, W || H) {
    for (int i = 0; i < H; i++) {
      std::cin >> map[i];
    }

    // Processing
    int res = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (map[i][j] != ' ')
          res++, dfs(j, i, map[i][j]);
      }
    }

    std::cout << res << std::endl;
  }
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int W, H;
char field[30][30];

void dfs(int x, int y, char obj) {
  field[x][y] = 'T';

  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (dx != 0 && dy != 0)
        continue;
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) {
        dfs(nx, ny, obj);
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> H, cin >> W;
    if (!W && !H)
      break;

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

    int count = 0;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') {
          dfs(i, j, field[i][j]);
          count++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int W, H;
char field[100][100];

void dfs(int x, int y, char obj) {
  field[x][y] = 'T';

  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (dx != 0 && dy != 0)
        continue;
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) {
        dfs(nx, ny, obj);
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> H, cin >> W;
    if (!W && !H)
      break;

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

    int count = 0;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') {
          dfs(i, j, field[i][j]);
          count++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int N = 22;
int w, h, ans;
char ch, maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y) {
  maze[x][y] = '-';
  for (int i = 0; i < 4; ++i) {
    int nx = x + cx[i], ny = y + cy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch)
      dfs(nx, ny);
  }
}
int main() {
  while (cin >> h >> w, (h && w)) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> maze[i][j];
    ans = 0;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j) {
        if (maze[i][j] != '-') {
          ch = maze[i][j];
          ++ans;
          dfs(i, j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int N = 111;
int w, h, ans;
char ch, maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y) {
  maze[x][y] = '-';
  for (int i = 0; i < 4; ++i) {
    int nx = x + cx[i], ny = y + cy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch)
      dfs(nx, ny);
  }
}
int main() {
  while (cin >> h >> w, (h && w)) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> maze[i][j];
    ans = 0;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j) {
        if (maze[i][j] != '-') {
          ch = maze[i][j];
          ++ans;
          dfs(i, j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
#define INF 2147483647

int w, h;
char a[22][22];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int ans = 0;

void dfs(int x, int y, char s) {
  if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s)
    return;
  char t = a[x][y];
  a[x][y] = 'o';
  for (int i = 0; i < 4; i++) {
    dfs(x + dir[i][0], y + dir[i][1], t);
  }
}

int main() {
  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;
    ans = 0;
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != 'o') {
          ans++;
          dfs(i, j, a[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
#define INF 2147483647

int w, h;
char a[102][102];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int ans = 0;

void dfs(int x, int y, char s) {
  if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s)
    return;
  char t = a[x][y];
  a[x][y] = 'o';
  for (int i = 0; i < 4; i++) {
    dfs(x + dir[i][0], y + dir[i][1], t);
  }
}

int main() {
  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;
    ans = 0;
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != 'o') {
          ans++;
          dfs(i, j, a[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

using namespace std;

const int MAX_H = 101;
const int MAX_W = 101;
int H, W;
char blocks[MAX_H][MAX_W];

int init() {
  scanf("%d%d", &H, &W);

  if (H == 0 && W == 0)
    return -1;

  for (int i = 0; i < H; i++) {
    scanf("%s", blocks[i]);
  }

  return 0;
}

void fill(int row, int col) {
  char type = blocks[row][col];
  blocks[row][col] = ' ';

  int di[] = {1, -1, 0, 0};
  int dj[] = {0, 0, 1, -1};

  for (int k = 0; k < 4; k++) {
    int ti = row + di[k];
    int tj = col + dj[k];

    if (0 <= ti && ti < H && 0 <= tj && tj < W) {
      if (blocks[ti][tj] == type) {
        fill(row, col);
      }
    }
  }
}

void solve() {
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (blocks[i][j] != ' ') {
        fill(i, j);
        ans++;
      }
    }
  }
  printf("%d\n", ans);
}

int main() {
  while (!init()) {
    solve();
  }
}

Fixed submission:
#include <cstdio>

using namespace std;

const int MAX_H = 101;
const int MAX_W = 101;
int H, W;
char blocks[MAX_H][MAX_W];

int init() {
  scanf("%d%d", &H, &W);

  if (H == 0 && W == 0)
    return -1;

  for (int i = 0; i < H; i++) {
    scanf("%s", blocks[i]);
  }

  return 0;
}

void fill(int row, int col) {
  char type = blocks[row][col];
  blocks[row][col] = ' ';

  int di[] = {1, -1, 0, 0};
  int dj[] = {0, 0, 1, -1};

  for (int k = 0; k < 4; k++) {
    int ti = row + di[k];
    int tj = col + dj[k];

    if (0 <= ti && ti < H && 0 <= tj && tj < W) {
      if (blocks[ti][tj] == type) {
        fill(ti, tj);
      }
    }
  }
}

void solve() {
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (blocks[i][j] != ' ') {
        fill(i, j);
        ans++;
      }
    }
  }
  printf("%d\n", ans);
}

int main() {
  while (!init()) {
    solve();
  }
}
Problem p00118 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int W, H;

string field[100];

bool visit[100][100];

void dfs(int y, int x) {
  visit[y][x] = true;
  static int dy[4] = {0, 1, 0, -1};
  static int dx[4] = {1, 0, -1, 0};
  char c = field[y][x];

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false &&
        c == field[ny][nx]) {
      dfs(ny, nx);
    }
  }
}

void pv() {
  cerr << endl;
  cerr << "-----" << endl;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << visit[y][x];
    }
    cerr << endl;
  }

  cerr << "-----" << endl;
}

int main() {
  for (; cin >> W >> H, W || H;) {
    for (int y = 0; y < H; y++)
      cin >> field[y];

    memset(visit, 0, sizeof visit);

    int ans = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (visit[y][x] == false) {
          ans++;
          dfs(y, x);
        }
      }
    }

    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int W, H;

string field[100];

bool visit[100][100];

void dfs(int y, int x) {
  visit[y][x] = true;
  static int dy[4] = {0, 1, 0, -1};
  static int dx[4] = {1, 0, -1, 0};
  char c = field[y][x];

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false &&
        c == field[ny][nx]) {
      dfs(ny, nx);
    }
  }
}

void pv() {
  cerr << endl;
  cerr << "-----" << endl;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << visit[y][x];
    }
    cerr << endl;
  }

  cerr << "-----" << endl;
}

int main() {
  for (; cin >> H >> W, W || H;) {
    for (int y = 0; y < H; y++)
      cin >> field[y];

    memset(visit, 0, sizeof visit);

    int ans = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (visit[y][x] == false) {
          ans++;
          dfs(y, x);
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
/*
 * Filename:    aoj0118.cpp
 * Desciption:  DFS
 * Created:     2016-03-02
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define INT_MAX 2 << 30
using namespace std;
// typedef long long ll;
int n, m, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
char maze[30][30];
void dfs(int x, int y, char s) {

  int tx, ty;
  maze[x][y] = 0;
  for (int i = 0; i < 4; i += 1) {
    tx = x + dx[i];
    ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= n || ty >= m)
      continue;
    if (maze[tx][ty] == s)
      dfs(tx, ty, s);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n && m) {
    for (int i = 0; i < n; i += 1) {
      scanf("%s", maze[i]);
    }
    ans = 0;
    for (int i = 0; i < n; i += 1) {
      for (int j = 0; j < m; j += 1) {
        if (maze[i][j] != 0) {
          dfs(i, j, maze[i][j]);
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
/*
 * Filename:    aoj0118.cpp
 * Desciption:  DFS
 * Created:     2016-03-02
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define INT_MAX 2 << 30
using namespace std;
// typedef long long ll;
int n, m, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
char maze[105][105];
void dfs(int x, int y, char s) {

  int tx, ty;
  maze[x][y] = 0;
  for (int i = 0; i < 4; i += 1) {
    tx = x + dx[i];
    ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= n || ty >= m)
      continue;
    if (maze[tx][ty] == s)
      dfs(tx, ty, s);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n && m) {
    for (int i = 0; i < n; i += 1) {
      scanf("%s", maze[i]);
    }
    ans = 0;
    for (int i = 0; i < n; i += 1) {
      for (int j = 0; j < m; j += 1) {
        if (maze[i][j] != 0) {
          dfs(i, j, maze[i][j]);
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;

vector<string> map;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {1, 0, -1, 0};

void rec(int x, int y, char c) {
  map[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) {
      if (map[y + dy[i]][x + dx[i]] == c) {
        rec(x + dx[i], y + dy[i], c);
      }
    }
  }
}

int solve() {
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (map[i][j] != '.') {
        rec(j, i, map[i][j]);
        ret++;
      }
    }
  }

  return ret;
}

int main() {
  while (true) {
    cin >> H >> W;

    map = vector<string>(H);

    for (int i = 0; i < H; i++) {
      cin >> map[i];
    }

    cout << solve() << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;

vector<string> map;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {1, 0, -1, 0};

void rec(int x, int y, char c) {
  map[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) {
      if (map[y + dy[i]][x + dx[i]] == c) {
        rec(x + dx[i], y + dy[i], c);
      }
    }
  }
}

int solve() {
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (map[i][j] != '.') {
        rec(j, i, map[i][j]);
        ret++;
      }
    }
  }

  return ret;
}

int main() {
  while (true) {
    cin >> H >> W;

    if (H == 0 && W == 0) {
      break;
    }

    map = vector<string>(H);

    for (int i = 0; i < H; i++) {
      cin >> map[i];
    }

    cout << solve() << endl;
  }

  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

char table[100][100];
int w, h;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void Dfs(int x, int y, char cur) {
  table[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(next_x, next_y, cur);
    }
  }
}

int main() {

  while (cin >> h >> w, w > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        cin >> table[x][y];
      }
    }

    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        if (table[x][y] != 'x') {
          Dfs(x, y, table[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

char table[100][100];
int w, h;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void Dfs(int x, int y, char cur) {
  table[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(next_x, next_y, cur);
    }
  }
}

int main() {

  while (cin >> h >> w, w > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        cin >> table[x][y];
      }
    }

    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        if (table[x][y] != 'X') {
          Dfs(x, y, table[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

char map[30][30];
pair<int, int> s;
int w, h;
int cnt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
class Node {
public:
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
};
int sx, sy;
void show() {
  for (int i = 0; i < h; i++) {
    cout << map[i] << endl;
  }
  cout << endl;
}
void bfs(int x, int y, char c) {
  //  cout<<x<<","<<y<<endl;
  queue<Node> q;
  q.push(Node(x, y));
  map[y][x] = '.';
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    //    show();
    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' ||
          map[ny][nx] != c)
        continue;
      map[ny][nx] = '.';
      q.push(Node(nx, ny));
    }
  }
}
void solve() {
  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] != '.') {
        cnt++;
        bfs(j, i, map[i][j]);
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  while (cin >> h >> w && (w || h)) {
    //    for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.';
    for (int i = 0; i < h; i++) {
      cin >> map[i];
    }
    solve();
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

char map[101][101];
pair<int, int> s;
int w, h;
int cnt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
class Node {
public:
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
};
int sx, sy;
void show() {
  for (int i = 0; i < h; i++) {
    cout << map[i] << endl;
  }
  cout << endl;
}
void bfs(int x, int y, char c) {
  //  cout<<x<<","<<y<<endl;
  queue<Node> q;
  q.push(Node(x, y));
  map[y][x] = '.';
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    //    show();
    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' ||
          map[ny][nx] != c)
        continue;
      map[ny][nx] = '.';
      q.push(Node(nx, ny));
    }
  }
}
void solve() {
  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] != '.') {
        cnt++;
        bfs(j, i, map[i][j]);
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  while (cin >> h >> w && (w || h)) {
    //    for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.';
    for (int i = 0; i < h; i++) {
      cin >> map[i];
    }
    solve();
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char c[100][100], sc;

int main() {
  while (cin >> H >> W) {
    if (!H)
      return 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    queue<P> q;
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (c[i][j] != '.') {
          q.push(P(i, j));
          sc = c[i][j];
          ans++;
        }
        while (!q.empty()) {
          int sy = q.front().first, sx = q.front().second;
          q.pop();
          c[sy][sx] = '.';
          // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl;
          for (int i = 0; i < 4; i++) {
            int ty = sy + dy[i], tx = sx + dx[i];
            if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc)
              continue;
            q.push(P(ty, tx));
          }
          /*for(int i=0;i<H;i++){
                  for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl;
          }*/
        }
      }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char c[100][100], sc;

int main() {
  while (cin >> H >> W) {
    if (!H)
      return 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    queue<P> q;
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (c[i][j] != '.') {
          q.push(P(i, j));
          sc = c[i][j];
          ans++;
        }
        while (!q.empty()) {
          int sy = q.front().first, sx = q.front().second;
          q.pop();
          c[sy][sx] = '.';
          // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl;
          for (int i = 0; i < 4; i++) {
            int ty = sy + dy[i], tx = sx + dx[i];
            if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc)
              continue;
            q.push(P(ty, tx));
            c[ty][tx] = '.';
          }
          /*for(int i=0;i<H;i++){
                  for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl;
          }*/
        }
      }
    cout << ans << endl;
  }
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 20;
int N, M;
char garden[MAX][MAX + 1];
int num;

void dfs(int x, int y, char ch) {
  garden[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (abs(dx) + abs(dy) > 1)
        continue;
      int nx = x + dx, ny = y + dy;
      if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch)
        dfs(nx, ny, ch);
    }
  }
  return;
}

int main() {
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(garden, '\0', sizeof(garden));
    for (int i = 0; i < N; i++)
      cin >> garden[i];
    num = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (garden[i][j] != '.') {
          dfs(i, j, garden[i][j]);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 100;
int N, M;
char garden[MAX][MAX + 1];
int num;

void dfs(int x, int y, char ch) {
  garden[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (abs(dx) + abs(dy) > 1)
        continue;
      int nx = x + dx, ny = y + dy;
      if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch)
        dfs(nx, ny, ch);
    }
  }
  return;
}

int main() {
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(garden, '\0', sizeof(garden));
    for (int i = 0; i < N; i++)
      cin >> garden[i];
    num = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (garden[i][j] != '.') {
          dfs(i, j, garden[i][j]);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  while (1) {
    int H, W;
    cin >> H >> W;
    UnionFind uf(H * W);
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j != W - 1 && field[i][j] == field[i][j + 1]) {
          uf.unionSet(i * W + j, i * W + j + 1);
        }
        if (i != H - 1 && field[i][j] == field[i + 1][j]) {
          uf.unionSet(i * W + j, (i + 1) * W + j);
        }
      }
    }
    set<int> aset;
    for (int i = 0; i < H * W; ++i) {
      aset.emplace(uf.root(i));
    }
    cout << aset.size() << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  while (1) {
    int H, W;
    cin >> H >> W;
    if (!H)
      break;
    UnionFind uf(H * W);
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j != W - 1 && field[i][j] == field[i][j + 1]) {
          uf.unionSet(i * W + j, i * W + j + 1);
        }
        if (i != H - 1 && field[i][j] == field[i + 1][j]) {
          uf.unionSet(i * W + j, (i + 1) * W + j);
        }
      }
    }
    set<int> aset;
    for (int i = 0; i < H * W; ++i) {
      aset.emplace(uf.root(i));
    }
    cout << aset.size() << endl;
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 100000000
#define _INF -100000000
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;

char field[100][101];
int h, w;
queue<P> que;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int ret;

void solve() {
  Loop(i, h) {
    Loop(j, w) {
      if (field[i][j] != '.') {
        que.push(P(i, j));
        char c = field[i][j];
        field[i][j] = '.';
        while (que.size() > 0) {
          P p = que.front();
          que.pop();
          Loop(k, 4) {
            int a = p.first + dx[k], b = p.second + dy[k];
            if (field[a][b] == c) {
              field[a][b] = '.';
              que.push(P(a, b));
            }
          }
        }
        ret++;
      }
    }
  }
}

int main() {
  while (cin >> h >> w, h || w) {
    Loop(i, h) {
      Loop(j, w) { cin >> field[i][j]; }
    }
    ret = 0;
    solve();
    cout << ret << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 100000000
#define _INF -100000000
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;

char field[100][101];
int h, w;
queue<P> que;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int ret;

void solve() {
  Loop(i, h) {
    Loop(j, w) {
      if (field[i][j] != '.') {
        que.push(P(i, j));
        char c = field[i][j];
        field[i][j] = '.';
        while (que.size() > 0) {
          P p = que.front();
          que.pop();
          Loop(k, 4) {
            int a = p.first + dx[k], b = p.second + dy[k];
            if (a >= 0 && a <= h - 1 && b >= 0 && b <= w - 1 &&
                field[a][b] == c) {
              field[a][b] = '.';
              que.push(P(a, b));
            }
          }
        }
        ret++;
      }
    }
  }
}

int main() {
  while (cin >> h >> w, h || w) {
    Loop(i, h) {
      Loop(j, w) { cin >> field[i][j]; }
    }
    ret = 0;
    solve();
    cout << ret << endl;
  }
  return 0;
}
Problem p00119 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

#define mp(x, y) make_pair(x, y)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;

int main(void) {
  int m;

  while (cin >> m) {
    vector<pii> v(m);

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

    int n;

    cin >> n;

    vector<pii> t(n);

    for (int i = 0; i < n; ++i) {
      cin >> t[i].X >> t[i].Y;
      --t[i].X;
      --t[i].Y;
    }

    bool hit = true;

    while (hit) {
      hit = false;

      for (int i = 0; i < m; ++i) {
        pii p = t[i];
        if (v[p.X].X < v[p.Y].X + 1) {
          v[p.X].X = v[p.Y].X + 1;
          hit = true;
        }
      }
    }

    sort(v.begin(), v.end(), greater<pii>());

    for (int i = 0; i < m; ++i) {
      cout << v.at(i).Y << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

#define mp(x, y) make_pair(x, y)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;

int main(void) {
  int m;

  while (cin >> m) {
    vector<pii> v(m);

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

    int n;

    cin >> n;

    vector<pii> t(n);

    for (int i = 0; i < n; ++i) {
      cin >> t[i].X >> t[i].Y;
      --t[i].X;
      --t[i].Y;
    }

    bool hit = true;

    while (hit) {
      hit = false;

      for (int i = 0; i < n; ++i) {
        pii p = t[i];
        if (v[p.X].X < v[p.Y].X + 1) {
          v[p.X].X = v[p.Y].X + 1;
          hit = true;
        }
      }
    }

    sort(v.begin(), v.end(), greater<pii>());

    for (int i = 0; i < m; ++i) {
      cout << v.at(i).Y << endl;
    }
  }
  return 0;
}
Problem p00120 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SUPrl 13
#define UNDEFCHAN 1145

int rl;
int rs[SUPrl];
int L;
double memf[1 << SUPrl][SUPrl];

void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; }

double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 +
                          // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2
  if (memf[s][ri] != UNDEFCHAN)
    return memf[s][ri];
  double re = 0;

  int a = 0;
  FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++;

  if (a == 1)
    re = 0;
  else {
    FOR(bri, 0, rl) {
      if ((s >> bri & 1) == 0)
        continue;
      if (bri == ri)
        continue;
      re =
          max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2));
    }
  }

  memf[s][ri] = re;
  return re;
}

void showf() {
  FOR(s, 0, 1 << rl)
      FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl;
}

double solve() {
  double re = 0;
  FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri));
  re = -re;
  FOR(ri, 0, rl) re += 2 * rs[ri];
  return re;
}

int main() {
  while (!cin.eof()) {
    cin >> L; // if(!L) return 0;
    rl = 0;
    while (cin.peek() != '\n') {
      cin >> rs[rl++];
    }
    resetf();
    double re = solve();
    // showf();
    if (re <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SUPrl 13
#define UNDEFCHAN 1145

int rl;
int rs[SUPrl];
int L;
double memf[1 << SUPrl][SUPrl];

void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; }

double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 +
                          // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2
  if (memf[s][ri] != UNDEFCHAN)
    return memf[s][ri];
  double re = 0;

  int a = 0;
  FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++;

  if (a == 1)
    re = 0;
  else {
    FOR(bri, 0, rl) {
      if ((s >> bri & 1) == 0)
        continue;
      if (bri == ri)
        continue;
      re =
          max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2));
    }
  }

  memf[s][ri] = re;
  return re;
}

void showf() {
  FOR(s, 0, 1 << rl)
      FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl;
}

double solve() {
  double re = 0;
  FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri));
  re = -re;
  FOR(ri, 0, rl) re += 2 * rs[ri];
  return re;
}

int main() {
  while (cin >> L) { // if(!L) return 0;
    rl = 0;
    while (cin.peek() != '\n') {
      cin >> rs[rl++];
    }
    resetf();
    double re = solve();
    // showf();
    if (re <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00121 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int b[19][19];

int main() {

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

  string s = "01234567";

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

  while (que.size()) {
    string q = que.front();
    que.pop();
    int x = q.find("0");
    for (int i = 0; i < 4; i++) {
      int next = x + dx[i];
      if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
          !(x == 4 && next == 3)) {
        string nextq = q;
        swap(nextq[x], nextq[next]);
        if (ans.find(nextq) == ans.end()) {
          que.push(nextq);
          ans[nextq] = ans[q] + 1;
        }
      }
    }
  }

  while (1) {
    string input = "";
    char temp;
    for (int i = 0; i < 8; i++) {
      if (cin >> temp)
        input.push_back(temp);
    }
    cout << ans[input] << endl;
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int b[19][19];

int main() {

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

  string s = "01234567";

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

  while (que.size()) {
    string q = que.front();
    que.pop();
    int x = q.find("0");
    for (int i = 0; i < 4; i++) {
      int next = x + dx[i];
      if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
          !(x == 4 && next == 3)) {
        string nextq = q;
        swap(nextq[x], nextq[next]);
        if (ans.find(nextq) == ans.end()) {
          que.push(nextq);
          ans[nextq] = ans[q] + 1;
        }
      }
    }
  }

  string input = "";
  while (getline(cin, input)) {
    input.erase(remove(input.begin(), input.end(), ' '), input.end());
    cout << ans[input] << endl;
  }
}
Problem p00121 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int ans[1 << 24];

int adj[] = {-6, -1, 1, 6};
int pos[] = {7, 8, 9, 10, 13, 14, 15, 16};

int encode(const vector<int> &v) {
  int r = 0;
  for (int i = 0; i < 8; ++i) {
    r |= v[pos[i]] << (3 * i);
  }
  return r;
}

void decode(int r, vector<int> &ret) {
  ret.assign(24, -1);
  for (int i = 0; i < 8; ++i) {
    ret[pos[i]] = r & 7;
    r >>= 3;
  }
}

int main() {
  for (int i = 0; i < (1 << 24); ++i) {
    ans[i] = -1;
  }

  vector<int> v(24, -1), w;
  for (int i = 0; i < 8; ++i) {
    v[pos[i]] = i;
  }
  int c = encode(v);
  ans[c] = 0;
  queue<int> q;
  q.push(c);
  q.push(-1);
  int tm = 1;
  while (q.size() > 1) {
    c = q.front();
    q.pop();
    if (c == -1) {
      ++tm;
      q.push(-1);
    } else {
      decode(c, v);
      int x;
      for (x = 7; v[x]; ++x)
        ;
      for (int i = 0; i < 4; ++i) {
        int y = x + adj[i];
        if (v[y] > 0) {
          swap(v[x], v[y]);
          c = encode(v);
          if (ans[c] == -1) {
            ans[c] = tm;
            q.push(c);
          }
          swap(v[x], v[y]);
        }
      }
    }
  }

  while (1) {
    for (int i = 0; i < 8; ++i) {
      if (scanf("%d", &v[pos[i]]) == EOF) {
        return 0;
      }
    }
    printf("%d\n", ans[encode(v)]);
  }
}

Fixed submission:
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

short ans[1 << 24];

int adj[] = {-6, -1, 1, 6};
int pos[] = {7, 8, 9, 10, 13, 14, 15, 16};

int encode(const vector<int> &v) {
  int r = 0;
  for (int i = 0; i < 8; ++i) {
    r |= v[pos[i]] << (3 * i);
  }
  return r;
}

void decode(int r, vector<int> &ret) {
  ret.assign(24, -1);
  for (int i = 0; i < 8; ++i) {
    ret[pos[i]] = r & 7;
    r >>= 3;
  }
}

int main() {
  for (int i = 0; i < (1 << 24); ++i) {
    ans[i] = -1;
  }

  vector<int> v(24, -1), w;
  for (int i = 0; i < 8; ++i) {
    v[pos[i]] = i;
  }
  int c = encode(v);
  ans[c] = 0;
  queue<int> q;
  q.push(c);
  q.push(-1);
  int tm = 1;
  while (q.size() > 1) {
    c = q.front();
    q.pop();
    if (c == -1) {
      ++tm;
      q.push(-1);
    } else {
      decode(c, v);
      int x;
      for (x = 7; v[x]; ++x)
        ;
      for (int i = 0; i < 4; ++i) {
        int y = x + adj[i];
        if (v[y] > 0) {
          swap(v[x], v[y]);
          c = encode(v);
          if (ans[c] == -1) {
            ans[c] = tm;
            q.push(c);
          }
          swap(v[x], v[y]);
        }
      }
    }
  }

  while (1) {
    for (int i = 0; i < 8; ++i) {
      if (scanf("%d", &v[pos[i]]) == EOF) {
        return 0;
      }
    }
    printf("%d\n", ans[encode(v)]);
  }
}
Problem p00121 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1e9
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int memo[16434825];
class Data {
public:
  vii status;
  pii zero;
  int cnt;
  Data() {}
  Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {}
};
int to_s(vector<vi> s) {
  int ret = 0;
  int r = 1;
  rep(i, 8) {
    ret += s[i / 4][i % 4] * r;
    r *= 8;
  }
  return ret;
}
void solve() {
  memset(memo, -1, sizeof(memo));
  vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}};
  queue<Data> Q;
  Q.push(Data(ans, make_pair(0, 0), 0));
  while (!Q.empty()) {
    auto d = Q.front();
    Q.pop();
    int tmp = to_s(d.status);
    if (memo[tmp] != -1)
      continue;
    memo[tmp] = d.cnt;
    auto z = d.zero;
    rep(i, 4) {
      int nx = dx[i] + z.first, ny = dy[i] + z.second;
      if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2)
        continue;
      auto p = d.status;
      swap(p[z.second][z.first], p[ny][nx]);
      Q.push(Data(p, make_pair(nx, ny), d.cnt + 1));
    }
  }
}
int main() {
  vii v = vii(2, vi(4, 0));
  solve();
  while (1) {
    cin >> v[0][0];
    if (cin.eof())
      break;
    reps(i, 1, 8) cin >> v[i / 4][i % 4];
    printf("%d\n", memo[to_s(v)]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1e9
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
char memo[16434825];
class Data {
public:
  vii status;
  pii zero;
  int cnt;
  Data() {}
  Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {}
};
int to_s(vector<vi> s) {
  int ret = 0;
  int r = 1;
  rep(i, 8) {
    ret += s[i / 4][i % 4] * r;
    r *= 8;
  }
  return ret;
}
void solve() {
  memset(memo, -1, sizeof(memo));
  vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}};
  queue<Data> Q;
  Q.push(Data(ans, make_pair(0, 0), 0));
  while (!Q.empty()) {
    auto d = Q.front();
    Q.pop();
    int tmp = to_s(d.status);
    if (memo[tmp] != -1)
      continue;
    memo[tmp] = d.cnt;
    auto z = d.zero;
    rep(i, 4) {
      int nx = dx[i] + z.first, ny = dy[i] + z.second;
      if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2)
        continue;
      auto p = d.status;
      swap(p[z.second][z.first], p[ny][nx]);
      Q.push(Data(p, make_pair(nx, ny), d.cnt + 1));
    }
  }
}
int main() {
  vii v = vii(2, vi(4, 0));
  solve();
  while (1) {
    cin >> v[0][0];
    if (cin.eof())
      break;
    reps(i, 1, 8) cin >> v[i / 4][i % 4];
    printf("%d\n", memo[to_s(v)]);
  }
  return 0;
}
Problem p00121 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int nx;
  int cnt = 0;
  int px[4] = {1, -1, 4, -4};
  string str = "        ";
  map<string, int> m;
  queue<int> q;
  queue<string> sq;

  q.push(0);
  sq.push("01234567");

  while (!q.empty()) {
    int siz = q.size();
    for (int nsiz = 0; nsiz < siz; nsiz++) {
      nx = q.front();
      q.pop();
      str = sq.front();
      sq.pop();

      if (m.count(str) == 0) {
        m[str] = cnt;
        for (int i = 0; i < 4; i++) {
          if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 &&
              nx / 4 == (nx + px[i]) / 4) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
          if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
        }
      }
    }
    cnt++;
  }

  while (1) {
    for (int i = 0; i < 8; i++) {
      cin >> str[i];
      if (str[i] == EOF)
        return (0);
    }
    cout << m[str] << endl;
  }

  return (0);
}

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

int main() {

  int nx;
  int cnt = 0;
  int px[4] = {1, -1, 4, -4};
  string str = "        ";
  map<string, int> m;
  queue<int> q;
  queue<string> sq;

  q.push(0);
  sq.push("01234567");

  while (!q.empty()) {
    int siz = q.size();
    for (int nsiz = 0; nsiz < siz; nsiz++) {
      nx = q.front();
      q.pop();
      str = sq.front();
      sq.pop();

      if (m.count(str) == 0) {
        m[str] = cnt;
        for (int i = 0; i < 4; i++) {
          if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 &&
              nx / 4 == (nx + px[i]) / 4) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
          if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
        }
      }
    }
    cnt++;
  }

  while (1) {
    for (int i = 0; i < 8; i++) {
      if (!(cin >> str[i]))
        return 0;
    }
    cout << m[str] << endl;
  }

  return (0);
}