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

#include <bits/stdc++.h>
using namespace std;
int pos(int x, int y, int h, int w) { return x * w + y; }
int main() {
  int h, w, x1, y1, x2, y2;
  cin >> h >> w;
  cin >> x1 >> y1 >> x2 >> y2;
  int s = pos(x1 - 1, y1 - 1, h, w);
  int t = pos(x2 - 1, y2 - 1, h, w);
  char **G = new char *[h];
  for (int i = 0; i < h; i++) {
    G[i] = new char[w];
    for (int j = 0; j < w; j++) {
      cin >> G[i][j];
    }
  }

  deque<int> q;
  q.push_front(s);
  map<int, int> m;
  m[s] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    G[x / w][x % w] = '#';
    int a = x / w, b = x % w;
    if (a - 1 >= 0 && G[a - 1][b] == '.') {
      int y = pos(a - 1, b, h, w);
      q.push_front(y);
      if (m.find(y) != m.end() && m[y] <= m[x])
        m[y] = m[y];
      else {
        m[y] = m[x];
        q.push_back(y);
      }
    }
    if (a + 1 < h && G[a + 1][b] == '.') {
      int y = pos(a + 1, b, h, w);
      q.push_front(y);
      if (m.find(y) != m.end() && m[y] <= m[x])
        m[y] = m[y];
      else {
        m[y] = m[x];
        q.push_back(y);
      }
    }
    if (b - 1 >= 0 && G[a][b - 1] == '.') {
      int y = pos(a, b - 1, h, w);
      q.push_front(y);
      if (m.find(y) != m.end() && m[y] <= m[x])
        m[y] = m[y];
      else {
        m[y] = m[x];
        q.push_back(y);
      }
    }
    if (b + 1 < w && G[a][b + 1] == '.') {
      int y = pos(a, b + 1, h, w);
      q.push_front(y);
      if (m.find(y) != m.end() && m[y] <= m[x])
        m[y] = m[y];
      else {
        m[y] = m[x];
        q.push_back(y);
      }
    }
    for (int i = a - 2; i < a + 3; i++) {
      for (int j = b - 2; j < b + 3; j++) {
        if (i >= 0 && i < h && j >= 0 && j < w && G[i][j] == '.') {
          int y = pos(i, j, h, w);
          if (m.find(y) != m.end() && m[y] <= m[x] + 1)
            m[y] = m[y];
          else {
            m[y] = m[x] + 1;
            q.push_back(y);
          }
        }
      }
    }
  }
  if (m.find(t) != m.end())
    cout << m[t] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int pos(int x, int y, int h, int w) { return x * w + y; }
int main() {
  int h, w, x1, y1, x2, y2;
  cin >> h >> w;
  cin >> x1 >> y1 >> x2 >> y2;
  int s = pos(x1 - 1, y1 - 1, h, w);
  int t = pos(x2 - 1, y2 - 1, h, w);
  char **G = new char *[h];
  for (int i = 0; i < h; i++) {
    G[i] = new char[w];
    for (int j = 0; j < w; j++) {
      cin >> G[i][j];
    }
  }

  deque<int> q;
  q.push_front(s);
  map<int, int> m;
  m[s] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    G[x / w][x % w] = '#';
    int a = x / w, b = x % w;
    if (a - 1 >= 0 && G[a - 1][b] == '.') {
      int y = pos(a - 1, b, h, w);
      q.push_front(y);
      if (m.find(y) != m.end())
        m[y] = min(m[y], m[x]);
      else
        m[y] = m[x];
    }
    if (a + 1 < h && G[a + 1][b] == '.') {
      int y = pos(a + 1, b, h, w);
      q.push_front(y);
      if (m.find(y) != m.end())
        m[y] = min(m[y], m[x]);
      else
        m[y] = m[x];
    }
    if (b - 1 >= 0 && G[a][b - 1] == '.') {
      int y = pos(a, b - 1, h, w);
      q.push_front(y);
      if (m.find(y) != m.end())
        m[y] = min(m[y], m[x]);
      else
        m[y] = m[x];
    }
    if (b + 1 < w && G[a][b + 1] == '.') {
      int y = pos(a, b + 1, h, w);
      q.push_front(y);
      if (m.find(y) != m.end())
        m[y] = min(m[y], m[x]);
      else
        m[y] = m[x];
    }
    for (int i = a - 2; i < a + 3; i++) {
      for (int j = b - 2; j < b + 3; j++) {
        if (i >= 0 && i < h && j >= 0 && j < w && G[i][j] == '.') {
          int y = pos(i, j, h, w);
          q.push_back(y);
          if (m.find(y) != m.end())
            m[y] = min(m[y], m[x] + 1);
          else
            m[y] = m[x] + 1;
        }
      }
    }
  }
  if (m.find(t) != m.end())
    cout << m[t] << endl;
  else
    cout << -1 << endl;
}