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


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define pii pair<int, int>
#define vpli vector<pair<long long, int>>
#define deb(x) cout << "deb " << #x << " " << x << endl
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define endl "\\n"
template <typename T> void printvec(vector<T> &v, int n, int start = 0) {
  // cout << "\\nprinting vector\\n";
  for (int i = start; i < n; i++) {
    cout << v[i] << " ";
  }
  cout << "done printing";
  cout << endl;
}
const int mod = 1e9 + 7;
int h, w, sh, sw, dh, dw;

inline bool valid(int row, int col) {
  return row >= 0 && col >= 0 && row < h && col < w;
}

void solve() {
  int cnt = 0;
  cin >> h >> w >> sh >> sw >> dh >> dw;
  sh--, sw--, dh--, dw--;
  vector<string> grid(h);
  for (string &x : grid)
    cin >> x;
  vvi dist(h, vi(w, 1e7));
  deque<pii> q;
  q.push_front({sh, sw});
  // vvb vis(h, vb(w));
  dist[sh][sw] = 0;
  // vis[sh][sw] = 1;
  int d;
  int r, c;
  while (!q.empty()) {
    pii x = q.front();
    r = x.first;
    c = x.second;
    // cout<<r<<" "<<c<<endl;
    q.pop_front();
    if (r == dh && c == dw)
      break;
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        // cout<<r-dx<<" "<<c+dy<<endl;
        if (!valid(r + dx, c + dy) || grid[r + dx][c + dy] == '#' ||
            (dx == 0 && dy == 0))
          continue;
        d = 0;
        if (!((abs(dx) == 1 && dy == 0) || (dx == 0 && abs(dy) == 1)))
          d = 1;
        if (dist[r + dx][c + dy] > dist[r][c] + d) {
          dist[r + dx][c + dy] = dist[r][c] + d;
          if (d == 0)
            q.push_front({r + dx, c + dy});
          else
            q.push_back({r + dx, c + dy});
        }
      }
    }
  }
  if (r != dh && c != dw)
    dist[dh][dw] = -1;
  cout << dist[dh][dw] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define pii pair<int, int>
#define vpli vector<pair<long long, int>>
#define deb(x) cout << "deb " << #x << " " << x << endl
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define endl "\\n"
template <typename T> void printvec(vector<T> &v, int n, int start = 0) {
  // cout << "\\nprinting vector\\n";
  for (int i = start; i < n; i++) {
    cout << v[i] << " ";
  }
  cout << "done printing";
  cout << endl;
}
const int mod = 1e9 + 7;
int h, w, sh, sw, dh, dw;

inline bool valid(int row, int col) {
  return row >= 0 && col >= 0 && row < h && col < w;
}

void solve() {
  cin >> h >> w >> sh >> sw >> dh >> dw;
  sh--, sw--, dh--, dw--;
  vector<string> grid(h);
  for (string &x : grid)
    cin >> x;
  vvi dist(h, vi(w, 1e7));
  queue<pii> q;
  q.push({sh, sw});
  vvb vis(h, vb(w));
  dist[sh][sw] = 0;
  vis[sh][sw] = 1;
  int d;
  int r, c;
  while (!q.empty()) {
    r = q.front().first;
    c = q.front().second;
    vis[r][c] = 1;
    q.pop();
    if (r == dh && c == dw)
      break;
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        // cout<<r-dx<<" "<<c+dy<<endl;
        if (!valid(r + dx, c + dy) || grid[r + dx][c + dy] == '#' ||
            vis[r + dx][c + dy])
          continue;
        q.push({r + dx, c + dy});
        d = 0;
        if (!((abs(dx) == 1 && abs(dy) == 0) || (abs(dx) == 0 && abs(dy) == 1)))
          d = 1;
        dist[r + dx][c + dy] = min(dist[r + dx][c + dy], d + dist[r][c]);
      }
    }
  }
  if (r != dh && c != dw)
    dist[dh][dw] = -1;
  cout << dist[dh][dw] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}