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

#include <cstdio>
#include <cstdlib>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

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

int h, w;
char B[12][13], gy, gx;

bool vis[12][12];

int ice[12][12], ice_sz[144];
void predfs(int i, int j, int id) {
  vis[i][j] = true;
  ice[i][j] = id;
  ice_sz[id]++;
  rep(k, 4) {
    int y = i + dy[k], x = j + dx[k];
    if (0 <= y && y < h && 0 <= x && x < w && B[y][x] == 'X' && !vis[y][x])
      predfs(y, x, id);
  }
}

int ans;
int step[144]; // ツ氷ツづーツ督・ツづアツつセツ嘉アツ青?
void dfs(int i, int j, int now) {
  if (B[i][j] == 'X')
    if (ice_sz[ice[i][j]] / 2 < step[ice[i][j]] + 1)
      return;

  if (ans <= now + abs(i - gy) + abs(j - gx))
    return; // Manhattan ツ仰猟猟」ツづ?A*
  if (B[i][j] == 'G') {
    ans = now;
    return;
  }

  vis[i][j] = true;
  if (B[i][j] == 'X')
    step[ice[i][j]]++;

  rep(k, 4) {
    int y = i + dy[k], x = j + dx[k];
    if (0 <= y && y < h && 0 <= x && x < w && B[y][x] != '#' && !vis[y][x]) {
      // ツつアツづェツづ慊づ?づ可津環づ?つスツ督ケツつゥツづァ (y,x)
      // ツづ可シツδ?ーツトツカツッツトツつオツづ?猟按づェツづゥツ湘ェツ債?づ債枝ツ環?づィ
      bool ok = true;
      rep(l, 4) {
        int yy = y + dy[l], xx = x + dx[l];
        if ((yy != i || xx != j) && 0 <= yy && yy < h && 0 <= xx && xx < w &&
            vis[yy][xx])
          ok = false;
      }
      if (ok)
        dfs(y, x, now + 1);
    }
  }

  vis[i][j] = false;
  if (B[i][j] == 'X')
    step[ice[i][j]]--;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    rep(i, h) scanf("%s", B[i]);

    rep(i, h) rep(j, w) vis[i][j] = false;
    int ice_id = 0;
    rep(i, h) rep(j, w) if (B[i][j] == 'X' && !vis[i][j]) {
      ice_sz[ice_id] = 0;
      predfs(i, j, ice_id++);
    }

    int sx, sy;
    rep(i, h) rep(j, w) {
      if (B[i][j] == 'S')
        sx = j, sy = i;
      if (B[i][j] == 'G')
        gx = j, gy = i;
    }

    rep(i, h) rep(j, w) vis[i][j] = false;
    ans = h * w;
    dfs(sy, sx, 0);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

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

int h, w;
char B[12][13], gy, gx;

bool vis[12][12];

int ice[12][12], ice_sz[144];
void predfs(int i, int j, int id) {
  vis[i][j] = true;
  ice[i][j] = id;
  ice_sz[id]++;
  rep(k, 4) {
    int y = i + dy[k], x = j + dx[k];
    if (0 <= y && y < h && 0 <= x && x < w && B[y][x] == 'X' && !vis[y][x])
      predfs(y, x, id);
  }
}

int ans;
int step[144]; // ツ氷ツづーツ督・ツづアツつセツ嘉アツ青?
void dfs(int i, int j, int now) {
  if (B[i][j] == 'X')
    if (ice_sz[ice[i][j]] / 2 < step[ice[i][j]] + 1)
      return;

  if (ans <= now + abs(i - gy) + abs(j - gx))
    return; // Manhattan ツ仰猟猟」ツづ?A*
  if (B[i][j] == 'G') {
    ans = now;
    return;
  }

  if (B[i][j] == 'X')
    step[ice[i][j]]++;

  vis[i][j] = true;
  rep(k, 4) {
    int y = i + dy[k], x = j + dx[k];
    if (0 <= y && y < h && 0 <= x && x < w && B[y][x] != '#' && !vis[y][x]) {
      // ツつアツづェツづ慊づ?づ可津環づ?つスツ督ケツつゥツづァ (y,x)
      // ツづ可シツδ?ーツトツカツッツトツつオツづ?猟按づェツづゥツ湘ェツ債?づ債枝ツ環?づィ
      bool ok = true;
      rep(l, 4) {
        int yy = y + dy[l], xx = x + dx[l];
        if ((yy != i || xx != j) && 0 <= yy && yy < h && 0 <= xx && xx < w &&
            vis[yy][xx])
          ok = false;
      }
      if (ok)
        dfs(y, x, now + 1);
    }
  }
  vis[i][j] = false;

  if (B[i][j] == 'X')
    step[ice[i][j]]--;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    rep(i, h) scanf("%s", B[i]);

    rep(i, h) rep(j, w) vis[i][j] = false;
    int ice_id = 0;
    rep(i, h) rep(j, w) if (B[i][j] == 'X' && !vis[i][j]) {
      ice_sz[ice_id] = 0;
      predfs(i, j, ice_id++);
    }

    int sx, sy;
    rep(i, h) rep(j, w) {
      if (B[i][j] == 'S')
        sx = j, sy = i;
      if (B[i][j] == 'G')
        gx = j, gy = i;
    }

    rep(i, h) rep(j, w) vis[i][j] = false;
    ans = h * w;
    dfs(sy, sx, 0);
    printf("%d\\n", ans);
  }

  return 0;
}