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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int W, H;
string maps[1000];
int d[6][1000][1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> W >> H, W || H) {
    REP(i, H) cin >> maps[i];

    int sx, sy, gx, gy;
    REP(i, H) REP(j, W) {
      if (maps[i][j] == 'S')
        sx = j, sy = i;
      if (maps[i][j] == 'G')
        gx = j, gy = i;
    }

    pii ans(INF, -1);
    REP(s, 5) {
      REP(k, 6) REP(i, H) REP(j, W) d[k][i][j] = INF;

      queue<pip> que;
      que.push(pip(0, pii(sy, sx)));
      d[0][sy][sx] = 0;

      while (!que.empty()) {
        pip now = que.front();
        que.pop();
        int x = now.se.se, y = now.se.fi;
        int num = now.fi;

        if (d[num][y][x] + 1 >= ans.fi)
          break;

        char gl;
        if (num < 4)
          gl = (s + num + 1) % 5 + '1';
        else
          gl = 'G';

        int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
        REP(i, 4) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!(nx >= 0 && nx < W && ny >= 0 && ny < H))
            continue;
          if (maps[ny][nx] == gl &&
              chmin(d[num + 1][ny][nx], d[num][y][x] + 1)) {
            if (num < 4)
              que.push(pip(num + 1, pii(ny, nx)));
            else
              goto end;
          } else if (chmin(d[num][ny][nx], d[num][y][x] + 1))
            que.push(pip(num, pii(ny, nx)));
        }
      }
    end:
      chmin(ans, pii(d[5][gy][gx], s));
    }
    if (ans.se == -1)
      cout << "NA\\n";
    else
      cout << ans.se + 1 << " " << ans.fi << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int W, H;
string maps[1000];
int d[6][1000][1000];

int main() {
  while (cin >> W >> H, W || H) {
    REP(i, H) cin >> maps[i];

    int sx, sy, gx, gy;
    REP(i, H) REP(j, W) {
      if (maps[i][j] == 'S')
        sx = j, sy = i;
      if (maps[i][j] == 'G')
        gx = j, gy = i;
    }

    pii ans(INF, -1);
    REP(s, 5) {
      REP(k, 6) REP(i, H) REP(j, W) d[k][i][j] = INF;

      queue<pip> que;
      que.push(pip(0, pii(sy, sx)));
      d[0][sy][sx] = 0;

      while (!que.empty()) {
        pip now = que.front();
        que.pop();
        int x = now.se.se, y = now.se.fi;
        int num = now.fi;

        if (d[num][y][x] + 1 >= ans.fi)
          break;

        char gl;
        if (num < 4)
          gl = (s + num + 1) % 5 + '1';
        else
          gl = 'G';

        int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
        REP(i, 4) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!(nx >= 0 && nx < W && ny >= 0 && ny < H))
            continue;
          if (maps[ny][nx] == gl &&
              chmin(d[num + 1][ny][nx], d[num][y][x] + 1)) {
            if (num < 4)
              que.push(pip(num + 1, pii(ny, nx)));
            else
              goto end;
          } else if (chmin(d[num][ny][nx], d[num][y][x] + 1))
            que.push(pip(num, pii(ny, nx)));
        }
      }
    end:
      chmin(ans, pii(d[5][gy][gx], s));
    }
    if (ans.se == -1)
      puts("NA");
    else
      printf("%d %d\\n", ans.se + 1, ans.fi);
  }
  return 0;
}