Problem p00247 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int W, H, SX, SY, GX, GY, ice_N;
int field[14][14], ice[14][14], icelife[1000];
bool flag[14][14];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y);
bool iddfs(int x, int y, int c);
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++) {
        field[i][j] = 1;
        flag[i][j] = true;
      }
    }
    memset(ice, 0, sizeof(ice));
    memset(icelife, 0, sizeof(icelife));
    ice_N = 1;
    string s;
    int ans;
    for (int i = 1; i <= H; i++) {
      cin >> s;
      for (int j = 1; j <= W; j++) {
        if (s[j - 1] == '.') {
          field[j][i] = 0;
        }
        if (s[j - 1] == '#') {
          field[j][i] = 1;
        }
        if (s[j - 1] == 'X') {
          field[j][i] = 2;
        }
        if (s[j - 1] == 'S') {
          field[j][i] = 0;
          SX = j;
          SY = i;
        }
        if (s[j - 1] == 'G') {
          field[j][i] = 0;
          GX = j;
          GY = i;
        }
      }
    }
    for (int i = 1; i <= W; i++) {
      for (int j = 1; j <= H; j++) {
        if (field[i][j] == 2 && ice[i][j] == 0) {
          dfs(i, j);
          icelife[ice_N] /= 2;
          ice_N++;
        }
      }
    }
    for (ans = abs(SX - GX) + abs(SY - GY);; ans++) {
      if (iddfs(SX, SY, ans)) {
        cout << ans << endl;
        break;
      }
    }
  }
  return 0;
}
void dfs(int x, int y) {
  ice[x][y] = ice_N;
  icelife[ice_N]++;
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    if (field[p][q] == 2 && ice[p][q] == 0) {
      dfs(p, q);
    }
  }
}
bool iddfs(int x, int y, int c) {
  bool f = false;
  if (x == GX && y == GY) {
    return true;
  }
  if (c < abs(x - GX) + abs(y - GY)) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    if (flag[p][q]) {
      if (field[p][q] == 0) {
        flag[p][q] = false;
        if (iddfs(p, q, c - 1)) {
          f = true;
        }
        flag[p][q] = true;
      }
      if (field[p][q] == 2) {
        if (icelife[ice[p][q]] > 0) {
          flag[p][q] = false;
          icelife[ice[p][q]]--;
          if (iddfs(p, q, c - 1)) {
            f = true;
          }
          icelife[ice[p][q]]++;
          flag[p][q] = true;
        }
      }
    }
  }
  return f;
}

Fixed submission:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int W, H, SX, SY, GX, GY, ice_N;
int field[14][14], ice[14][14], icelife[1000];
bool flag[14][14];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y);
bool iddfs(int x, int y, int c);
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++) {
        field[i][j] = 1;
        flag[i][j] = true;
      }
    }
    memset(ice, 0, sizeof(ice));
    memset(icelife, 0, sizeof(icelife));
    ice_N = 1;
    string s;
    int ans;
    for (int i = 1; i <= H; i++) {
      cin >> s;
      for (int j = 1; j <= W; j++) {
        if (s[j - 1] == '.') {
          field[j][i] = 0;
        }
        if (s[j - 1] == '#') {
          field[j][i] = 1;
        }
        if (s[j - 1] == 'X') {
          field[j][i] = 2;
        }
        if (s[j - 1] == 'S') {
          field[j][i] = 0;
          SX = j;
          SY = i;
        }
        if (s[j - 1] == 'G') {
          field[j][i] = 0;
          GX = j;
          GY = i;
        }
      }
    }
    for (int i = 1; i <= W; i++) {
      for (int j = 1; j <= H; j++) {
        if (field[i][j] == 2 && ice[i][j] == 0) {
          dfs(i, j);
          icelife[ice_N] /= 2;
          ice_N++;
        }
      }
    }
    for (ans = abs(SX - GX) + abs(SY - GY);; ans++) {
      if (iddfs(SX, SY, ans)) {
        cout << ans << endl;
        break;
      }
    }
  }
  return 0;
}
void dfs(int x, int y) {
  ice[x][y] = ice_N;
  icelife[ice_N]++;
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    if (field[p][q] == 2 && ice[p][q] == 0) {
      dfs(p, q);
    }
  }
}
bool iddfs(int x, int y, int c) {
  bool f = false;
  if (x == GX && y == GY) {
    return true;
  }
  if (c < abs(x - GX) + abs(y - GY)) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    bool next = false;
    for (int j = 0; j < 4; j++) {
      int pp = p + dx[j], qq = q + dy[j];
      if (pp != x || qq != y) {
        if (!flag[pp][qq]) {
          next = true;
        }
      }
    }
    if (next) {
      continue;
    }
    if (flag[p][q]) {
      if (field[p][q] == 0) {
        flag[p][q] = false;
        if (iddfs(p, q, c - 1)) {
          f = true;
        }
        flag[p][q] = true;
      }
      if (field[p][q] == 2) {
        if (icelife[ice[p][q]] > 0) {
          flag[p][q] = false;
          icelife[ice[p][q]]--;
          if (iddfs(p, q, c - 1)) {
            f = true;
          }
          icelife[ice[p][q]]++;
          flag[p][q] = true;
        }
      }
    }
  }
  return f;
}