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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;
int vis[1001][1001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<string> vec;

  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    vec.push_back(str);
  }

  int col = 1;
  vector<vector<int>> g(h * w + 2);

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vis[i][j] != 0 || vec[i][j] == '#')
        continue;

      queue<int> q;
      q.push(i);
      q.push(j);
      vis[i][j] = col;

      while (!q.empty()) {
        int x = q.front();
        q.pop();
        int y = q.front();
        q.pop();
        g[col].push_back(x);
        g[col].push_back(y);
        for (int k = 0; k < 4; k++) {
          int xx = dx[k] + x;
          int xy = dy[k] + y;

          if (xx < 0 || xx >= h || xy < 0 || xy >= w || vec[xx][xy] == '#' ||
              vis[xx][xy])
            continue;

          q.push(xx);
          q.push(xy);
          vis[xx][xy] = col;
        }
      }

      col++;
    }
  }

  //	for(int i = 1;i<col;i++)
  //	{
  //		for(int j = 0;j<g[i].size();j+=2)
  //		{
  //			cout<<'('<<g[i][j]<<','<<g[i][j+1]<<')'<<" ";
  //		}
  //		cout<<endl;
  //	}

  int start = vis[x1][y1];
  int end = vis[x2][y2];

  queue<int> q;
  vector<int> viss(col + 1, 0);
  q.push(start);
  q.push(0);
  viss[start] = 1;

  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int dis = q.front();
    q.pop();

    if (x == end) {
      cout << dis << endl;
      return 0;
    }

    for (int i = 0; i < g[x].size(); i += 2) {
      int xx = g[x][i];
      int xy = g[x][i + 1];

      for (int k = xx - 2; k <= xx + 2; k++) {
        for (int l = xy - 2; l <= xy + 2; l++) {
          if (k < 0 || k >= h || l < 0 || l >= w || vec[k][l] == '#' ||
              viss[vis[k][l]])
            continue;

          q.push(vis[k][l]);
          q.push(dis + 1);
          viss[vis[k][l]] = 1;
        }
      }
    }
  }

  cout << "-1" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;
int vis[1001][1001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<string> vec;

  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    vec.push_back(str);
  }

  int col = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vis[i][j] != 0 || vec[i][j] == '#')
        continue;

      queue<int> q;
      q.push(i);
      q.push(j);
      vis[i][j] = col;

      while (!q.empty()) {
        int x = q.front();
        q.pop();
        int y = q.front();
        q.pop();

        for (int k = 0; k < 4; k++) {
          int xx = dx[k] + x;
          int xy = dy[k] + y;

          if (xx < 0 || xx >= h || xy < 0 || xy >= w || vec[xx][xy] == '#' ||
              vis[xx][xy])
            continue;

          q.push(xx);
          q.push(xy);
          vis[xx][xy] = col;
        }
      }

      col++;
    }
  }

  vector<set<int>> g(col + 1);

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vec[i][j] == '#')
        continue;

      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (k < 0 || k >= h || l < 0 || l >= w || vec[k][l] == '#' ||
              vis[k][l] == vis[i][j])
            continue;

          g[vis[i][j]].insert(vis[k][l]);
          g[vis[k][l]].insert(vis[i][j]);
        }
      }
    }
  }

  int start = vis[x1][y1];
  int end = vis[x2][y2];

  queue<int> q;
  q.push(start);
  q.push(0);
  vector<int> viss(col + 1, 0);

  viss[start] = 1;

  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int dis = q.front();
    q.pop();

    if (x == end) {
      cout << dis << endl;
      return 0;
    }

    set<int>::iterator it;

    for (it = g[x].begin(); it != g[x].end(); it++) {
      int y = *it;

      if (viss[y])
        continue;

      q.push(y);
      q.push(dis + 1);
    }
  }

  cout << "-1" << endl;

  return 0;
}