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

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

using namespace std;

int main() {
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    int p;
    cin >> p;
    vector<vector<vector<int>>> flag(
        x + 1, vector<vector<int>>(y + 1, vector<int>(2, 1)));
    for (int s = 0; s <= x; s++) {
      for (int t = 0; t <= y; t++) {
        if (s == 0) {
          flag[s][t][1] = 0; // 左から
        }
        if (t == 0) {
          flag[s][t][0] = 0; // 上から
        }
      }
    }
    if (p != 0) {
      vector<vector<int>> mata(p, vector<int>(4));

      for (int j = 0; j < p; j++) {
        cin >> mata[j][0] >> mata[j][1] >> mata[j][2] >> mata[j][3];

        if (mata[j][0] == mata[j][2]) { // 上から
          if (mata[j][1] > mata[j][3]) {
            flag[mata[j][0]][mata[j][1]][0] = 0;
          } else {
            flag[mata[j][0]][mata[j][3]][0] = 0;
          }
        }
        if (mata[j][1] == mata[j][3]) {
          if (mata[j][0] > mata[j][2]) {
            flag[mata[j][0]][mata[j][1]][1] = 0;
          } else {
            flag[mata[j][2]][mata[j][1]][1] = 0;
          }
        }
      }
    }
    vector<vector<int>> dp(x + 1, vector<int>(y + 1, 0));
    dp[0][0] = 1;
    for (int n = 0; n <= x; n++) {
      for (int m = 0; m <= y; m++) {
        if (flag[n][m][0] == 1) {
          dp[n][m] += dp[n][m - 1];
        }
        if (flag[n][m][1] == 1) {
          dp[n][m] += dp[n - 1][m];
        }
      }
    }
    if (dp[x][y] == 0) {
      cout << "Miserable Hokusai!\\n";
    } else {
      cout << dp[x][y] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    int p;
    cin >> p;
    vector<vector<vector<int>>> flag(
        x + 1, vector<vector<int>>(y + 1, vector<int>(2, 1)));
    for (int s = 0; s <= x; s++) {
      for (int t = 0; t <= y; t++) {
        if (s == 0) {
          flag[s][t][1] = 0;
          cout << "hoge";
        }
        if (t == 0) {
          flag[s][t][0] = 0;
        }
      }
    }

    if (p != 0) {
      vector<vector<int>> mata(p, vector<int>(4));

      for (int j = 0; j < p; j++) {
        cin >> mata[j][0] >> mata[j][1] >> mata[j][2] >> mata[j][3];
      }

      for (int j = 0; j < p; j++) {
        if (mata[j][0] == mata[j][2]) {
          if (mata[j][1] > mata[j][3]) {
            flag[mata[j][0]][mata[j][1]][0] = 0;
          } else {
            flag[mata[j][0]][mata[j][3]][0] = 0;
          }
        }
        if (mata[j][1] == mata[j][3]) {
          if (mata[j][0] > mata[j][2]) {
            flag[mata[j][0]][mata[j][1]][1] = 0;
          } else {
            flag[mata[j][0]][mata[j][2]][1] = 0;
          }
        }
      }
    }
    vector<vector<int>> dp(x, vector<int>(y, 0));
    dp[0][0] = 1;
    for (int n = 0; n <= x; n++) {
      for (int m = 0; m <= y; m) {
        if (flag[n][m][0] == 1) {
          dp[n][m] += dp[n][m - 1];
        }
        if (flag[n][m][0] == 1) {
          dp[n][m] += dp[n - 1][m];
        }
      }
    }
    if (dp[x][y] == 0) {
      cout << "Miserable Hokusai!\\n";
    } else {
      cout << dp[x][y] << endl;
    }
  }

  return 0;
}