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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
  return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

const int mod = 1000000007;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
}

int w, h, sx, sy, gx, gy;
char c[16][16];
int f[16][16];
vector<int> limit;

void mark_ice(int _x, int _y) {
  int size = 0;
  int id = limit.size();

  queue<pint> q;
  q.push(pint(_x, _y));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();

    f[y][x] = id;
    ++size;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (valid_pos(nx, ny, w, h) && c[ny][nx] == 'X' && f[ny][nx] == -1)
        q.push(pint(nx, ny));
    }
  }

  limit.push_back((size + 2) / 2);
}

int res;
int walk[256];
bool visit[16][16];
int dir[16][16][4];
int gdis[16][16];

void dfs(int x, int y, int dis) {
  if (gdis[y][x] + dis >= res)
    return;
  else if (x == gx && y == gy) {
    res = dis;
    return;
  }

  for (int i = 0; i < 4; ++i) {
    int d = dir[y][x][i];
    int nx = x + dx[d], ny = y + dy[d];
    if (valid_pos(nx, ny, w, h) && !visit[ny][nx] && f[ny][nx] != -1) {
      int id = f[ny][nx];
      ++walk[id];
      if (walk[id] < limit[id]) {
        visit[ny][nx] = true;
        dfs(nx, ny, dis + 1);
        visit[ny][nx] = false;
      }
      --walk[id];
    }
  }
}
int dis_ignore_ice(int sx, int sy) {
  int d[16][16];
  CL(d, -1);
  queue<pint> q;
  d[sy][sx] = 0;
  q.push(pint(sx, sy));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();

    if (x == gx && y == gy)
      return d[y][x];

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (valid_pos(nx, ny, w, h) && c[ny][nx] != '#' && d[ny][nx] == -1) {
        d[ny][nx] = d[y][x] + 1;
        q.push(pint(nx, ny));
      }
    }
  }

  return 114514;
}
int main() {
  ios::sync_with_stdio(false);

  while (cin >> w >> h, w) {
    limit.clear();
    CL(visit, 0);
    CL(f, -1);

    for (int i = 0; i < h; ++i)
      cin >> c[i];

    limit.push_back(114514);
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (strchr("SG.", c[y][x]))
          f[y][x] = 0;

        if (c[y][x] == 'S')
          sx = x, sy = y;
        else if (c[y][x] == 'G')
          gx = x, gy = y;
        else if (c[y][x] == 'X' && f[y][x] == -1)
          mark_ice(x, y);
        else if (c[y][x] == '#')
          f[y][x] = -1;
      }
    }

    for (int y = 0; y < h; ++y)
      for (int x = 0; x < w; ++x)
        gdis[y][x] = dis_ignore_ice(x, y);
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        pint dd[4];
        for (int i = 0; i < 4; ++i) {
          int nx = x + dx[i], ny = y + dy[i];
          dd[i] = pint(valid_pos(nx, ny, w, h) ? gdis[ny][nx] : 114514, i);
        }
        sort(dd, dd + 4);
        for (int i = 0; i < 4; ++i)
          dir[y][x][i] = dd[i].second;
      }
    }

    res = 114514;
    visit[sy][sx] = true;
    dfs(sx, sy, 0);
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
  return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

const int mod = 1000000007;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
}

int w, h, sx, sy, gx, gy;
char c[16][16];
int f[16][16];
vector<int> limit;

void mark_ice(int _x, int _y) {
  int size = 0;
  int id = limit.size();

  queue<pint> q;
  q.push(pint(_x, _y));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();

    f[y][x] = id;
    ++size;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (valid_pos(nx, ny, w, h) && c[ny][nx] == 'X' && f[ny][nx] == -1)
        q.push(pint(nx, ny));
    }
  }

  limit.push_back((size + 2) / 2);
}

int res;
int walk[256];
bool visit[16][16];
int dir[16][16][4];
int gdir[16][16];
void dfs(int x, int y, int dis) {
  if (gdir[y][x] + dis >= res)
    return;
  else if (x == gx && y == gy) {
    res = dis;
    return;
  }

  for (int i = 0; i < 4; ++i) {
    int d = dir[y][x][i];
    int nx = x + dx[d], ny = y + dy[d];
    if (valid_pos(nx, ny, w, h) && !visit[ny][nx] && f[ny][nx] != -1) {
      int id = f[ny][nx];
      ++walk[id];
      if (walk[id] < limit[id]) {
        visit[ny][nx] = true;
        dfs(nx, ny, dis + 1);
        visit[ny][nx] = false;
      }
      --walk[id];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);

  while (cin >> w >> h, w) {
    limit.clear();
    CL(visit, 0);
    CL(f, -1);

    for (int i = 0; i < h; ++i)
      cin >> c[i];

    limit.push_back(114514);
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (strchr("SG.", c[y][x]))
          f[y][x] = 0;

        if (c[y][x] == 'S')
          sx = x, sy = y;
        else if (c[y][x] == 'G')
          gx = x, gy = y;
        else if (c[y][x] == 'X' && f[y][x] == -1)
          mark_ice(x, y);
        else if (c[y][x] == '#')
          f[y][x] = -1;

        pint dd[4];
        for (int i = 0; i < 4; ++i)
          dd[i] = pint(abs(x + dx[i] - gx) + abs(y + dy[i] - gy), i);
        sort(dd, dd + 4);
        for (int i = 0; i < 4; ++i)
          dir[y][x][i] = dd[i].second;
      }
    }
    for (int y = 0; y < h; ++y)
      for (int x = 0; x < w; ++x)
        gdir[y][x] = abs(x - gx) + abs(y - gy);

    res = 114514;
    visit[sy][sx] = true;
    dfs(sx, sy, 0);
    cout << res << endl;
  }
}