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

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

void QueueOut(priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                             greater<tuple<int, int, int>>>
                  todo) {
  while (!todo.empty()) {
    cout << get<0>(todo.top()) << "," << get<1>(todo.top()) << ","
         << get<2>(todo.top()) << endl;
    todo.pop();
  }
  cout << "output end" << endl;
  return;
}

int main() {
  int h, w;
  int sx, sy;
  int ex, ey;
  char s[1001][1001];

  cin >> h >> w;
  cin >> sx >> sy;
  cin >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

  // tuple : cnt_warp, x, y
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      todo;
  vector<vector<int>> warp(1001, vector<int>(1001, INF));
  warp[sx][sy] = 0;
  todo.push(make_tuple(0, sx, sy));

  // QueueOut(todo);

  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  bool can_flag = 0;

  vector<int> dwx;
  vector<int> dwy;
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 1 && j == 0)
        continue;
      if (i == -1 && j == 0)
        continue;
      if (i == 0 && j == 1)
        continue;
      if (i == 0 && j == -1)
        continue;
      dwx.push_back(i);
      dwy.push_back(j);
    }
  }

  while (!todo.empty()) {
    can_flag = 0;
    int x = get<1>(todo.top());
    int y = get<2>(todo.top());
    int nx = 0;
    int ny = 0;
    todo.pop();

    for (int i = 0; i < 4; i++) {
      nx = x + dx[i];
      ny = y + dy[i];
      if (nx >= h || ny >= w || nx < 0 || ny < 0)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (warp[nx][ny] <= warp[x][y])
        continue;
      warp[nx][ny] = warp[x][y];
      todo.push(make_tuple(warp[nx][ny], nx, ny));
    }
    for (int i = 0; i < 20; i++) {
      nx = x + dwx[i];
      ny = y + dwy[i];
      if (nx >= h || ny >= w || nx < 0 || ny < 0)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (warp[nx][ny] != INF)
        continue;
      warp[nx][ny] = warp[x][y] + 1;
      todo.push(make_tuple(warp[nx][ny], nx, ny));
    }
    // if(warp[ex][ey] != INF) break;
  }

  // for(int i = 0; i < h; i++){
  //   for(int j = 0; j < w; j++){
  //     cout << warp[i][j];
  //   }
  //   cout << endl;
  // }

  if (warp[ex][ey] != INF)
    cout << warp[ex][ey] << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

int main() {
  int h, w;
  int sx, sy;
  int ex, ey;
  char s[1001][1001];

  cin >> h >> w;
  cin >> sx >> sy;
  cin >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

  queue<pair<int, int>> todo;
  vector<vector<int>> seen(1001, vector<int>(1001, -1));
  todo.push(make_pair(sx, sy));
  seen[sx][sy] = 0;

  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  bool cant_flag = 0;
  int cnt = 0;

  vector<int> dwx;
  vector<int> dwy;
  vector<vector<int>> warp(1001, vector<int>(1001, INF));
  warp[sx][sy] = 0;
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      dwx.push_back(i);
      dwy.push_back(j);
    }
  }
  // for(int i = 0; i < 24; i++){
  //   cout << dx[i] << ", " << dy[i] << endl;
  // }

  while (!todo.empty()) {
    cant_flag = 0;
    int x = todo.front().first;
    int y = todo.front().second;
    todo.pop();

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= h || ny >= w || nx < 0 || ny < 0)
        continue;
      if (seen[nx][ny] >= 0 && warp[nx][ny] <= warp[x][y])
        continue;
      if (s[nx][ny] == '#')
        continue;
      cant_flag = 1;
    }

    if (cant_flag == 1) {
      // can walk to next map
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= h || ny >= w || nx < 0 || ny < 0)
          continue;
        if (seen[nx][ny] >= 0 && warp[nx][ny] < warp[x][y])
          continue;
        if (s[nx][ny] == '#')
          continue;
        seen[nx][ny] = seen[x][y] + 1;
        warp[nx][ny] = warp[x][y];
        todo.push(make_pair(nx, ny));
      }
    } else {
      // need to use warp;
      for (int i = 0; i < 24; i++) {
        int nx = x + dwx[i];
        int ny = y + dwy[i];
        if (nx >= h || ny >= w || nx < 0 || ny < 0)
          continue;
        if (seen[nx][ny] >= 0 && warp[nx][ny] < warp[x][y] + 1)
          continue;
        if (s[nx][ny] == '#')
          continue;
        todo.push(make_pair(nx, ny));
        warp[nx][ny] = warp[x][y] + 1;
        seen[nx][ny] = seen[x][y] + 1;
      }
    }
  }

  // for(int i = 0; i < h; i++){
  //   for(int j = 0; j < w; j++){
  //     cout << warp[i][j];
  //   }
  //   cout << endl;
  // }

  if (warp[ex][ey] != INF)
    cout << warp[ex][ey] << endl;
  else
    cout << -1 << endl;

  return 0;
}