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

// 連鎖は、色を変えた場所付近から起こる＆0連鎖になるものをわざわざ消す必要はない。
// ということで、シミュレーションの前に、（色を変えるべき場所,色）の候補を絞る。
#include <iostream>
using namespace std;

int n;
int input[10000];

int erase(int pos, int num) {
  static int data[10000];
  int i, j;
  for (i = 0; i < n; i++) {
    data[i] = input[i];
  }
  data[pos] = num;

  bool eflag = true;
  while (eflag) {
    eflag = false;
    for (i = 0; i < n - 3; i++) {
      if (data[i] == 0)
        continue;

      int erased = 0;
      for (j = 1; i + j < n; j++) {
        if (data[i + j] == 0) {
          erased++;
          continue;
        }

        if (data[i] != data[i + j])
          break;
      }
      if (j - erased >= 4) {
        eflag = true;
        // 消す
        int st = i, ed = i + j;
        for (j = st; j < ed; j++) {
          data[j] = 0;
        }
        // ここでbreakをするとTLE確実。
      }
    }
  }
  int ret = n;
  for (i = 0; i < n; i++) {
    if (data[i] == 0)
      ret--;
  }
  return ret;
}

int main() {
  int i, j;

  while (cin >> n) {
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      cin >> input[i];
    }

    int ans = n;
    for (i = 0; i < n; i++) {
      for (j = 1; j <= 3; j++) {
        int l, r;
        for (l = i - 1; l >= 0; l--) {
          if (input[l] != j)
            break;
        }
        for (r = i + 1; r < n; r++) {
          if (input[r] != j)
            break;
          if (r - l - 1 >= 4) {
            ans = min(ans, erase(i, j));
            break;
          }
        }
        // breakしなかった場合の処理も書こう(戒め
        if (r - l - 1 >= 4) {
          ans = min(ans, erase(i, j));
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 実際に配列にコピーして云々する方法
#include <iostream>
using namespace std;

int n;
int input[10000];

int erase(int pos, int num) {
  static int data[10000];
  int i, j;
  for (i = 0; i < n; i++) {
    data[i] = input[i];
  }
  data[pos] = num;

  bool eflag = true;
  while (eflag) {
    eflag = false;
    for (i = 0; i < n - 3; i++) {
      if (data[i] == 0)
        continue;

      int erased = 0;
      for (j = 1; i + j < n; j++) {
        if (data[i + j] == 0) {
          erased++;
          continue;
        }

        if (data[i] != data[i + j])
          break;
      }
      if (j - erased >= 4) {
        eflag = true;
        // 消す
        int st = i, ed = i + j;
        for (j = st; j < ed; j++) {
          data[j] = 0;
        }
        // ここでbreakをするとTLE確実。
      }
    }
  }
  int ret = n;
  for (i = 0; i < n; i++) {
    if (data[i] == 0)
      ret--;
  }
  return ret;
}

int main() {
  int i, j, k;

  while (cin >> n) {
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      cin >> input[i];
    }

    int ans = 19191919;
    for (i = 0; i < n; i++) {
      for (j = 1; j <= 3; j++) {
        ans = min(ans, erase(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> a;

  while (scanf("%d", &n) && n) {
    a.resize(n);
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);

    int ans = n;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        if (a[i] == j)
          continue;
        if ((i && a[i - 1] == j) || (i == n - 1 && a[i + 1] == j)) {
          vector<int> v = a;
          v[i] = j;

          for (;;) {
            int prv = 0, color = a[0], num = 1;
            for (int i = 1; i < (int)v.size(); i++) {
              if (v[i] == color)
                num++;
              else {
                if (4 <= num)
                  break;
                color = v[i];
                num = 1;
                prv = i;
              }
            }

            if (4 <= num)
              for (int i = 0; i < num; i++)
                v.erase(v.begin() + prv);
            else
              break;
          }
          ans = min(ans, (int)v.size());
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> a;

  while (scanf("%d", &n) && n) {
    a.resize(n);
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);

    int ans = n;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        if (a[i] == j)
          continue;

        vector<int> v = a;
        v[i] = j;

        for (;;) {
          int prv = 0, color = a[0], num = 1;
          for (int i = 1; i < (int)v.size(); i++) {
            if (v[i] == color)
              num++;
            else {
              if (4 <= num)
                break;
              color = v[i];
              num = 1;
              prv = i;
            }
          }

          if (4 <= num)
            for (int i = 0; i < num; i++)
              v.erase(v.begin() + prv);
          else
            break;
        }
        ans = min(ans, (int)v.size());
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define PB push_back
#define MP make_pair

typedef pair<int, int> PII;

int n;
int c[10000];

int solve(int l) {
  int r = l + 1, res = n;

  while (l >= 0 && r < n) {
    int color = c[l];
    int count = 0;
    for (; l >= 0 && c[l] == color; l--, count++)
      ;
    for (; r < n && c[r] == color; r++, count++)
      ;

    if (count < 4)
      break;
    res -= count;
  }
  return res;
}

int main() {
  while (true) {
    int ans;
    cin >> n;
    if (n == 0)
      break;

    REP(i, n) scanf("%d", &c[i]);

    ans = n;
    REP(i, n) {
      int tmp = c[i];
      REP(j, 3) {
        c[i] = j + 1;
        ans = min(ans, solve(i));
      }
      c[i] = tmp;
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#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 PB push_back
#define MP make_pair

typedef pair<int, int> PII;

int solve(vector<PII> v) {
  bool f = true;
  int res = 0;

  while (f) {
    f = false;
    FOR(i, 1, v.size()) {
      if (v[i - 1].first == v[i].first) {
        v[i - 1].second += v[i].second;
        v.erase(v.begin() + i);
        i--;
      }
    }
    REP(i, v.size()) if (v[i].second >= 4) {
      f = true;
      v.erase(v.begin() + i);
      i--;
    }

    REP(i, v.size()) cout << v[i].first << " " << v[i].second << endl;
  }
  REP(i, v.size()) res += v[i].second;
  return res;
}

int main() {
  while (true) {
    int n, a, b, ans;
    vector<PII> p;

    cin >> n;
    if (n == 0)
      break;
    a = -1;
    b = 0;
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (a == tmp)
        b++;
      else {
        p.PB(MP(a, b));
        a = tmp;
        b = 1;
      }
    }
    p.PB(MP(a, b));
    p.erase(p.begin());

    ans = n;
    REP(i, p.size()) {
      if (p[i].second == 1) {
        int tmp = p[i].first;
        if (i > 0 && p[i - 1].second >= 3) {
          p[i].first = p[i - 1].first;
          ans = min(ans, solve(p));
        }
        if (i < p.size() && p[i + 1].second >= 3) {
          p[i].first = p[i + 1].first;
          ans = min(ans, solve(p));
        }
        p[i].first = tmp;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int ma;
int m, n;
int ve[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void solve(int ni, int nj, int nstep, int isice[100][100]) {
  isice[ni][nj] = 0;
  bool is = false;
  for (int i = 0; i < 4; i++) {
    int newi = ni + ve[i][0], newj = nj + ve[i][1];
    if (0 <= newi && newi < n && 0 <= newj && newj < m &&
        isice[newi][newj] != 0) {
      solve(ni + ve[i][0], nj + ve[i][1], nstep + 1, isice);
      is = true;
    }
  }
  isice[ni][nj] = 1;
  if (!is)
    ma = max(nstep, ma);
}
int main() {
  for (;;) {
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    int isice[100][100];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%d", &isice[i][j]);
    ma = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (isice[i][j] != 0)
          solve(i, j, 1, isice);
      }
    }
    printf("%d\\n", ma);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int ma;
int m, n;
int ve[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void solve(int ni, int nj, int nstep, vector<vector<int>> isice) {
  if (!(0 <= ni && ni < n && 0 <= nj && nj < m) || isice[ni][nj] == 0) {
    ma = max(nstep, ma);
    return;
  }
  isice[ni][nj] = 0;
  for (int i = 0; i < 4; i++) {
    solve(ni + ve[i][0], nj + ve[i][1], nstep + 1, isice);
  }
  isice[ni][nj] = 1;
}
int main() {
  for (;;) {
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    vector<vector<int>> isice(n, vector<int>(m));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%d", &isice[i][j]);
    ma = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        solve(i, j, 0, isice);
      }
    }
    printf("%d\\n", ma);
  }
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

#define MAX_W 90
#define MAX_H 90

using namespace std;

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

int width, height;
bool field[MAX_W][MAX_H];

int ans;

void solve(int sx, int sy, int sum) {
  if (sx >= width || sx < 0)
    return;
  if (sy >= height || sy < 0)
    return;
  bool now = field[sx][sy];
  if (now) {
    field[sx][sy] = false;
    ans = max(ans, sum + 1);
    for (int i = 0; i < 4; i++) {
      solve(sx + dx[i], sy + dy[i], sum + 1);
    }
    field[sx][sy] = true;
  }
  return;
}

int main() {

  while (true) {

    scanf("%d%d", &width, &height);
    if (width == 0 && height == 0)
      break;
    int fake;
    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        scanf("%d", &fake);
        field[i][j] = (fake == 1);
      }
    }

    ans = 0;
    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        solve(i, j, 0);
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

#define MAX_W 90
#define MAX_H 90

using namespace std;

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

int width, height;
bool field[MAX_W][MAX_H];

int ans;

void solve(int sx, int sy, int sum) {
  bool now = field[sx][sy];
  if (now) {
    field[sx][sy] = false;
    ans = max(ans, sum + 1);
    for (int i = 0; i < 4; i++) {
      solve(sx + dx[i], sy + dy[i], sum + 1);
    }
    field[sx][sy] = true;
  }
  return;
}

int main() {

  scanf("%d%d", &width, &height);
  int fake;
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      scanf("%d", &fake);
      field[i][j] = (fake == 1);
    }
  }

  ans = 0;
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      solve(i, j, 0);
    }
  }
  printf("%d\\n", ans);

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

#include <iostream>
using namespace std;
int m, n;
bool a[100][100] = {{0}};
bool b[100][100] = {{0}};
bool f[100][100] = {{0}};
int cnt = 0, ans = 0;
int p = 0, q = 0;
void check(int x, int y);
int main(void) {
  for (;;) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        b[i][j] = 0;
        a[i][j] = 0;
      }
    }
    ans = 0;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        b[i][j] = a[i][j];
      }
    }
    // in
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cnt = 0;
        if (b[i][j] == 1) {
          // cout<<".."<<i<<"."<<j<<endl;
          check(i, j);
        }
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j <= m; j++) {
            b[i][j] = a[i][j];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
void check(int x, int y) {
  b[x][y] = 0;
  // p++,cout<<p<<"+"<<x<<","<<y<<endl;
  cnt++;
  int flag = 0;
  if (b[x + 1][y] == 1) {
    flag = 1;
    check(x + 1, y);
    b[x + 1][y] = 1;
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }

  if (b[x][y + 1] == 1) {
    flag = 1;
    check(x, y + 1);
    b[x][y + 1] = 1;
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }
  if (b[x - 1][y] == 1) {
    flag = 1;
    check(x - 1, y);
    b[x - 1][y] = 1;
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }

  if (b[x][y - 1] == 1) {
    flag = 1;
    check(x, y - 1);
    b[x][y - 1] = 1;
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }

  if (flag == 0) {
    ans = max(ans, cnt);
    // p--,cout<<"-"<<x<<","<<y<<endl;

    return;
  }
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int m, n;
bool a[100][100];
int cnt = 0, ans = 0;
void check(int x, int y);
int main(void) {
  for (;;) {
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    // in
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cnt = 0;
        if (a[i][j] == 1)
          check(i, j);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
void check(int x, int y) {
  a[x][y] = 0;
  cnt++;
  int flag = 0;
  if (a[x + 1][y] == 1)
    flag = 1, check(x + 1, y);

  if (a[x][y + 1] == 1)
    flag = 1, check(x, y + 1);

  if (a[x - 1][y] == 1)
    flag = 1, check(x - 1, y);

  if (a[x][y - 1] == 1)
    flag = 1, check(x, y - 1);

  if (flag == 0) {
    ans = max(ans, cnt);
    cnt--;
    return;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890

int map[100][100];
int x_max, y_max;
int ans;

int dp(int x, int y) {
  if (map[x][y] == 0)
    return 0;
  int answer = 1;
  map[x][y] = 0;
  if (map[x][y - 1] == 1)
    answer = max(answer, dp(x, y - 1) + 1);
  if (map[x][y + 1] == 1)
    answer = max(answer, dp(x, y + 1) + 1);
  if (map[x - 1][y] == 1)
    answer = max(answer, dp(x - 1, y) + 1);
  if (map[x + 1][y] == 1)
    answer = max(answer, dp(x + 1, y) + 1);
  map[x][y] = 1;
  return answer;
}

int main() {
  while (1) {
    memset(map, 0, sizeof(map));
    ans = 0;

    scanf("%d%d", &x_max, &y_max);
    if (x_max == 0 && y_max == 0)
      break;
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        scanf("%d", &map[a][b]);
      }
    }
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        ans = max(ans, dp(a, b));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890

int map[60][60];
int x_max, y_max;
int ans;

int dp(int x, int y) {
  if (map[x][y] == 0)
    return 0;
  int answer = 1;
  map[x][y] = 0;
  if (map[x][y - 1] == 1)
    answer = max(answer, dp(x, y - 1) + 1);
  if (map[x][y + 1] == 1)
    answer = max(answer, dp(x, y + 1) + 1);
  if (map[x - 1][y] == 1)
    answer = max(answer, dp(x - 1, y) + 1);
  if (map[x + 1][y] == 1)
    answer = max(answer, dp(x + 1, y) + 1);
  map[x][y] = 1;
  return answer;
}

int main() {
  while (1) {
    memset(map, 0, sizeof(map));
    ans = 0;

    scanf("%d%d", &x_max, &y_max);
    if (x_max == 0 && y_max == 0)
      break;
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        scanf("%d", &map[a][b]);
      }
    }
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        ans = max(ans, dp(a, b));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

int m, n;
bool a[90][90];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int ma;

void f(bool a[90][90], int depth, int y, int x) {
  if (y < 0 || y >= n)
    return;
  if (x < 0 || x >= m)
    return;
  if (!a[y][x])
    return;

  a[y][x] = 0;

  if (ma < depth)
    ma = depth;

  for (int i = 0; i < 4; ++i)
    f(a, depth + 1, y + dy[i], x + dx[i]);

  a[y][x] = 1;
}

int main(void) {
  while (cin >> m >> n, m || n) {
    memset(a, 0, sizeof(a));
    ma = 0;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        cin >> a[i][j];

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (a[i][j])
          f(a, 1, i, j);

    cout << ma << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

int m, n;

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

int ma;

void f(vvi a, int depth, int y, int x) {
  if (y < 0 || y >= n)
    return;
  if (x < 0 || x >= m)
    return;
  if (a[y][x] == 0)
    return;

  a[y][x] = 0;

  if (ma < depth)
    ma = depth;

  for (int i = 0; i < 4; ++i)
    f(a, depth + 1, y + dy[i], x + dx[i]);
}

int main() {
  while (cin >> m >> n, m || n) {
    vvi a(90, vi(90));

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        cin >> a[i][j];

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (a[i][j])
          f(a, 1, i, j);

    cout << ma << endl;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int h, w;

// int saiki(int x,int y,int n,vector< vector<int> > board){
int saiki(int x, int y, int n, int board[][100]) {
  int ret = n;
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
      continue;
    }
    if (board[ny][nx] == 1) {
      board[ny][nx] = 0;
      ret = max(ret, saiki(nx, ny, n + 1, board));
      board[ny][nx] = 1;
    }
  }
  return ret;
}

int main() {
  while (true) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    // vector< vector<int> > board(h,vector<int>(w,0));
    int board[100][100];
    REP(i, h) REP(j, w) { scanf("%d", &board[i][j]); }
    int ans = 0;
    REP(i, h) REP(j, w) {
      if (board[i][j] == 1) {
        board[i][j] = 0;
        ans = max(ans, saiki(j, i, 1, board));
        board[i][j] = 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int h, w;

int saiki(int x, int y, int n, vector<vector<int>> board) {
  int ret = n;
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
      continue;
    }
    if (board[ny][nx] == 1) {
      board[ny][nx] = 0;
      ret = max(ret, saiki(nx, ny, n + 1, board));
      board[ny][nx] = 1;
    }
  }
  return ret;
}

int main() {
  while (true) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    vector<vector<int>> board(h, vector<int>(w, 0));
    REP(i, h) REP(j, w) { scanf("%d", &board[i][j]); }
    int ans = 0;
    REP(i, h) REP(j, w) {
      if (board[i][j] == 1) {
        int count = 0;
        REP(i, 4) {
          int nx = j + dx[i];
          int ny = i + dy[i];
          if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
            continue;
          }
          if (board[ny][ny] == 1)
            count++;
        }
        if (count <= 1) {
          board[i][j] = 0;
          ans = max(ans, saiki(j, i, 1, board));
          board[i][j] = 1;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
void kouri(int, int, int);
int sum_max = 0;
int c[90][90];
int c2[90][90] = {0};
int a, b;
int nx[] = {1, 0, -1, 0};
int ny[] = {0, -1, 0, 1};
int main() {
  while (1) {
    sum_max = 0;
    cin >> a;
    cin >> b;
    if (a == 0 && b == 0)
      return 0;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        cin >> c[i][j];
      }
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (c[i][j] == 1) {
          kouri(i, j, 0);
        }
      }
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        c[i][j] = 0;
      }
    }
    cout << sum_max << endl;
  }
}
void kouri(int x, int y, int sum) {
  /*for (int i = 0; i < a; i++) {
          for (int j = 0; j < b; j++) {
                  cout << c[i][j]<<" ";
          }
          cout << endl;
  }
  cout << endl << "--------------------------------" << endl;*/
  c[x][y] = 0;
  sum++;
  if (c[x][y + 1] == 1)
    kouri(x, y + 1, sum);
  if (c[x][y - 1] == 1)
    kouri(x, y - 1, sum);
  if (c[x + 1][y] == 1)
    kouri(x + 1, y, sum);
  if (c[x - 1][y] == 1)
    kouri(x - 1, y, sum);
  c[x][y] = 1;
  if (sum > sum_max)
    sum_max = sum;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct p {
  int x;
  int y;
  int cnt;
  int visited[101][101];
};
int kouri[1001][1001], a, b, c, d, cnt_MAX = 0, ruio = 0;
int main() {
  queue<p> info;
  p u, v;
  int mx[] = {1, 0, -1, 0};
  int my[] = {0, -1, 0, 1};
  while (1) {
    cin >> a >> b;
    if (a == 0 && b == 0)
      return 0;
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        cin >> kouri[i][j];
      }
    }
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        if (kouri[i][j] == 1) {
          u.x = i;
          u.y = j;
          u.cnt = 0;
          info.push(u);
          ruio++;
        }
      }
    }
    while (!info.empty()) {
      for (int k = 0; k < 4; k++) {
        u = info.front();
        v = info.front();

        int nx = v.x + mx[k];
        int ny = v.y + my[k];
        if (kouri[nx][ny] == 1 && v.visited[nx][ny] != -1) {
          u.cnt++;
          if (u.cnt > cnt_MAX) {
            cnt_MAX = u.cnt;
          }
          u.x = nx;
          u.y = ny;
          u.visited[nx][ny] = -1;
          info.push(u);
        }
        if (k == 3) {
          info.pop();
        }
      }
    }
    cout << cnt_MAX << endl;
    memset(kouri, 0, sizeof(kouri));
    cnt_MAX = 0;
  }
  return 0;
}