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

#include <algorithm>
#include <cstdio>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e3 + 5;

struct Triple {
  int x, y, val;
  Triple(int x = 0, int y = 0, int val = INF) : x(x), y(y), val(val) {}
  bool operator==(const Triple &rhs) const {
    return (x == rhs.x && y == rhs.y && val == rhs.val);
  }

  void operator=(const Triple &rhs) {
    x = rhs.x;
    y = rhs.y;
    val = rhs.val;
  }
};

char ch[maxn][maxn];
int vis[maxn][maxn], res[maxn][maxn], n, m, p, q, r, s;
queue<Triple> t;

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &p, &q, &r, &s);
  for (int i = 1; i <= n; i++)
    scanf("%s", ch[i] + 1);

  t.push(Triple(p, q, 1));

  while (!t.empty()) {
    Triple u = t.front();
    t.pop();
    if (res[u.x][u.y])
      continue;
    res[u.x][u.y] = u.val;

    queue<Triple> a1;
    a1.push(u);
    while (!a1.empty()) {
      Triple c = a1.front();
      a1.pop();
      int x = c.x;
      int y = c.y;

      for (int i = -2; i <= 2; i++) {
        if (x + i < 1 || x + i > n)
          continue;
        for (int j = -2; j <= 2; j++) {
          if (y + j < 1 || y + j > m)
            continue;
          if (abs(i) + abs(j) == 1 && !res[x + i][y + j] &&
              ch[x + i][y + j] == '.') {
            res[x + i][y + j] = res[x][y];
            a1.push(Triple(x + i, y + j, res[x][y]));
          } else if (abs(i) + abs(j) >= 2 && !res[x + i][y + j] &&
                     ch[x + i][y + j] == '.') {
            t.push(Triple(x + i, y + j, res[x][y] + 1));
          }
        }
      }
    }

    if (res[r][s])
      break;
  }

  // printf("x = %d, y = %d\\n", x, y);
  // printf("res:\\n");
  // for (int i = 1; i <= n; i++) {
  //     for (int j = 1; j <= m; j++)
  //         printf("%d ", res[i][j]);
  //     putchar('\\n');
  // }
  // printf("vis:\\n");
  // for (int i = 1; i <= n; i++) {
  //     for (int j = 1; j <= m; j++)
  //         printf("%d ", vis[i][j]);
  //     putchar('\\n');
  // }
  // printf("--------\\n");

  printf("%d\\n", res[r][s] - 1);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int maxn = 1e3 + 5;

struct Triple {
  int first, second, third;
  Triple(int x = 0, int y = 0, int z = 0) : first(x), second(y), third(z) {}
};

char s[maxn][maxn];
int h, w, a, b;
int vis[maxn][maxn];
Triple bg, ed;

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d", &a, &b);
  bg.first = a, bg.second = b, bg.third = 1;
  scanf("%d%d", &a, &b);
  ed.first = a, ed.second = b;

  vis[bg.first][bg.second] = 1;

  for (int i = 1; i <= h; i++)
    scanf("%s", s[i] + 1);

  queue<Triple> q;
  q.push(bg);
  while (!q.empty()) {
    Triple u = q.front();
    q.pop();
    int x = u.first, y = u.second;
    if (vis[x][y] != u.third)
      continue;

    for (int i = -1; i <= 1; i += 2) {
      if (x + i >= 1 && x + i <= h) {
        if (s[x + i][y] == '.') {
          if (!vis[x + i][y] || vis[x + i][y] > vis[x][y]) {
            vis[x + i][y] = vis[x][y];
            q.push(Triple(x + i, y, vis[x][y]));
          }
        }
      }
      if (y + i >= 1 && y + i <= w) {
        if (s[x][y + i] == '.') {
          if (!vis[x][y + i] || vis[x][y + i] > vis[x][y]) {
            vis[x][y + i] = vis[x][y];
            q.push(Triple(x, y + i, vis[x][y]));
          }
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      if (x + i >= 1 && x + i <= h) {
        for (int j = -2; j <= 2; j++) {
          if (y + j >= 1 && y + j <= w) {
            if (abs(i) + abs(j) < 2)
              continue;
            if (s[x + i][y + j] == '.') {
              if (!vis[x + i][y + j] || vis[x + i][y + j] > vis[x][y] + 1) {
                vis[x + i][y + j] = vis[x][y] + 1;
                q.push(Triple(x + i, y + j, vis[x + i][y + j]));
              }
            }
          }
        }
      }
    }
  }

  printf("%d\\n", vis[ed.first][ed.second] - 1);
  return 0;
}