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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x-- > 0)
#define memset(a, b) memset(a, b, sizeof(a))
#define debug(x) cout << "[ " << #x << " : " << x << " ]" << endl;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
}
/*
think fast
think simple
*/
struct node {
  int a, b, c;
  node(){};
  node(int a, int b) {
    this->a = a;
    this->b = b;
  }
};

int n, m;
vector<vector<char>> v;
int x, y, fx, fy;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int dxx[] = {-2, -1, 0, 1, 2, -2, 2, -2, 2, -2, 2, -2, -1, 0, 1, 2};
int dyy[] = {2, 2, 2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2, -2, -2};

int dis[1009][1009];
bool check(int x, int y) {
  if ((0 < x) and (x <= n) and (0 < y) and (y <= m) and (v[x][y] == '.'))
    return 1;
  return 0;
}
void bfs(int x, int y) {
  deque<node> q;
  q.pb(node(x, y));
  dis[x][y] = 0;
  while (q.size()) {
    auto fr = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = fr.a + dx[i];
      int ny = fr.b + dy[i];

      if (check(nx, ny)) {
        // cout << "here\\n";
        if (dis[nx][ny] > dis[fr.a][fr.b]) {
          dis[nx][ny] = dis[fr.a][fr.b];
          q.push_front(node(nx, ny));
        }
      }
    }
    for (int x = fr.a - 2; x <= fr.a + 2; x++)
      for (int y = fr.b - 2; y <= fr.b + 2; y++)
        if (check(x, y)) {
          if (dis[x][y] > dis[fr.a][fr.b] + 1) {
            dis[x][y] = dis[fr.a][fr.b] + 1;
            q.pb(node(x, y));
          }
        }
  }
}
void mysol() {
  for (auto &i : dis)
    for (auto &a : i)
      a = 1e10;

  cin >> n >> m;

  v.resize(n + 9);
  for (auto &i : v)
    i.assign(m + 9, '#');

  cin >> x >> y >> fx >> fy;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> v[i][j];

  bfs(x, y);

  cout << (dis[fx][fy] >= 1e10 ? -1 : dis[fx][fy]);
}

int32_t main() {
  fast();
  // w(t)
  { mysol(); }
  // cerr << sizeof(dp)*(1e-6) << " megabytes\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x-- > 0)
#define memset(a, b) memset(a, b, sizeof(a))
#define debug(x) cout << "[ " << #x << " : " << x << " ]" << endl;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
}
/*
think fast
think simple
*/
struct node {
  int a, b, c;
  node(){};
  node(int a, int b, int c) {
    this->a = a;
    this->b = b;
    this->c = c;
  }
};

int n, m;
vector<vector<char>> v;
int x, y, fx, fy;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int dxx[] = {-2, -1, 0, 1, 2, -2, 2, -2, 2, -2, 2, -2, -1, 0, 1, 2};
int dyy[] = {2, 2, 2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2, -2, -2};

int vis[1009][1009];
int ans = INT_MAX;
bool check(int x, int y) {
  if ((0 < x) and (x <= n) and (0 < y) and (y <= m))
    return 1;
  return 0;
}
void bfs(int x, int y) {
  queue<node> q;
  q.push(node(x, y, 0));
  while (q.size()) {
    auto fr = q.front();
    q.pop();
    if (fr.a == fx and fr.b == fy) {
      // cout << "eureka";
      ans = min(ans, fr.c);
      continue;
    }
    vis[fr.a][fr.b] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = fr.a + dx[i];
      int ny = fr.b + dy[i];
      if (check(nx, ny)) {
        if (v[nx][ny] == '.') {
          if (vis[nx][ny] == -1) {
            vis[nx][ny] = fr.c;
            q.push(node(nx, ny, fr.c));
          } else if (vis[nx][ny] > fr.c) {
            vis[nx][ny] = fr.c;
            q.push(node(nx, ny, fr.c));
          }
        } else {
          // cout << "here";
          // cout << fr.a << ' ' << fr.b << endl;
          for (int j = 0; j < 16; j++) {
            int jx = fr.a + dxx[j];
            int jy = fr.b + dyy[j];
            // cout << jx << ' ' << jy;
            if (check(jx, jy) and (v[jx][jy] == '.')) {
              if (vis[jx][jy] == -1) {
                // cout << "<-";
                vis[jx][jy] = fr.c + 1;
                q.push(node(jx, jy, fr.c + 1));
              } else if (vis[jx][jy] > fr.c + 1) {
                vis[jx][jy] = fr.c + 1;
                q.push(node(jx, jy, fr.c + 1));
              }
            }
            // cout << endl;
          }
        }
      }
    }
  }
}
void mysol() {
  memset(vis, -1);
  cin >> n >> m;

  v.resize(n + 99);
  for (auto &i : v)
    i.assign(m + 99, '#');

  cin >> x >> y >> fx >> fy;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> v[i][j];
  // for (int i = 1; i <= n; i++) {
  //     for (int j = 1; j <= m; j++)
  //         cout << v[i][j];
  //     cout << endl;
  // }
  bfs(x, y);
  cout << (ans == INT_MAX ? -1 : ans);
}

int32_t main() {
  fast();
  // w(t)
  { mysol(); }
  // cerr << sizeof(dp)*(1e-6) << " megabytes\\n";
  return 0;
}