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

#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int W, H, N;
typedef pair<int, int> P;
queue<P> que;
char maze[1003][1003];
int timer[1003][1003];
int pos_x, pos_y;
int sh_x[4] = {0, 0, -1, 1};
int sh_y[4] = {1, -1, 0, 0};
int res = 0;
int strength = 1;

void solve() {
  que.push(P(pos_x, pos_y));

  while (que.size()) {
    P pos = que.front();
    que.pop();
    if (maze[pos.second][pos.first] - '0' == strength) {
      // printf("[%d] [%d]\\n", pos.second, pos.first);
      while (que.size())
        que.pop();
      strength++;
      if (strength == N + 1) {
        res = timer[pos.second][pos.first];
        break;
      }

      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (maze[i][j] == '#')
            maze[i][j] = '.';
        }
      }

      maze[pos.second][pos.first] = '#';
      que.push(pos);
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int new_x = pos.first + sh_x[i];
      int new_y = pos.second + sh_y[i];
      if (new_x >= 0 && new_x < W && new_y >= 0 && new_y < H &&
          maze[new_y][new_x] != '#' && maze[new_y][new_x] != 'X') {
        // printf("Go to [%d] [%d]\\n", new_y, new_x);
        que.push(P(new_x, new_y));
        if (maze[new_y][new_x] == '.') {
          maze[new_y][new_x] = '#';
        }
        timer[new_y][new_x] = timer[pos.second][pos.first] + 1;
      }
    }
  }
}

int main() {
  scanf("%d %d %d", &H, &W, &N);
  getchar();

  for (int i = 0; i < H; i++) {
    fgets(maze[i], 1002, stdin);
    maze[i][W] = '#';
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == 'S') {
        pos_x = j;
        pos_y = i;
      }
      if (i == H - 1) {
        maze[H][j] = '#';
      }
    }
  }

  solve();
  printf("%d\\n", res);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int W, H, N;
typedef pair<int, int> P;
queue<P> que;
char maze[1003][1003];
int d[1003][1003];
int pos_x, pos_y;
int sh_x[4] = {0, 0, -1, 1};
int sh_y[4] = {1, -1, 0, 0};
int res = 0;
int strength = 1;

void clear(queue<P> &q) {
  queue<P> empty;
  swap(q, empty);
}

void solve() {
  que.push(P(pos_x, pos_y));
  memset(d, -1, sizeof(d));
  d[pos_y][pos_x] = 0;

  while (que.size()) {
    P pos = que.front();
    que.pop();
    if (maze[pos.second][pos.first] - '0' == strength) {
      // printf("[%d] [%d] %d\\n", pos.second, pos.first, res);
      clear(que);
      res += d[pos.second][pos.first];

      strength++;
      if (strength == N + 1) {
        break;
      }

      // for (int i=0;i<H;i++) {
      //   for (int j=0;j<W;j++) {
      //     if (maze[i][j] == '#') maze[i][j] = '.';
      //   }
      // }
      memset(d, -1, sizeof(d));
      d[pos.second][pos.first] = 0;

      // while(que.size()) que.pop();
      // maze[pos.second][pos.first] = '#';
      que.push(pos);
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int new_x = pos.first + sh_x[i];
      int new_y = pos.second + sh_y[i];
      if (new_x >= 0 && new_x < W && new_y >= 0 && new_y < H &&
          maze[new_y][new_x] != 'X' && d[new_y][new_x] == -1) {
        // printf("Go to [%d] [%d]\\n", new_y, new_x);
        que.push(P(new_x, new_y));
        // if (maze[new_y][new_x] == '.') {
        //   maze[new_y][new_x] = '#';
        // }
        d[new_y][new_x] = d[pos.second][pos.first] + 1;
      }
    }
  }
}

int main() {
  while (1) {
    scanf("%d %d %d", &H, &W, &N);
    getchar();
    if (!W && !H) {
      break;
    }

    for (int i = 0; i < H; i++) {
      fgets(maze[i], 1002, stdin);
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (maze[i][j] == 'S') {
          pos_x = j;
          pos_y = i;
        }
      }
    }

    solve();
    printf("%d\\n", res);
    strength = 1;
    res = 0;
  }
  return 0;
}