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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_W = 100;
const int MAX_H = 100;

const int dxs[] = {1, 0, -1, 0};
const int dys[] = {0, 1, 0, -1};

/* typedef */

/* global variables */

int w, h, n;
bool wls[MAX_H][MAX_W][4], vstd[MAX_H][MAX_W][4];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> w >> h >> n;
    if (w == 0)
      break;

    memset(wls, 0, sizeof(wls));
    memset(vstd, 0, sizeof(vstd));

    for (int i = 0; i < n; i++) {
      int x0, y0, x1, y1;
      cin >> x0 >> y0 >> x1 >> y1;

      if (y0 == y1) {
        if (x0 > x1)
          swap(x0, x1);
        for (int x = x0; x < x1; x++)
          wls[y0 - 1][x][1] = wls[y0][x][3] = true;
      } else {
        if (y0 > y1)
          swap(y0, y1);
        for (int y = y0; y < y1; y++)
          wls[y][x0 - 1][0] = wls[y][x0][2] = true;
      }
    }

    for (int x = 0; x < w; x++)
      wls[0][x][3] = wls[h - 1][x][1] = true;
    for (int y = 0; y < h; y++)
      wls[y][0][2] = wls[y][w - 1][0] = true;

    int x, y, x1, y1, gx, gy, left;
    cin >> x >> y >> x1 >> y1 >> gx >> gy;

    if (y == y1) {
      if (x > x1)
        swap(x, x1);
      if (y == 0)
        left = 2;
      else
        y--, left = 0;
    } else {
      if (y > y1)
        swap(y, y1);
      if (x == 0)
        left = 1;
      else
        x--, left = 3;
    }

    int d;
    for (d = 1; x != gx || y != gy; d++) {
      // printf("x=%d, y=%d, left=%d\\n", x, y, left);

      if (vstd[y][x][left]) {
        d = 0;
        break;
      }
      vstd[y][x][left] = true;

      int drc = left;
      for (int di = 0; di < 4; di++, drc = (drc + 3) % 4)
        if (!wls[y][x][drc]) {
          x += dxs[drc];
          y += dys[drc];
          left = (drc + 1) % 4;
          break;
        }
    }

    if (d <= 0)
      cout << "Impossible" << endl;
    else
      cout << d << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_W = 100;
const int MAX_H = 100;

const int dxs[] = {1, 0, -1, 0};
const int dys[] = {0, 1, 0, -1};

/* typedef */

/* global variables */

int w, h, n;
bool wls[MAX_H][MAX_W][4], vstd[MAX_H][MAX_W][4];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> w >> h >> n;
    if (w == 0)
      break;

    memset(wls, 0, sizeof(wls));
    memset(vstd, 0, sizeof(vstd));

    for (int i = 0; i < n; i++) {
      int x0, y0, x1, y1;
      cin >> x0 >> y0 >> x1 >> y1;

      if (y0 == y1) {
        if (x0 > x1)
          swap(x0, x1);
        for (int x = x0; x < x1; x++) {
          if (y0 > 0)
            wls[y0 - 1][x][1] = true;
          if (y0 < h)
            wls[y0][x][3] = true;
        }
      } else {
        if (y0 > y1)
          swap(y0, y1);
        for (int y = y0; y < y1; y++) {
          if (x0 > 0)
            wls[y][x0 - 1][0] = true;
          if (x0 < w)
            wls[y][x0][2] = true;
        }
      }
    }

    for (int x = 0; x < w; x++)
      wls[0][x][3] = wls[h - 1][x][1] = true;
    for (int y = 0; y < h; y++)
      wls[y][0][2] = wls[y][w - 1][0] = true;

    int x, y, x1, y1, gx, gy, left;
    cin >> x >> y >> x1 >> y1 >> gx >> gy;

    if (y == y1) {
      if (x > x1)
        swap(x, x1);
      if (y == 0)
        left = 2;
      else
        y--, left = 0;
    } else {
      if (y > y1)
        swap(y, y1);
      if (x == 0)
        left = 1;
      else
        x--, left = 3;
    }

    int d;
    for (d = 1; x != gx || y != gy; d++) {
      // printf("x=%d, y=%d, left=%d\\n", x, y, left);

      if (vstd[y][x][left]) {
        d = -1;
        break;
      }

      vstd[y][x][left] = true;

      int drc = (left + 3) % 4;
      if (wls[y][x][left]) {
        while (wls[y][x][drc])
          drc = (drc + 3) % 4, left = (drc + 1) % 4;
      } else {
        drc = left;
        left = (left + 1) % 4;
      }

      x += dxs[drc];
      y += dys[drc];
    }

    if (d < 0)
      cout << "Impossible" << endl;
    else
      cout << d << endl;
  }

  return 0;
}