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

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

int H, W;
vector<vector<char>> maze;
vector<vector<bool>> seen;
vector<vector<int>> dist;
queue<pair<int, int>> walked;

void walk(int h, int w, int n) {
  seen[h][w] = true;
  queue<pair<int, int>> que;
  pair<int, int> start;
  start.first = h;
  start.second = w;
  que.push(start);
  while (!que.empty()) {
    pair<int, int> nowon = que.front();
    que.pop();
    walked.push(nowon);
    dist[nowon.first][nowon.second] = n;
    for (int i = -1; i <= 1; i += 2) {
      if (nowon.first + i >= 0 && nowon.first + i < H) {
        if (!seen[nowon.first + i][nowon.second] &&
            maze[nowon.first + i][nowon.second] == '.') {
          pair<int, int> next;
          next.first = nowon.first + i;
          next.second = nowon.second;
          que.push(next);
          seen[next.first][next.second] = true;
        }
      }
    }
    for (int i = -1; i <= 1; i += 2) {
      if (nowon.second + i >= 0 && nowon.second + i < W) {
        if (!seen[nowon.first][nowon.second + i] &&
            maze[nowon.first][nowon.second + i] == '.') {
          pair<int, int> next;
          next.first = nowon.first;
          next.second = nowon.second + i;
          que.push(next);
          seen[next.first][next.second] = true;
        }
      }
    }
  }
}

int main() {
  // 入力受け取り
  //  ---------------------------------------------
  cin >> H >> W;
  pair<int, int> start;
  cin >> start.first >> start.second;
  pair<int, int> goal;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  // 配列初期化
  maze = vector<vector<char>>(H, vector<char>(W));
  seen = vector<vector<bool>>(H, vector<bool>(W, false));
  dist = vector<vector<int>>(H, vector<int>(W, -1));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> maze[i][j];
  }
  // cout << "hey" << endl;
  //  ---------------------------------------------
  // 処理
  // 1. スタートから歩ける場所を列挙
  // 2. 歩いた場所からワープできる場所を列挙
  // 3. ワープ先から歩ける場所を列挙　以下ループ
  int warp_num = 0;
  queue<pair<int, int>> warp_to;
  warp_to.push(start);
  while (true) {
    while (!warp_to.empty()) {
      pair<int, int> v = warp_to.front();
      warp_to.pop();
      if (!seen[v.first][v.second] && maze[v.first][v.second] == '.')
        walk(v.first, v.second, warp_num);
    }

    while (!walked.empty()) {
      pair<int, int> v = walked.front();
      walked.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (v.first + i >= 0 && v.first + i < H && v.second + j >= 0 &&
              v.second + j < W) {
            if (!seen[v.first + i][v.second + j] &&
                maze[v.first + i][v.second + j] == '.') {
              pair<int, int> w;
              w.first = v.first + i;
              w.second = v.second + j;
              warp_to.push(w);
            }
          }
        }
      }
    }
    warp_num++;
    if (warp_to.empty())
      break;
  }

  // デバッグ用：全体図を出力
  // ---------------------------------------------
  /*
  for (int i=0; i<H; i++)
  {
      for (int j=0; j<W; j++)
      {
          cout << dist[i][j] << " ";
      }
      cout << endl;
  }
  */
  // ---------------------------------------------

  cout << dist[goal.first][goal.second] << endl;
}

Error: TLE

Fixed submission:

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

int H, W;
vector<vector<char>> meiro(H, vector<char>(W));
vector<vector<bool>> seen(H, vector<bool>(W, false));
vector<vector<int>> warp(H, vector<int>(W, -1));
vector<vector<int>> cget;
vector<vector<bool>> note(H, vector<bool>(W, false));

void search(int h, int w) {
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (h + i >= 0 && h + i < H && w + j >= 0 && w + j < W) {
        if (meiro[h + i][w + j] == '.' && !note[h + i][w + j]) {
          vector<int> tmp(2);
          tmp[0] = h + i;
          tmp[1] = w + j;
          cget.push_back(tmp);
          note[h + i][w + j] = true;
        }
      }
    }
  }
}

queue<pair<int, int>> que;
void walk(int h, int w, int n) {
  pair<int, int> v;
  v.first = h;
  v.second = w;
  que.push(v);
  seen[h][w] = true;
  warp[h][w] = n;
  while (!que.empty()) {
    v = que.front();
    que.pop();
    search(v.first, v.second);
    for (int i = -1; i <= 1; i++) {
      if (v.first + i >= 0 && v.first + i < H) {
        if (seen[v.first + i][v.second] == false &&
            meiro[v.first + i][v.second] == '.') {
          pair<int, int> next;
          next.first = v.first + i;
          next.second = v.second;
          que.push(next);
          seen[next.first][next.second] = true;
          warp[next.first][next.second] = n;
        }
      }
    }
    for (int i = -1; i <= 1; i++) {
      if (v.second + i >= 0 && v.second + i < W) {
        if (seen[v.first][v.second + i] == false &&
            meiro[v.first][v.second + i] == '.') {
          pair<int, int> next;
          next.first = v.first;
          next.second = v.second + i;
          que.push(next);
          seen[next.first][next.second] = true;
          warp[next.first][next.second] = n;
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  meiro = vector<vector<char>>(H, vector<char>(W));
  seen = vector<vector<bool>>(H, vector<bool>(W, false));
  warp = vector<vector<int>>(H, vector<int>(W, -1));
  cget = vector<vector<int>>(0);
  note = vector<vector<bool>>(H, vector<bool>(W, false));

  vector<int> start(2);
  vector<int> goal(2);

  cin >> start[0] >> start[1];
  cin >> goal[0] >> goal[1];

  start[0]--;
  start[1]--;
  goal[0]--;
  goal[1]--;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> meiro[i][j];
  }

  cget.push_back(start);
  int n_warp = -1;

  while (true) {
    n_warp++;
    vector<vector<int>> tmp_cget = cget;
    cget = vector<vector<int>>(0);
    note = vector<vector<bool>>(H, vector<bool>(W, false));
    int cget_count = 0;

    for (int i = 0; i < tmp_cget.size(); i++) {
      vector<int> kouho = tmp_cget[i];
      if (!seen[kouho[0]][kouho[1]]) {
        queue<pair<int, int>> que;
        walk(kouho[0], kouho[1], n_warp);
      }
      cget_count++;
    }

    if (cget_count == 0)
      break;
  }
  /*
  for (int i=0; i<H; i++)
  {
      for (int j=0; j<W; j++)
      {
          cout << warp[i][j] << " ";
      }
      cout << endl;
  }
  */
  cout << warp[goal[0]][goal[1]] << endl;
}