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

#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#define INF 1 << 15
using namespace std;
struct d {
  int x;
  int y;
  int visited[12][12];
  int ic[144];
  int cnt;
};
struct b {
  int x;
  int y;
};
int main() {
  int lx, ly, i, j, nc = 1, k, a, min = 144, gx, gy, ex, ey;
  int maps[12][12] = {0};
  int mx[4] = {-1, 0, 1, 0};
  int my[4] = {0, -1, 0, 1};
  int ig[144] = {0};
  int f[12][12];
  int mapc[13][13];
  char map[13][13];
  queue<b> q;
  b v, u;
  stack<d> s;
  d r, t;
  while (1) {
    cin >> lx >> ly;
    if (lx == 0 && ly == 0) {
      break;
    }
    memset(mapc, -1, sizeof(mapc));
    nc = 1;
    memset(maps, 0, sizeof(maps));
    memset(ig, 0, sizeof(ig));
    min = 144;
    for (i = 0; i < ly; i++) {
      cin >> map[i];
    }
    for (i = 0; i < ly; i++) {
      for (j = 0; j < lx; j++) {
        if (map[i][j] == 'S') {
          r.x = j;
          r.y = i;
          memset(r.visited, 0, sizeof(r.visited));
          memset(r.ic, 0, sizeof(r.ic));
          r.cnt = 0;
          mapc[i][j] = 0;
          s.push(r);
        }
        if (map[i][j] == 'G') {
          gx = j;
          gy = i;
        }
        if (map[i][j] == 'X' && maps[i][j] == 0) {
          v.x = j;
          v.y = i;
          q.push(v);
          while (!q.empty()) {
            v = q.front();
            q.pop();
            maps[v.y][v.x] = nc;
            ig[nc]++;
            for (k = 0; k < 4; k++) {
              u = v;
              u.x += mx[k];
              u.y += my[k];
              if (map[u.y][u.x] == 'X' && maps[u.y][u.x] == 0 && u.x >= 0 &&
                  u.x < lx && u.y >= 0 && u.y < ly) {
                q.push(u);
              }
            }
          }
          if (ig[nc] == 1) {
            map[v.y][v.x] = '#';
          }
          nc++;
        }
      }
    }
    v.x = gx;
    v.y = gy;

    memset(f, -1, sizeof(f));
    f[gy][gx] = 0;
    q.push(v);
    while (!q.empty()) {
      v = q.front();
      q.pop();
      for (i = 0; i < 4; i++) {
        u = v;
        u.x += mx[i];
        u.y += my[i];
        if (map[u.y][u.x] != '#' && u.x >= 0 && u.x < lx && u.y >= 0 &&
            u.y < ly && f[u.y][u.x] == -1) {
          q.push(u);
          f[u.y][u.x] = f[v.y][v.x] + 1;
        }
      }
    }

    while (!s.empty()) {
      r = s.top();
      s.pop();
      if (r.visited[r.y][r.x] == 1) {
        continue;
      } else {
        r.visited[r.y][r.x] = 1;
      }
      if (r.cnt + f[r.y][r.x] >= min) {
        continue;
      }

      for (i = 0; i < 4; i++) {
        t = r;
        t.x += mx[i];
        t.y += my[i];
        t.cnt++;
        if (map[t.y][t.x] != '#' && t.x >= 0 && t.x < lx && t.y >= 0 &&
            t.y < ly) {

          if (mapc[t.y][t.x] >= r.cnt + 1 || mapc[t.y][t.x] == -1) {
            if (map[t.y][t.x] == 'G') {
              ex = t.x;
              ey = t.y;
              min = t.cnt;
              mapc[t.y][t.x] = r.cnt + 1;
              break;
            }
            if (map[t.y][t.x] == 'X') {
              a = maps[t.y][t.x];
              t.ic[a]++;
              if (t.ic[a] <= (ig[a] / 2)) {
                mapc[t.y][t.x] = r.cnt + 1;
                s.push(t);
              }
            }

            else {
              mapc[t.y][t.x] = r.cnt + 1;
              s.push(t);
            }
          } else {
            if (mapc[t.y][t.x] <= r.cnt) {
              continue;
            }
          }
        }
      }
    }
    cout << mapc[ey][ex] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct d {
  int x;
  int y;
  int visited[12][12];
  int ic[144];
  int cnt;
};
struct b {
  int x;
  int y;
};
int main() {
  int lx, ly, i, j, nc = 1, k, a, min = 144, gx, gy;
  int maps[12][12] = {0};
  int mx[4] = {-1, 0, 1, 0};
  int my[4] = {0, -1, 0, 1};
  int ig[144] = {0};
  int f[12][12];
  char map[13][13];
  queue<b> q;
  b v, u;
  stack<d> s;
  d r, t;
  while (1) {
    cin >> lx >> ly;
    if (lx == 0 && ly == 0) {
      break;
    }
    nc = 1;
    memset(maps, 0, sizeof(maps));
    memset(ig, 0, sizeof(ig));
    min = 144;
    for (i = 0; i < ly; i++) {
      cin >> map[i];
    }
    for (i = 0; i < ly; i++) {
      for (j = 0; j < lx; j++) {
        if (map[i][j] == 'S') {
          r.x = j;
          r.y = i;
          memset(r.visited, 0, sizeof(r.visited));
          memset(r.ic, 0, sizeof(r.ic));
          r.cnt = 0;
          s.push(r);
        }
        if (map[i][j] == 'G') {
          gx = j;
          gy = i;
        }
        if (map[i][j] == 'X' && maps[i][j] == 0) {
          v.x = j;
          v.y = i;
          q.push(v);
          while (!q.empty()) {
            v = q.front();
            q.pop();
            maps[v.y][v.x] = nc;
            ig[nc]++;
            for (k = 0; k < 4; k++) {
              u = v;
              u.x += mx[k];
              u.y += my[k];
              if (map[u.y][u.x] == 'X' && maps[u.y][u.x] == 0 && u.x >= 0 &&
                  u.x < lx && u.y >= 0 && u.y < ly) {
                q.push(u);
              }
            }
          }
          if (ig[nc] == 1) {
            map[v.y][v.x] = '#';
          }
          nc++;
        }
      }
    }
    v.x = gx;
    v.y = gy;

    memset(f, -1, sizeof(f));
    f[gy][gx] = 0;
    q.push(v);
    while (!q.empty()) {
      v = q.front();
      q.pop();
      for (i = 0; i < 4; i++) {
        u = v;
        u.x += mx[i];
        u.y += my[i];
        if (map[u.y][u.x] != '#' && u.x >= 0 && u.x < lx && u.y >= 0 &&
            u.y < ly && f[u.y][u.x] == -1) {
          q.push(u);
          f[u.y][u.x] = f[v.y][v.x] + 1;
        }
      }
    }

    while (!s.empty()) {
      r = s.top();
      s.pop();
      if (r.visited[r.y][r.x] == 1) {
        continue;
      } else {
        r.visited[r.y][r.x] = 1;
      }
      if (r.cnt + f[r.y][r.x] >= min) {
        continue;
      }
      /*
      if(map[r.y][r.x]=='X'){
              a=maps[r.y][r.x];
              r.ic[a]++;
              if(r.ic[a]>(ig[a]/2)){
                      continue;
              }
      }*/
      for (i = 0; i < 4; i++) {
        t = r;
        t.x += mx[i];
        t.y += my[i];
        t.cnt++;
        if (map[t.y][t.x] == 'G' && t.cnt < min && t.x >= 0 && t.x < lx &&
            t.y >= 0 && t.y < ly) {
          min = t.cnt;
          break;
        } else if (map[t.y][t.x] != '#' && t.x >= 0 && t.x < lx && t.y >= 0 &&
                   t.y < ly && r.visited[t.y][t.x] == 0) {
          if (map[t.y][t.x] == 'X') {
            a = maps[t.y][t.x];
            t.ic[a]++;
            if (t.ic[a] <= (ig[a] / 2)) {
              s.push(t);
            }
          } else {
            s.push(t);
          }
        }
      }
    }
    cout << min << endl;
  }
  return 0;
}