Problem p03457 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> p;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool isFind = false;
int safe(int i, int j) {
  return (0 <= i && i < 100000) && (0 <= j && j < 100000);
}
void f(int i, int j, int cnt, int index) {

  if (i < 0 || j < 0)
    return;
  if (cnt == p[index][0]) {
    if (i == p[index][1] && j == p[index][2])
      isFind = true;
    return;
  }

  f(i + 1, j, cnt + 1, index);
  f(i - 1, j, cnt + 1, index);
  f(i, j + 1, cnt + 1, index);
  f(i, j - 1, cnt + 1, index);
}

// x , y, time, postion
void g(int i, int j, int cnt, int index) {
  queue<vector<int>> q;
  int t, x, y;
  q.push({i, j, cnt});

  while (!q.empty()) {
    x = q.front()[0];
    y = q.front()[1];
    t = q.front()[2];
    q.pop();

    if (t == p[index][0]) {
      if (x == p[index][1] && y == p[index][2]) {
        isFind = true;
        break;
      }
    } else {

      q.push({x + 1, y, t + 1});
      q.push({x - 1, y, t + 1});
      q.push({x, y + 1, t + 1});
      q.push({x, y - 1, t + 1});
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N;

  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    p.push_back({a, b, c});
  }

  int x, y, time;
  x = y = time = 0;
  for (int i = 0; i < p.size(); i++) {
    isFind = false;
    int dist = (abs(x - p[i][1]) + abs(y - p[i][2]));
    int t = p[i][0] - time;
    if (dist <= t) {
      if (dist % 2 == t % 2)
        f(x, y, time, i);
      else {
        cout << "No\n";
        return 0;
      }

      x = p[i][1];
      y = p[i][2];
      time = p[i][0];
    } else {
      cout << "No\n";
      return 0;
    }
    if (isFind == false) {
      cout << "No\n";
      return 0;
    }
  }
  cout << "Yes\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> p;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool isFind = false;
int safe(int i, int j) {
  return (0 <= i && i < 100000) && (0 <= j && j < 100000);
}
void f(int i, int j, int cnt, int index) {

  if (i < 0 || j < 0)
    return;
  if (cnt == p[index][0]) {
    if (i == p[index][1] && j == p[index][2])
      isFind = true;
    return;
  }

  f(i + 1, j, cnt + 1, index);
  f(i - 1, j, cnt + 1, index);
  f(i, j + 1, cnt + 1, index);
  f(i, j - 1, cnt + 1, index);
}

// x , y, time, postion
void g(int i, int j, int cnt, int index) {
  queue<vector<int>> q;
  int t, x, y;
  q.push({i, j, cnt});

  while (!q.empty()) {
    x = q.front()[0];
    y = q.front()[1];
    t = q.front()[2];
    q.pop();

    if (t == p[index][0]) {
      if (x == p[index][1] && y == p[index][2]) {
        isFind = true;
        break;
      }
    } else {

      q.push({x + 1, y, t + 1});
      q.push({x - 1, y, t + 1});
      q.push({x, y + 1, t + 1});
      q.push({x, y - 1, t + 1});
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N;

  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    p.push_back({a, b, c});
  }

  int x, y, time;
  x = y = time = 0;
  for (int i = 0; i < p.size(); i++) {
    isFind = false;
    int dist = (abs(x - p[i][1]) + abs(y - p[i][2]));
    int t = p[i][0] - time;
    if (dist <= t) {
      if (dist % 2 == t % 2)
        continue; // f(x, y, time, i);
      else {
        cout << "No\n";
        return 0;
      }

      x = p[i][1];
      y = p[i][2];
      time = p[i][0];
    } else {
      cout << "No\n";
      return 0;
    }
    if (isFind == false) {
      cout << "No\n";
      return 0;
    }
  }
  cout << "Yes\n";
  return 0;
}