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

#include <cassert>

#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1000000000
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define MOD 1000000007LL;
using namespace std;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int a, b;
int ans = 0;
vector<vector<bool>> v;
void dfs(int y, int x) {
  // cout<<x<<" "<<y<<endl;
  if (y == b - 1 && x == a - 1) {
    ans++;
  }
  if (y + 1 < b && v[y + 1][x])
    dfs(y + 1, x);
  if (x + 1 < a && v[y][x + 1])
    dfs(y, x + 1);
  return;
}

int main() {
  while (cin >> a >> b && a && b) {
    ans = 0;
    v.clear();
    v.resize(b, vector<bool>(a, true));
    int n;
    cin >> n;
    rep(i, 0, n) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      v[y][x] = false;
    }
    dfs(0, 0);
    cout << ans << endl;
  }
  return 0;
}

/*
3 3 3 4 4
3 3 4 4 4
3 3 3 3 3

*/


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
#define fcout cout << fixed << setprecision(10)
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// template<typename T>void pp(T x){cout<<x<<endl;}

using namespace std;
template <typename T> void test(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

int w, h;
int ans = 0;
void dfs(int y, int x, vector<vector<int>> v) {
  if (v[y][x] == 1)
    return;
  if (y == h - 1 && x == w - 1)
    ans++;
  int ny = y + 1, nx = x + 1;
  if (ny < h)
    dfs(ny, x, v);
  if (nx < w)
    dfs(y, nx, v);
  return;
}
int main() {
  while (cin >> w >> h && w && h) {
    ans = 0;
    vector<vector<int>> v(h, vector<int>(w, 0));
    int n;
    cin >> n;
    rep(i, 0, n) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      v[y][x] = 1;
    }
    dfs(0, 0, v);
    cout << ans << endl;
  }
  return 0;
}