Problem p00209 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

typedef pair<int, int> P;
typedef pair<bool, P> PP;
int field[200][200];
int pic[51][51];
int n, m;
const P NA = P(1000, 1000);

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

  cout << endl;
}
void rotate() {
  int cop[m][m];

  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      cop[i][j] = pic[i][j];

  // print();

  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      pic[m - 1 - j][i] = cop[i][j];

  // print();
}

PP much(int x, int y) {

  PP ret;
  ret.first = true;

  bool flg = true;
  for (int i = x, k = 0; i < x + m; i++, k++)
    for (int j = y, l = 0; j < y + m; j++, l++) {
      if (!(field[i][j] == pic[k][l] || pic[k][l] == -1))
        return PP(false, NA);
      if (flg && pic[k][l] != -1) {
        flg = false;
        ret.second = P(i + 1, j + 1);
      }
    }

  return ret;
}

P solve() {
  P ans = NA;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      PP pp = much(i, j);
      if (pp.first)
        ans = min(ans, pp.second);
    }

  return ans;
}

int main() {

  while (cin >> n >> m && n + m) {
    memset(field, -1, sizeof(field));

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

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

    P ans = solve();

    for (int i = 0; i < 3; i++) {
      rotate();
      solve();
      ans = min(ans, solve());
    }

    if (ans != NA)
      cout << ans.second << " " << ans.first << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<int, int> P;
typedef pair<bool, P> PP;
const int MAX = 15;
int field[MAX][MAX];
int pic[MAX][MAX];
int n, m;
const P NA = P(1000, 1000);

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

  cout << endl;
}
void rotate() {
  int cop[m][m];

  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      cop[i][j] = pic[i][j];

  /// print();

  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      pic[m - 1 - j][i] = cop[i][j];

  //  print();
}

PP much(int x, int y) {

  PP ret;
  ret.first = true;

  for (int i = x, k = 0; i < x + m; i++, k++)
    for (int j = y, l = 0; j < y + m; j++, l++)
      if (!(field[i][j] == pic[k][l] || pic[k][l] == -1))
        return PP(false, NA);

  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      if (pic[i][j] != -1)
        ret.second = P(i + 1, j + 1);

  return ret;
}

P solve() {
  P ans = NA;

  for (int i = 0; i < n - m; i++)
    for (int j = 0; j < n - m; j++) {
      PP pp = much(i, j);
      if (pp.first)
        ans = min(ans, pp.second);
    }

  return ans;
}

int main() {

  while (cin >> n >> m && n + m) {

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

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

    P ans = solve();

    for (int i = 0; i < 3; i++) {
      rotate();
      solve();
      ans = min(ans, solve());
    }

    if (ans != NA)
      cout << ans.first << " " << ans.second << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}