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

#include <algorithm>
#include <stdio.h>
using namespace std;
char str[15][15];
pair<int, int> par[15][15];
int sz[15][15];
int now[15][15];
int v[15][15];
int tr, tc;
int H, W;
void dfs0(int a, int b) {
  par[a][b] = make_pair(tr, tc);
  sz[tr][tc]++;
  v[a][b] = 1;
  if (a && str[a - 1][b] == 'X' && !v[a - 1][b])
    dfs0(a - 1, b);
  if (b && str[a][b - 1] == 'X' && !v[a][b - 1])
    dfs0(a, b - 1);
  if (a < H - 1 && str[a + 1][b] == 'X' && !v[a + 1][b])
    dfs0(a + 1, b);
  if (b < W - 1 && str[a][b + 1] == 'X' && !v[a][b + 1])
    dfs0(a, b + 1);
}
int res;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int used[15][15];
int gr, gc;
int ABS(int a) { return max(a, -a); }
int dfs(int a, int b, int c) {
  if (str[a][b] == 'G') {
    return 1;
  }
  if (c == 0)
    return 0;
  if (ABS(gr - a) + ABS(gc - b) > c)
    return 0;
  for (int i = 0; i < 4; i++) {
    int nr = a + dx[i];
    int nc = b + dy[i];
    if (nr < 0 || nr >= H || nc < 0 || nc >= W)
      continue;
    if (used[nr][nc])
      continue;
    if (str[nr][nc] == '#')
      continue;
    if (nr && nc && used[nr - 1][nc - 1] && used[nr - 1][nc] &&
        used[nr][nc - 1])
      continue;
    if (nr && nc < W - 1 && used[nr - 1][nc] && used[nr - 1][nc + 1] &&
        used[nr][nc + 1])
      continue;
    if (nr < H - 1 && nc && used[nr][nc - 1] && used[nr + 1][nc] &&
        used[nr + 1][nc - 1])
      continue;
    if (nr < H - 1 && nc < W - 1 && used[nr][nc + 1] && used[nr + 1][nc] &&
        used[nr + 1][nc + 1])
      continue;
    if (str[nr][nc] == 'X') {
      int row = par[nr][nc].first;
      int col = par[nr][nc].second;
      if ((now[row][col] + 1) * 2 > sz[row][col])
        continue;
      now[row][col]++;
      used[nr][nc] = 1;
      if (dfs(nr, nc, c - 1))
        return 1;
      used[nr][nc] = 0;
      now[row][col]--;
    } else {
      used[nr][nc] = 1;
      if (dfs(nr, nc, c - 1))
        return 1;
      used[nr][nc] = 0;
    }
  }
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        v[i][j] = now[i][j] = sz[i][j] = used[i][j] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (str[i][j] == 'X' && !v[i][j]) {
          tr = i;
          tc = j;
          dfs0(i, j);
        }
      }
    res = 99999;
    //	int ret;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == 'G') {
          gr = i;
          gc = j;
        }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == 'S') {
          for (int k = 0;; k++) {
            // res=0;
            res = dfs(i, j, k);
            if (res) {
              printf("%d\\n", k);
              break;
            }
          }
        }
    //	printf("%d\\n",res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
char str[15][15];
pair<int, int> par[15][15];
int sz[15][15];
int now[15][15];
int v[15][15];
int tr, tc;
int H, W;
void dfs0(int a, int b) {
  par[a][b] = make_pair(tr, tc);
  sz[tr][tc]++;
  v[a][b] = 1;
  if (a && str[a - 1][b] == 'X' && !v[a - 1][b])
    dfs0(a - 1, b);
  if (b && str[a][b - 1] == 'X' && !v[a][b - 1])
    dfs0(a, b - 1);
  if (a < H - 1 && str[a + 1][b] == 'X' && !v[a + 1][b])
    dfs0(a + 1, b);
  if (b < W - 1 && str[a][b + 1] == 'X' && !v[a][b + 1])
    dfs0(a, b + 1);
}
int res;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int used[15][15];
int dfs(int a, int b, int c) {
  if (str[a][b] == 'G') {
    return 1;
  }
  if (c == 0)
    return 0;
  for (int i = 0; i < 4; i++) {
    if (a + dx[i] < 0 || a + dx[i] >= H || b + dy[i] < 0 || b + dy[i] >= W)
      continue;
    if (used[a + dx[i]][b + dy[i]])
      continue;
    if (str[a + dx[i]][b + dy[i]] == '#')
      continue;
    if (a + dx[i] && b + dy[i] && used[a + dx[i] - 1][b + dy[i] - 1] &&
        used[a + dx[i] - 1][b + dy[i]] && used[a + dx[i]][b + dy[i] - 1])
      continue;
    if (a + dx[i] && b + dy[i] < W - 1 && used[a + dx[i] - 1][b + dy[i]] &&
        used[a + dx[i] - 1][b + dy[i] + 1] && used[a + dx[i]][b + dy[i] + 1])
      continue;
    if (a + dx[i] < H - 1 && b + dy[i] && used[a + dx[i]][b + dy[i] - 1] &&
        used[a + dx[i] + 1][b + dy[i]] && used[a + dx[i] + 1][b + dy[i] - 1])
      continue;
    if (a + dx[i] < H - 1 && b + dy[i] < W - 1 &&
        used[a + dx[i]][b + dy[i] + 1] && used[a + dx[i] + 1][b + dy[i]] &&
        used[a + dx[i] + 1][b + dy[i] + 1])
      continue;
    if (str[a + dx[i]][b + dy[i]] == 'X') {
      int row = par[a + dx[i]][b + dy[i]].first;
      int col = par[a + dx[i]][b + dy[i]].second;
      if ((now[row][col] + 1) * 2 > sz[row][col])
        continue;
      now[row][col]++;
      used[a + dx[i]][b + dy[i]] = 1;
      if (dfs(a + dx[i], b + dy[i], c - 1))
        return 1;
      used[a + dx[i]][b + dy[i]] = 0;
      now[row][col]--;
    } else {
      used[a + dx[i]][b + dy[i]] = 1;
      if (dfs(a + dx[i], b + dy[i], c - 1))
        return 1;
      used[a + dx[i]][b + dy[i]] = 0;
    }
  }
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        v[i][j] = now[i][j] = sz[i][j] = used[i][j] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (str[i][j] == 'X' && !v[i][j]) {
          tr = i;
          tc = j;
          dfs0(i, j);
        }
      }
    res = 99999;
    //	int ret;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == 'S') {
          for (int k = 0;; k++) {
            // res=0;
            res = dfs(i, j, k);
            if (res) {
              printf("%d\\n", k);
              break;
            }
          }
        }
    //	printf("%d\\n",res);
  }
}