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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cerr << v[a][b] << ' ';                                                \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

bool cant[20][20];
int a, b, ans;

int main() {
  while (cin >> a >> b, a) {
    REP(i, 20) REP(j, 20) cant[i][j] = false;
    ans = 0;
    int n;
    cin >> n;

    REP(i, n) {
      int x, y;
      cin >> x >> y;
      cant[x - 1][y - 1] = true;
    }

    queue<pair<int, int>> q;
    q.push(MP(0, 0));

    while (!q.empty()) {
      pair<int, int> tmp = q.front();
      q.pop();

      if (tmp.FI == a - 1 && tmp.SE == b - 1) {
        ans++;
        continue;
      }

      if (cant[tmp.FI][tmp.SE] == true) {
        continue;
      }

      if (tmp.FI < a - 1) {
        q.push(MP(tmp.FI + 1, tmp.SE));
      }

      if (tmp.SE < b - 1) {
        q.push(MP(tmp.FI, tmp.SE + 1));
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cerr << v[a][b] << ' ';                                                \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

bool cant[20][20];
int a, b, ans;

bool check(vector<int> v) {
  int x = 0, y = 0;
  REP(i, v.size()) {
    if (v[i] == 0) {
      x++;
    } else {
      y++;
    }
    if (cant[x][y]) {
      return false;
    }
  }

  return true;
}

int main() {
  while (cin >> a >> b, a) {
    REP(i, 20) REP(j, 20) cant[i][j] = false;
    ans = 0;
    int n;
    cin >> n;

    REP(i, n) {
      int x, y;
      cin >> x >> y;
      cant[x - 1][y - 1] = true;
    }

    vector<int> v;

    REP(i, a - 1) { v.PB(0); }
    REP(i, b - 1) { v.PB(1); }

    do {
      if (check(v)) {
        ans++;
      }
    } while (next_permutation(ALL(v)));

    cout << ans << endl;
  }

  return 0;
}