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

#include <bits/stdc++.h>

using namespace std;

void f(int set[][501], int m) {
  int ss[100][100] = {};

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      ss[j][(m - 1) - i] = set[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      set[i][j] = ss[i][j];
    }
  }
}

void log(int set[][501], int m, int setcount) {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      printf("%02d ", set[i][j]);
    }
    cout << endl;
  }

  cout << setcount << endl;
}

int main() {
  int n, m;

  while (cin >> n >> m && n) {
    int flag = 0;
    int setcount = 0;
    int count = 0;
    int data[201][201] = {};
    int set[501][501] = {};
    int i, j, k, l;
    int ans[150][150] = {};

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

    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        cin >> set[i][j];
        if (set[i][j] >= 0)
          setcount++;
      }
    }

    for (int z = 0; z < 4; z++) {
      int first = 0;
      for (int l = 0; l < n - m + 1; l++) {
        for (int k = 0; k < n - m + 1; k++) {
          count = 0;
          for (i = 0; i < m; i++) {
            for (j = 0; j < m; j++) {
              if (set[i][j] == data[i + l][j + k])
                count++;
              if (count == setcount)
                flag = 1;
            }
          }
          if (count == setcount) {
            for (i = 0; i < m; i++) {
              for (j = 0; j < m; j++) {
                if (set[i][j] == data[i + l][j + k] && !first) {
                  ans[i + l][j + k]++;
                  first = 1;
                  break;
                }
              }
              if (first)
                break;
            }
          }
          if (first)
            break;
        }
        if (first)
          break;
      }
      f(set, m);
    }
    if (flag) {
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          if (ans[i][j]) {
            cout << j + 1 << " " << i + 1 << endl;
            break;
          }
        }
        if (ans[i][j])
          break;
      }
    } else if (!flag)
      cout << "NA" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void f(int set[][501], int m) {
  int ans[100][100] = {};

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      ans[j][(m - 1) - i] = set[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      set[i][j] = ans[i][j];
    }
  }
}

int main() {
  int n, m;

  while (cin >> n >> m && n) {
    int flag = 0;
    int setcount = 0;
    int count = 0;
    int data[101][101] = {};
    int set[501][501] = {};
    int i, j, k, l;

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

    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        cin >> set[i][j];
        if (set[i][j] >= 0)
          setcount++;
      }
    }

    for (int z = 0; z < 4; z++) {
      for (int l = 0; l < n; l++) {
        for (int k = 0; k < n; k++) {
          count = 0;
          for (i = 0; i < m; i++) {
            for (j = 0; j < m; j++) {
              if (set[i][j] == data[i + k][j + l])
                count++;
              if (count == setcount) {
                cout << l + i << " " << k + j << endl;
                flag = 1;
                break;
              }
            }
            if (flag)
              break;
          }
          if (flag)
            break;
        }
        if (flag)
          break;
      }
      if (flag)
        break;
      else
        f(set, m);
    }
    if (!flag)
      cout << "NA" << endl;
  }

  return 0;
}