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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

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

int inf = 10000000;
int main() {
  int w, h;
  while (cin >> w >> h && w != 0) {
    vector<vector<int>> p(h, vector<int>(w));
    pair<int, int> s, t;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> p[i][j];
        if (p[i][j] == 2) {
          s.first = i;
          s.second = j;
          p[i][j] = 0;
        } else if (p[i][j] == 3) {
          t.first = i;
          t.second = j;
        }
      }
    }
    int ans = inf;
    queue<pair<pair<int, int>, pair<int, vector<vector<int>>>>> q;
    q.push(make_pair(make_pair(s.first, s.second), make_pair(0, p)));
    while (!q.empty()) {
      auto xxx = q.front();
      q.pop();
      int xx = xxx.first.first;
      int yy = xxx.first.second;
      int z = xxx.second.first;
      if (z >= 10)
        continue;
      auto pq = xxx.second.second;
      for (int i = 0; i < 4; i++) {
        int x = xx;
        int y = yy;
        auto pp = pq;
        bool flag = 1;
        while (1) {
          if (x + dx[i] < 0 || x + dx[i] >= h)
            break;
          if (y + dy[i] < 0 || y + dy[i] >= w)
            break;
          if (flag) {
            if (pp[x + dx[i]][y + dy[i]] == 1) {
              break;
            } else {
              flag = 0;
            }
          }
          if (pp[x + dx[i]][y + dy[i]] == 0) {
            x += dx[i];
            y += dy[i];
            continue;
          } else if (pp[x + dx[i]][y + dy[i]] == 3) {
            ans = min(ans, z + 1);
            break;
          } else {
            pp[x + dx[i]][y + dy[i]] = 0;
            if (z + 1 >= 10)
              break;
            q.push(make_pair(make_pair(x, y), make_pair(z + 1, pp)));
            break;
          }
        }
        if (ans != inf)
          break;
      }
      if (ans != inf)
        break;
    }
    if (ans == inf) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

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

int inf = 10000000;
int main() {
  int w, h;
  while (cin >> w >> h && w != 0) {
    vector<vector<int>> p(h, vector<int>(w));
    pair<int, int> s, t;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> p[i][j];
        if (p[i][j] == 2) {
          s.first = i;
          s.second = j;
          p[i][j] = 0;
        } else if (p[i][j] == 3) {
          t.first = i;
          t.second = j;
        }
      }
    }
    int ans = inf;
    queue<pair<pair<int, int>, pair<int, vector<vector<int>>>>> q;
    q.push(make_pair(make_pair(s.first, s.second), make_pair(0, p)));
    while (!q.empty()) {
      auto xxx = q.front();
      q.pop();
      int xx = xxx.first.first;
      int yy = xxx.first.second;
      int z = xxx.second.first;
      if (z >= 10)
        continue;
      auto pq = xxx.second.second;
      for (int i = 0; i < 4; i++) {
        int x = xx;
        int y = yy;
        auto pp = pq;
        bool flag = 1;
        while (1) {
          if (x + dx[i] < 0 || x + dx[i] >= h)
            break;
          if (y + dy[i] < 0 || y + dy[i] >= w)
            break;
          if (flag) {
            if (pp[x + dx[i]][y + dy[i]] == 1) {
              break;
            } else {
              flag = 0;
            }
          }
          if (pp[x + dx[i]][y + dy[i]] == 0) {
            x += dx[i];
            y += dy[i];
            continue;
          } else if (pp[x + dx[i]][y + dy[i]] == 3) {
            ans = min(ans, z + 1);
            break;
          } else {
            pp[x + dx[i]][y + dy[i]] = 0;
            if (z + 1 >= 10)
              break;
            q.push(make_pair(make_pair(x, y), make_pair(z + 1, pp)));
            break;
          }
        }
      }
    }
    if (ans == inf) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}