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

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1 << 28;
const ll MOD = 1000000007LL;

char board[1010][1010];

int rec[1010][1010][5];
int H, W;

struct Elem {
  int x, y, bit;
};

signed main() {
  while (cin >> W >> H, W || H) {
    int sx, sy, gx, gy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> board[i][j];

        if (board[i][j] == 'S')
          sx = i, sy = j;
        if (board[i][j] == 'G')
          gx = i, gy = j;
        if ('0' <= board[i][j] and board[i][j] <= '9') {
          int c = board[i][j] - '0';
          board[i][j] = (char)(c - 1 + '0');
        }
      }
    }

    const pair<int, int> NIL(INF, INF);
    pair<int, int> ans = NIL;

    for (int p = 0; p < 5; p++) {
      fill(rec[0][0], rec[H][0], INF);
      rec[sx][sy][0] = 0;

      queue<Elem> que;
      que.push(Elem{sx, sy, 0});

      while (que.size()) {
        Elem cur = que.front();
        que.pop();
        int x = cur.x, y = cur.y, bit = cur.bit;
        if (x == gx and y == gy and bit == 4)
          break;

        for (int k = 0; k < 4; k++) {
          int nx = x + dx[k], ny = y + dy[k];
          if (nx < 0 || nx >= H || ny < 0 || ny >= W)
            continue;

          int nbit = bit;
          if ('0' <= board[nx][ny] and board[nx][ny] <= '9') {
            int c = (board[nx][ny] - '0' - p + 15) % 5;
            bool ok = (bit + 1 >= c);

            nbit = ok ? max(bit, c) : bit;
          }

          if (rec[nx][ny][nbit] > rec[x][y][bit] + 1) {
            rec[nx][ny][nbit] = rec[x][y][bit] + 1;
            que.push(Elem{nx, ny, nbit});
          }
        }
      }

      ans = min(ans, make_pair(rec[gx][gy][4], p + 1));
    }
    if (ans.first == INF)
      cout << "NA" << endl;
    else
      cout << ans.second << " " << ans.first << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1 << 28;
const ll MOD = 1000000007LL;

char board[1010][1010];

int rec[1010][1010][1 << 5];
int H, W;

struct Elem {
  int x, y, bit;
};

signed main() {
  while (cin >> W >> H, W || H) {
    int sx, sy, gx, gy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> board[i][j];

        if (board[i][j] == 'S')
          sx = i, sy = j;
        if (board[i][j] == 'G')
          gx = i, gy = j;
        if (isdigit(board[i][j])) {
          int c = board[i][j] - '0';
          board[i][j] = (char)(c - 1 + '0');
        }
      }
    }

    const pair<int, int> NIL(INF, INF);
    pair<int, int> ans = NIL;

    for (int x = 0; x < 5; x++) {
      fill(rec[0][0], rec[H][0], INF);
      rec[sx][sy][1 << x] = 0;

      queue<Elem> que;
      que.push(Elem{sx, sy, 1 << x});

      int fullbit = (1 << 5) - 1;
      while (que.size()) {
        Elem cur = que.front();
        que.pop();
        int x = cur.x, y = cur.y, bit = cur.bit;
        if (x == gx and y == gy and bit == fullbit)
          break;

        for (int k = 0; k < 4; k++) {
          int nx = x + dx[k], ny = y + dy[k];
          if (nx < 0 || nx >= H || ny < 0 || ny >= W)
            continue;

          int nbit = bit;
          if (isdigit(board[nx][ny])) {
            int c = (board[nx][ny] - '0');
            int pre = (c - 1 + 5) % 5;

            bool ok = false;
            ok |= (bit >> c & 1);
            ok |= (bit >> pre & 1);

            if (!ok)
              continue;
            nbit = bit | (1 << c);
          }

          if (rec[nx][ny][nbit] > rec[x][y][bit] + 1) {
            rec[nx][ny][nbit] = rec[x][y][bit] + 1;
            que.push(Elem{nx, ny, nbit});
          }
        }
      }

      ans = min(ans, make_pair(rec[gx][gy][fullbit], x + 1));
    }
    if (ans.first == INF)
      cout << "NA" << endl;
    else
      cout << ans.second << " " << ans.first << endl;
  }
  return 0;
}