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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define f first
#define s second

typedef pair<int, int> pii;
const int W = 1111;
const int INF = 1000000000;

int w, h;
char board[W][W];

bool input() {
  cin >> w >> h;
  if (w == 0)
    return false;
  rep(i, h) {
    string str;
    cin >> str;
    rep(j, w) { board[i][j] = str[j]; }
  }
  return true;
}

class P {
public:
  int x, y, num, val;
  P(int x, int y, int num, int val) : x(x), y(y), num(num), val(val) {}
};

void plusBoard() {
  rep(i, h) {
    rep(j, w) {
      char c = board[i][j];
      if ('1' <= c && c <= '5')
        board[i][j] = ((c + 4) - '1') % 5 + '1';
    }
  }
}

const int T = 6;
bool visit[W][W][T];
bool isnum(char c) { return '0' <= c && c <= '9'; }
int haba() {
  /*
  puts("");
  rep(i,h){
          rep(j,w)printf("%c",board[i][j]);
          puts("");
  }*/

  queue<P> que;
  rep(i, h) rep(j, w) if (board[i][j] == 'S') que.push(P(j, i, 1, 0));
  rep(i, h) rep(j, w) rep(k, T) visit[i][j][k] = 0;

  while (1) {
    if (que.empty())
      break;
    P u = que.front();
    que.pop();

    if (visit[u.y][u.x][u.num] == 1)
      continue;
    visit[u.y][u.x][u.num] = 1;

    if (isnum(board[u.y][u.x])) {
      if (u.num + 1 < board[u.y][u.x] - '0') {
      } else
        u.num = max(u.num, board[u.y][u.x] - '0');
    }

    if (board[u.y][u.x] == 'G' && u.num == 5) {
      return u.val;
    }

    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};

    rep(i, 4) {
      int nx = u.x + dx[i];
      int ny = u.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        continue;
      if (visit[ny][nx][u.num] == 1)
        continue;
      que.push(P(nx, ny, u.num, u.val + 1));
    }
  }
  return INF;
}

pii solve() {

  vector<pii> rec;
  rep(i, 5) {
    int a = haba();
    plusBoard();
    rec.push_back(pii(i + 1, a));
  }

  rep(i, rec.size()) {
    reps(j, i + 1, rec.size()) {
      if (rec[i].s > rec[j].s)
        swap(rec[i], rec[j]);
    }
  }

  /*
  rep(i,rec.size()){
          printf("ans = %d %d\\n",rec[i].f, rec[i].s);
  }*/

  if (rec[0].s == INF)
    return pii(-1, -1);
  return rec[0];
}

void csolve() {
  pii val = solve();

  if (val.f == -1)
    puts("NA");
  else
    printf("%d %d\\n", val.f, val.s);
}

int main() {
  while (input())
    csolve();
}

/*
9 1
S.243.45G
9 1
S.415.12G
6 6
S.1..4
3..5..
..4.1.
4....5
.2.32.
5.1..G
3 2
...
S.G
0 0


*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define f first
#define s second

typedef pair<int, int> pii;
const int W = 1111;
const int INF = 1000000000;

int w, h;
char board[W][W];

bool input() {
  cin >> w >> h;

  if (w == 0)
    return false;
  rep(i, h) {
    string str;
    cin >> str;
    rep(j, w) { board[i][j] = str[j]; }
  }
  return true;
}

class P {
public:
  int x, y, num, val;
  P(int x, int y, int num, int val) : x(x), y(y), num(num), val(val) {}
};

void plusBoard() {
  rep(i, h) {
    rep(j, w) {
      char c = board[i][j];
      if ('1' <= c && c <= '5')
        board[i][j] = ((c + 4) - '1') % 5 + '1';
    }
  }
}

const int T = 7;
bool visit[W][W][T];
bool isnum(char c) { return '0' <= c && c <= '9'; }
int haba() {
  /*
  puts("");
  rep(i,h){
          rep(j,w)printf("%c",board[i][j]);
          puts("");
  }*/

  queue<P> que;
  rep(i, h) rep(j, w) if (board[i][j] == 'S') que.push(P(j, i, 1, 0));
  rep(i, h + 1) rep(j, w + 1) rep(k, T) visit[i][j][k] = 0;

  while (1) {
    if (que.empty())
      break;
    P u = que.front();
    que.pop();

    if (visit[u.y][u.x][u.num] == 1)
      continue;
    visit[u.y][u.x][u.num] = 1;

    // printf("%d %d %d %d\\n",u.y,u.x,u.num,u.val);

    if (isnum(board[u.y][u.x])) {
      if (u.num + 1 < board[u.y][u.x] - '0') {
      } else
        u.num = max(u.num, board[u.y][u.x] - '0');
    }

    if (board[u.y][u.x] == 'G' && u.num == 5) {
      return u.val;
    }

    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};

    rep(i, 4) {
      int nx = u.x + dx[i];
      int ny = u.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        continue;
      que.push(P(nx, ny, u.num, u.val + 1));
    }
  }
  return INF;
}

pii solve() {

  vector<pii> rec;
  rep(i, 5) {
    int a = haba();
    plusBoard();
    rec.push_back(pii(i + 1, a));
  }

  rep(i, rec.size()) {
    rep(j, rec.size()) {
      if (rec[i].s < rec[j].s)
        swap(rec[i], rec[j]);
    }
  }

  /*
  rep(i,rec.size()){
          printf("ans = %d %d\\n",rec[i].f, rec[i].s);
  }*/

  if (rec[0].s == INF)
    return pii(-1, -1);
  return rec[0];
}

void csolve() {
  pii val = solve();

  if (val.f == -1)
    puts("NA");
  else
    printf("%d %d\\n", val.f, val.s);
}

int main() {
  while (input())
    csolve();
}

/*
9 1
S.243.45G
9 1
S.415.12G
0 0


*/