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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;
const double EPS = 1e-10;

char field[15][15];
bool visit[15][15];
int cnt[100];
int sz[100];
int id[15][15];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int nextlimit;

void dfs(int r, int c, int k) {
  id[r][c] = k;
  sz[k]++;
  rep(i, 4) {
    int r2 = r + dy[i];
    int c2 = c + dx[i];
    if (id[r2][c2] == -1 && field[r2][c2] == 'X') {
      dfs(r2, c2, k);
    }
  }
}

bool iddfs(int r, int c, int gr, int gc, int dep, int limit) {
  if (dep + abs(gr - r) + abs(gc - c) > limit) {
    nextlimit = min(dep + abs(gr - r) + abs(gc - c), nextlimit);
    return false;
  }
  if (r == gr && c == gc)
    return true;
  visit[r][c] = true;

  rep(i, 4) {

    int r2 = r + dy[i];
    int c2 = c + dx[i];

    if (field[r2][c2] == '#' || visit[r2][c2])
      continue;
    if (field[r2][c2] == '.') {
      if (iddfs(r2, c2, gr, gc, dep + 1, limit))
        return true;
    }
    if (field[r2][c2] == 'X') {
      if (cnt[id[r2][c2]] + 1 > sz[id[r2][c2]] / 2)
        continue;
      cnt[id[r2][c2]]++;
      if (iddfs(r2, c2, gr, gc, dep + 1, limit))
        return true;
      cnt[id[r2][c2]]--;
    }
  }
  visit[r][c] = false;
  return false;
}

int main() {
  int x, y, sr, sc, gr, gc, c;
  while (cin >> x >> y) {
    c = 0;
    if (x == 0 && y == 0)
      continue;
    ;
    fill(&field[0][0], &field[0][0] + 15 * 15, '#');

    memset(sz, 0, sizeof(sz));
    memset(cnt, 0, sizeof(cnt));
    memset(id, -1, sizeof(id));
    memset(visit, false, sizeof(visit));

    rep(i, y) rep(j, x) {
      cin >> field[i + 1][j + 1];
      if (field[i + 1][j + 1] == 'S')
        sr = i + 1, sc = j + 1;
      if (field[i + 1][j + 1] == 'G')
        gr = i + 1, gc = j + 1, field[i + 1][j + 1] = '.';
    }

    rep(i, y) rep(j, x) {
      if (id[i + 1][j + 1] == -1 && field[i + 1][j + 1] == 'X') {
        dfs(i + 1, j + 1, c++);
      }
      // cout << setw(3) << id[i+1][j+1];
      //       if(j == x - 1) cout << endl;
    }

    int len = 1;
    nextlimit = INF;
    while (!iddfs(sr, sc, gr, gc, 0, len)) {
      len = nextlimit;
      nextlimit = INF;
    }
    cout << len << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;
const double EPS = 1e-10;

char field[15][15];
bool visit[15][15];
int best;
int cnt[100];
int sz[100];
int id[15][15];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

void dfs(int r, int c, int k) {
  id[r][c] = k;
  sz[k]++;
  rep(i, 4) {
    int r2 = r + dy[i];
    int c2 = c + dx[i];
    if (id[r2][c2] == -1 && field[r2][c2] == 'X') {
      dfs(r2, c2, k);
    }
  }
}

void iddfs(int r, int c, int gr, int gc, int dep, int limit) {
  //  if(dep + abs(gr - r) + abs(gc - c) > limit) return false;
  //  cout << r << " " << c << endl;
  // if(r == gr && c == gc) return true;
  if (dep + abs(gr - r) + abs(gc - c) > best)
    return;
  if (r == gr && c == gc) {
    best = dep;
    return;
  }
  visit[r][c] = true;

  rep(i, 4) {

    int r2 = r + dy[i];
    int c2 = c + dx[i];

    if (field[r2][c2] == '#' || visit[r2][c2])
      continue;
    if (field[r2][c2] == '.') {
      // if(iddfs(r2, c2, gr, gc, dep + 1, limit)) return true;
      iddfs(r2, c2, gr, gc, dep + 1, limit);
    }
    if (field[r2][c2] == 'X') {
      //      cout << cnt[id[r2][c2]] << " " << sz[id[r2][c2]] << "**" << endl;
      if (cnt[id[r2][c2]] + 1 > sz[id[r2][c2]] / 2)
        continue;
      cnt[id[r2][c2]]++;
      // if(iddfs(r2, c2, gr, gc, dep + 1, limit)) return true;
      iddfs(r2, c2, gr, gc, dep + 1, limit);
      cnt[id[r2][c2]]--;
    }
  }
  visit[r][c] = false;
  //  return false;
}

int main() {
  int x, y, sr, sc, gr, gc, c;
  while (cin >> x >> y) {
    c = 0;
    if (x == 0 && y == 0)
      continue;
    ;
    fill(&field[0][0], &field[0][0] + 15 * 15, '#');

    memset(sz, 0, sizeof(sz));
    memset(cnt, 0, sizeof(cnt));
    memset(id, -1, sizeof(id));
    memset(visit, false, sizeof(visit));

    rep(i, y) rep(j, x) {
      cin >> field[i + 1][j + 1];
      if (field[i + 1][j + 1] == 'S')
        sr = i + 1, sc = j + 1;
      if (field[i + 1][j + 1] == 'G')
        gr = i + 1, gc = j + 1, field[i + 1][j + 1] = '.';
    }

    rep(i, y) rep(j, x) {
      if (id[i + 1][j + 1] == -1 && field[i + 1][j + 1] == 'X') {
        dfs(i + 1, j + 1, c++);
      }
      //      cout << setw(3) << id[i+1][j+1];
      //      if(j == x - 1) cout << endl;
    }

    best = INF;
    iddfs(sr, sc, gr, gc, 0, best);
    /*
      int len = 1;
    while(!iddfs(sr,sc,gr,gc,0,len)){
      len++;
      }

    cout << len << endl;
*/
    cout << best << endl;
  }
  return 0;
}