Problem p00842 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int N, M;
int a[55][55];
int b[11111][11111];

vector<int> sw[1111];

void print() {
  for (int i = 0; i < N + M; i++) {
    for (int j = 0; j < N + M; j++)
      cout << b[i][j] << " ";
    cout << endl;
  }
}

int main() {
  while (cin >> N, N) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (int i = 0; i < 55; i++)
      sw[i].clear();
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cin >> a[i][j];
        b[i][j] = a[i][j];
      }
    }
    M = 0;
    sw[0].push_back(0);
    for (int i = 0; i < a[0][1] - 2; i++) {
      sw[M].push_back(N + M + 1);
      sw[M + 1].push_back(N + M);
      M++;
    }
    sw[M].push_back(1);
    M++;
    for (int i = 0; i < M; i++) {
      b[0][N + i] = b[N + i][0] = i + 1;
      b[1][N + i] = b[N + i][1] = a[0][1] - i - 1;
      for (int j = 0; j < M; j++)
        b[N + i][N + j] = b[N + j][N + i] = abs(i - j);
    }
    for (int i = 2; i < N; i++) {
      bool judge = false;
      for (int j = 0; j < M; j++) {
        bool flag = false;
        for (int k = 0; k < i; k++) {
          if (b[N + j][k] + 1 != b[k][i])
            flag = true;
        }
        if (flag)
          continue;
        sw[j].push_back(i);
        for (int k = 0; k < M; k++)
          b[i][N + k] = b[N + k][i] = b[N + j][N + k] + 1;
        judge = true;
        break;
      }
      if (!judge) {
        for (int j = 0; j < M; j++) {
          bool check = false;
          for (int k = 0; k < i - 1; k++) {
            if (b[i][k] - b[N + j][k] != b[i][k + 1] - b[N + j][k + 1])
              check = true;
          }
          if (check)
            continue;
          int m = M;
          sw[j].push_back(N + M);
          sw[M].push_back(j);
          for (int k = 0; k < b[i][0] - b[N + j][0] - 2; k++) {
            sw[M].push_back(N + M + 1);
            sw[M + 1].push_back(N + M);
            M++;
          }
          sw[M].push_back(i);
          M++;
          for (int k = m; k < M; k++) {
            for (int l = 0; l < i; l++)
              b[l][N + k] = b[N + k][l] = b[l][N + j] + k - m + 1;
            for (int l = m; l < M; l++)
              b[N + k][N + l] = b[N + l][N + k] = abs(k - l);
          }
          for (int k = m; k < M; k++) {
            b[N + j][N + k] = b[N + k][N + j] = k - m + 1;
            b[i][N + k] = b[N + k][i] = M - k;
            for (int l = 0; l < m; l++)
              b[N + l][N + k] = b[N + k][N + l] =
                  b[N + j][N + l] + b[N + j][N + k];
          }
          for (int k = 0; k < m; k++)
            b[i][N + k] = b[N + k][i] =
                b[N + j][N + k] + b[N + j][N + M - 1] + 1;
          break;
        }
      }
    }
    vector<int> ans;
    for (int i = 0; i < M; i++)
      ans.push_back(sw[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < M; i++)
      cout << ans[i] << (i == M - 1 ? '\n' : ' ');
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int N, M;
int a[55][55];
int b[1111][1111];

vector<int> sw[1111];

void print() {
  for (int i = 0; i < N + M; i++) {
    for (int j = 0; j < N + M; j++)
      cout << b[i][j] << " ";
    cout << endl;
  }
}

int main() {
  while (cin >> N, N) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (int i = 0; i < 55; i++)
      sw[i].clear();
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cin >> a[i][j];
        b[i][j] = a[i][j];
      }
    }
    M = 0;
    sw[0].push_back(0);
    for (int i = 0; i < a[0][1] - 2; i++) {
      sw[M].push_back(N + M + 1);
      sw[M + 1].push_back(N + M);
      M++;
    }
    sw[M].push_back(1);
    M++;
    for (int i = 0; i < M; i++) {
      b[0][N + i] = b[N + i][0] = i + 1;
      b[1][N + i] = b[N + i][1] = a[0][1] - i - 1;
      for (int j = 0; j < M; j++)
        b[N + i][N + j] = b[N + j][N + i] = abs(i - j);
    }
    for (int i = 2; i < N; i++) {
      bool judge = false;
      for (int j = 0; j < M; j++) {
        bool flag = false;
        for (int k = 0; k < i; k++) {
          if (b[N + j][k] + 1 != b[k][i])
            flag = true;
        }
        if (flag)
          continue;
        sw[j].push_back(i);
        for (int k = 0; k < M; k++)
          b[i][N + k] = b[N + k][i] = b[N + j][N + k] + 1;
        judge = true;
        break;
      }
      if (!judge) {
        for (int j = 0; j < M; j++) {
          bool check = false;
          for (int k = 0; k < i - 1; k++) {
            if (b[i][k] - b[N + j][k] != b[i][k + 1] - b[N + j][k + 1])
              check = true;
          }
          if (check)
            continue;
          int m = M;
          sw[j].push_back(N + M);
          sw[M].push_back(j);
          for (int k = 0; k < b[i][0] - b[N + j][0] - 2; k++) {
            sw[M].push_back(N + M + 1);
            sw[M + 1].push_back(N + M);
            M++;
          }
          sw[M].push_back(i);
          M++;
          for (int k = m; k < M; k++) {
            for (int l = 0; l < i; l++)
              b[l][N + k] = b[N + k][l] = b[l][N + j] + k - m + 1;
            for (int l = m; l < M; l++)
              b[N + k][N + l] = b[N + l][N + k] = abs(k - l);
          }
          for (int k = m; k < M; k++) {
            b[N + j][N + k] = b[N + k][N + j] = k - m + 1;
            b[i][N + k] = b[N + k][i] = M - k;
            for (int l = 0; l < m; l++)
              b[N + l][N + k] = b[N + k][N + l] =
                  b[N + j][N + l] + b[N + j][N + k];
          }
          for (int k = 0; k < m; k++)
            b[i][N + k] = b[N + k][i] =
                b[N + j][N + k] + b[N + j][N + M - 1] + 1;
          break;
        }
      }
    }
    vector<int> ans;
    for (int i = 0; i < M; i++)
      ans.push_back(sw[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < M; i++)
      cout << ans[i] << (i == M - 1 ? '\n' : ' ');
  }
  return 0;
}