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

#include <bits/stdc++.h>
using namespace std;
int inh, inw, ch, cw, dh, dw;
// vector <vector<char>> s;
// vector <vector<int >> mark;
char s[1000][1000];
int mark[1000][1000];
bool isValid(pair<int, int> p, int value) {
  // cout << "isvalid " << x << " " << y << endl;
  int x = p.first, y = p.second;
  if (x < 0)
    return false;
  if (y < 0)
    return false;
  if (x >= inw)
    return false;
  if (y >= inh)
    return false;
  if (s[y][x] == '#')
    return false;
  if (mark[y][x] == -1)
    return true;
  if (mark[y][x] <= value)
    return false;
  // cout << "true" << endl;
  return true;
}
bool markIt(pair<int, int> p, int value) {
  if (isValid(p, value)) {
    mark[p.second][p.first] = value;
    return true;
  }
  return false;
}
void myFill(int x, int y, int value) {
  // cout << x << " " << y << " " << value << endl;
  // dequeを使ってBFS
  deque<pair<int, int>> myDeque;
  myDeque.push_back({x, y});
  mark[y][x] = value;
  while (!myDeque.empty()) {
    pair<int, int> walk = myDeque.front();
    int cost = mark[walk.second][walk.first];
    myDeque.pop_front();
    pair<int, int> up = walk, down = walk, right = walk, left = walk;
    up.second++;
    down.second--;
    right.first++;
    left.first--;
    if (markIt(up, cost))
      myDeque.push_front(up);
    if (markIt(down, cost))
      myDeque.push_front(down);
    if (markIt(right, cost))
      myDeque.push_front(right);
    if (markIt(left, cost))
      myDeque.push_front(left);
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        pair<int, int> magic = walk;
        magic.first += i;
        magic.second += j;
        if (markIt(magic, cost + 1))
          myDeque.push_back(magic);
      }
    }
  };
}
int main() {
  cin >> inh >> inw >> ch >> cw >> dh >> dw;
  // s = vector<vector<char>>(inh,vector<char>(inw));
  // mark = vector<vector<int>>(inh,vector<int>(inw,-1));

  for (int i = 0; i < inh; i++) {
    for (int j = 0; j < inw; j++) {
      cin >> s[i][j];
      mark[i][j] = -1;
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  myFill(cw, ch, 0);
  cout << mark[dh][dw] << endl;

#if 0
    for(int i = 0; i < inh; i++){
        for(int j = 0; j < inw; j++){
            cout << mark[i][j]; 
        }
        cout << endl;
    }
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int inh, inw, ch, cw, dh, dw;
// vector <vector<char>> s;
// vector <vector<int >> mark;
char s[1000][1000];
int mark[1000][1000];
void inline myFill(int x, int y, int value) {
  // cout << x << " " << y << " " << value << endl;
  if (x < 0)
    return;
  if (y < 0)
    return;
  if (x >= inw)
    return;
  if (y >= inh)
    return;
  if ((mark[y][x] != -1) && (mark[y][x] <= value))
    return;
  if (s[y][x] == '.') {
    if ((mark[y][x] == -1) || (mark[y][x] > value)) {
      mark[y][x] = value;
      myFill(x + 1l, y, value);
      myFill(x - 1l, y, value);
      myFill(x, y + 1l, value);
      myFill(x, y - 1l, value);
      // corner
      myFill(x + 1l, y + 1l, value + 1l);
      myFill(x + 1l, y - 1l, value + 1l);
      myFill(x - 1l, y + 1l, value + 1l);
      myFill(x - 1l, y - 1l, value + 1l);
      // square
      for (int i = -2; i <= 2; i++) {
        myFill(x + i, y + 2, value + 1);
        myFill(x + i, y - 2, value + 1);
      }
      for (int i = -1; i <= 1; i++) {
        myFill(x - 2l, y + i, value + 1l);
        myFill(x + 2l, y + i, value + 1l);
      }
    }
  }
}
bool isValid(int x, int y) {
  // cout << "isvalid " << x << " " << y << endl;
  if (x < 0)
    return false;
  if (y < 0)
    return false;
  if (x >= inw)
    return false;
  if (y >= inh)
    return false;
  // cout << "true" << endl;
  return true;
}
int main() {
  cin >> inh >> inw >> ch >> cw >> dh >> dw;
  // s = vector<vector<char>>(inh,vector<char>(inw));
  // mark = vector<vector<int>>(inh,vector<int>(inw,-1));

  for (int i = 0; i < inh; i++) {
    for (int j = 0; j < inw; j++) {
      cin >> s[i][j];
      mark[i][j] = -1;
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  myFill(cw, ch, 0);
  cout << mark[dh][dw] << endl;

#if 0
    for(int i = 0; i < inh; i++){
        for(int j = 0; j < inw; j++){
            cout << mark[i][j]; 
        }
        cout << endl;
    }
#endif
  return 0;
}