Problem p00209 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#define N 100
#define M 50
#define Pair pair<int, int>
#define x first
#define y second

using namespace std;

int n, m;
int data[N][N], data_[4][M][M];
Pair p[4];

void spin(int k) {
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j) {
      data_[k][m - j - 1][i] = data_[k - 1][i][j];
    }
  }
}

void top_point() {
  for (int k = 0; k < 4; ++k) {
    for (int i = 0; i < m; ++i) {
      if (data_[k][0][i] != -1) {
        p[k].x = 0;
        p[k].y = i;
        break;
      }
    }
  }
}

bool check(int u, int v, int k, int i, int j) {
  if (data_[k][i][j] == -1)
    return true;
  return data[u][v] == data_[k][i][j];
}

bool solve2(int u, int v, int k) {
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j) {
      if (!check(u + i, v + j, k, i, j))
        return false;
    }
  }
  return true;
}

bool solve(Pair *pair) {
  for (int u = 0; u <= n - m; ++u) {
    for (int v = 0; v <= n - m; ++v) {
      for (int k = 0; k < 4; ++k) {
        if (solve2(u, v, k)) {
          pair->x = u + p[k].x;
          pair->y = v + p[k].y;
          return true;
        }
      }
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> data[i][j];
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> data_[0][i][j];
      }
    }
    spin(1);
    spin(2);
    spin(3);
    top_point();
    Pair pair;
    if (solve(&pair))
      cout << pair.y + 1 << " " << pair.x + 1 << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

vector<vector<int>> MakeMatrix(int);
multimap<int, int> SpinMatrix(vector<vector<int>> &);
void SearchPicture(vector<vector<int>>, vector<vector<int>>, multimap<int, int>,
                   int &, int &);
bool SearchPictureInner(vector<vector<int>>, vector<vector<int>>,
                        multimap<int, int>, int, int);
int main() {
  int i, n, m, x, y;
  multimap<int, int> info;
  vector<vector<int>> data1, data2;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    data1 = MakeMatrix(n);
    data2 = MakeMatrix(m);
    x = data1.size() - data2.size() + 1;
    y = 0;

    for (i = 0; i < 4; ++i) {
      info = SpinMatrix(data2);
      SearchPicture(data1, data2, info, x, y);
    }

    if (x == data1.size() - data2.size() + 1)
      cout << "NA" << endl;
    else
      cout << y + 1 << " " << x + 1 << endl;
  }
  return 0;
}

vector<vector<int>> MakeMatrix(int n) {
  int i, j, x;
  vector<int> _data;
  vector<vector<int>> data;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      cin >> x;
      _data.push_back(x);
    }
    data.push_back(_data);
    _data.clear();
  }
  return data;
}

multimap<int, int> SpinMatrix(vector<vector<int>> &data) {
  int i, j;
  vector<int> __data;
  vector<vector<int>> _data;
  multimap<int, int> map_data;

  for (i = 0; i < data.size(); ++i)
    __data.push_back(0);
  for (i = 0; i < data.size(); ++i)
    _data.push_back(__data);

  for (i = 0; i < data.size(); ++i) {
    for (j = 0; j < data.size(); ++j) {
      _data[i][j] = data[data.size() - 1 - j][i];
      if (_data[i][j] != -1)
        map_data.insert(make_pair(i, j));
    }
  }
  data = _data;
  return map_data;
}

void SearchPicture(vector<vector<int>> data1, vector<vector<int>> data2,
                   multimap<int, int> info, int &x, int &y) {
  int i, j, n;
  if (x == data1.size() - data2.size() + 1)
    n = x - 1;
  else
    n = x;

  for (i = 0; i <= n; ++i) {
    for (j = 0; j <= n; ++j) {
      if (SearchPictureInner(data1, data2, info, i, j)) {
        if (i + info.begin()->first < x ||
            (i + info.begin()->first == x && j + info.begin()->second < y)) {
          x = i + info.begin()->first;
          y = j + info.begin()->second;
        }
        return;
      }
    }
  }
}

bool SearchPictureInner(vector<vector<int>> data1, vector<vector<int>> data2,
                        multimap<int, int> info, int n, int m) {
  multimap<int, int>::iterator i;
  for (i = info.begin(); i != info.end(); ++i) {
    if (data1[n + i->first][m + i->second] != data2[i->first][i->second])
      return false;
  }
  return true;
}