Problem p00481 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

#define INF 1000000

int H, W, N;
int sx, sy;
int res = 0;
int d[1100][1100];
char maze[1100][1100];
pair<int, int> cheese[10];

void bfs(int HP) {
  queue<pair<int, int>> que;

  sx = cheese[HP - 1].first, sy = cheese[HP - 1].second;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      d[i][j] = INF;
    }
  }
  que.push(pair<int, int>(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if ((maze[p.first][p.second] - '0') == HP)
      break;
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy) {
        if (abs(dx - dy) != 1)
          continue;
        int nx = p.first + dx, ny = p.second + dy;
        if (0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
  }

  if (HP == N) {
    res += d[cheese[HP].first][cheese[HP].second];
    return;
  }

  res += d[cheese[HP].first][cheese[HP].second];
  bfs(HP + 1);
  return;
}

int main(void) {
  cin >> H >> W >> N;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> maze[i][j];
      if (maze[i][j] - '0' >= 1 && maze[i][j] - '0' <= N) {
        cheese[maze[i][j] - '0'] = pair<int, int>(i, j);
      } else if (maze[i][j] == 'S') {
        cheese[0] = pair<int, int>(i, j);
      }
    }
  }

  bfs(1);
  int r = res;
  cout << r << endl;
  while (1)
    ;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

#define INF 1000000

int H, W, N;
int sx, sy;
int res = 0;
int d[1100][1100];
char maze[1100][1100];
pair<int, int> cheese[10];

void bfs(int HP) {
  queue<pair<int, int>> que;

  sx = cheese[HP - 1].first, sy = cheese[HP - 1].second;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      d[i][j] = INF;
    }
  }
  que.push(pair<int, int>(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if ((maze[p.first][p.second] - '0') == HP)
      break;
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy) {
        if (abs(dx - dy) != 1)
          continue;
        int nx = p.first + dx, ny = p.second + dy;
        if (0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
  }

  if (HP == N) {
    res += d[cheese[HP].first][cheese[HP].second];
    return;
  }

  res += d[cheese[HP].first][cheese[HP].second];
  bfs(HP + 1);
  return;
}

int main(void) {
  cin >> H >> W >> N;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> maze[i][j];
      if (maze[i][j] - '0' >= 1 && maze[i][j] - '0' <= N) {
        cheese[maze[i][j] - '0'] = pair<int, int>(i, j);
      } else if (maze[i][j] == 'S') {
        cheese[0] = pair<int, int>(i, j);
      }
    }
  }

  bfs(1);
  int r = res;
  cout << r << endl;
  return 0;
}