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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
vector<vector<int>> rot(vector<vector<int>> &ary) {
  int n = (int)ary.size();
  vector<vector<int>> ret(n, vector<int>(n));
  REP(y, n) REP(x, n) ret[y][x] = ary[n - 1 - x][y];
  return ret;
}
int main(void) {
  int m1, m2;
  while (cin >> m1 >> m2) {
    if (m1 == 0)
      break;
    vector<vector<int>> view(m1, vector<int>(m1));
    REP(y, m1) REP(x, m1) cin >> view[y][x];
    vector<vector<int>> pict(m2, vector<int>(m2));
    REP(y, m2) REP(x, m2) cin >> pict[y][x];
    vector<PII> ans;
    REP(k, 4) {
      for (int sy = 0; sy + m2 - 1 < m1; sy++) {
        for (int sx = 0; sx + m2 - 1 < m1; sx++) {
          bool f = true;
          REP(dy, m2) REP(dx, m2) {
            if (pict[dy][dx] != -1 && pict[dy][dx] != view[sy + dy][sx + dx]) {
              f = false;
              goto READ;
            }
          }
        READ:
          if (f) {
            REP(y, m2) REP(x, m2) {
              if (pict[y][x] != -1) {
                ans.push_back(PII(sy + y, sx + x));
                goto END;
              }
            }
          }
        END:;
        }
      }
      pict = rot(pict);
    }
    if (ans.empty()) {
      cout << "NA" << endl;
    } else {
      sort(ans.begin(), ans.end());
      printf("%d %d\\n", ans[0].second + 1, ans[0].first + 1);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
vector<vector<int>> rot(vector<vector<int>> ary) {
  int n = (int)ary.size();
  vector<vector<int>> ret(n, vector<int>(n));
  REP(y, n) REP(x, n) ret[y][x] = ary[n - 1 - x][y];
  return ret;
}
int main(void) {
  int m1, m2;
  while (cin >> m1 >> m2) {
    if (m1 == 0)
      break;
    vector<vector<int>> view(m1, vector<int>(m1));
    REP(y, m1) REP(x, m1) cin >> view[y][x];
    vector<vector<int>> pict(m2, vector<int>(m2));
    REP(y, m2) REP(x, m2) cin >> pict[y][x];
    vector<PII> ans;
    REP(k, 4) {
      for (int sy = 0; sy + m2 - 1 < m1; sy++) {
        for (int sx = 0; sx + m2 - 1 < m1; sx++) {
          bool f = true;
          REP(dy, m2) REP(dx, m2) {
            if (pict[dy][dx] != -1 && pict[dy][dx] != view[sy + dy][sx + dx]) {
              f = false;
              goto READ;
            }
          }
        READ:
          if (f) {
            REP(y, m2) REP(x, m2) {
              if (pict[y][x] != -1) {
                ans.push_back(PII(sy + y, sx + x));
                goto END;
              }
            }
          }
          pict = rot(pict);
        }
      }
    END:;
    }
    if (ans.empty()) {
      cout << "NA" << endl;
    } else {
      sort(ans.begin(), ans.end());
      printf("%d %d\\n", ans[0].second + 1, ans[0].first + 1);
    }
  }
  return 0;
}