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

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[1001][1001];
int v[1001][1001], n, h, w, sx, sy, ans;
int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
char b[1001];
int s[1001][1001];
struct st {
  int x, y;
} st1, st2, fl, pp;
int check(st kk) {
  if (kk.x >= 0 && kk.x < h && kk.y >= 0 && kk.y < w)
    return 1;
  else
    return 0;
}
int bfs(char c) {
  queue<st> qq;
  qq.push(pp);
  memset(v, 0, sizeof(v));
  memset(s, 0, sizeof(s));
  while (!qq.empty()) {
    st1 = qq.front();
    qq.pop();
    if (a[st1.x][st1.y] == c) {
      pp = st1;
      // printf("+%d %d+ %d\\n",st1.x,st1.y,s[pp.x][pp.y]);
      break;
    }
    for (int i = 0; i < 4; i++) {
      st2.x = st1.x + dir[i][0];
      st2.y = st1.y + dir[i][1];
      if (check(st2) && v[st2.x][st2.y] == 0 && a[st2.x][st2.y] != 'X') {
        v[st2.x][st2.y] = 1;
        s[st2.x][st2.y] = s[st1.x][st1.y] + 1;
        // printf("-%d %d %d-\\n",st2.x,st2.y,s[st2.x][st2.y]);
        qq.push(st2);
      }
    }
  }
  return s[pp.x][pp.y];
  // printf("(%d %d %d %c)\\n",st1.x,st1.y,s[st1.x][st1.y],c);
}
int main() {
  int i, j, k, m, l;
  while (~scanf("%d %d %d", &h, &w, &n)) {
    getchar();
    memset(v, 0, sizeof(v));
    memset(s, 0, sizeof(s));
    for (i = 0; i < h; i++) {
      scanf("%s", &a[i]);
    }
    for (i = 0; i < n; i++) {
      b[i] = i + 1 + '0';
      // printf("%c\\n",b[i]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (a[i][j] == 'S') {
          pp.x = i;
          pp.y = j;
        }
      }
    }
    // printf("%d %d\\n",sx,sy);
    int sum = 0;
    for (i = 0; i < n; i++) {
      sum += bfs(b[i]);
      // printf("[%d]\\n",sum);
    }
    printf("%d\\n", sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[1001][1001];
int v[1001][1001], n, h, w, sx, sy, ans, s;
int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
char b[1001];
int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w)
    return 1;
  return 0;
}
void dfs(int x, int y, char c, int cnt) {
  int t1, t2;
  if (cnt >= 1000)
    return;
  for (int i = 0; i < 4; i++) {
    // printf("%d\\n",cnt);
    t1 = x + dir[i][0];
    t2 = y + dir[i][1];
    if (check(t1, t2) && v[t1][t2] == 0 && a[t1][t2] != 'X') {

      v[t1][t2] = 1;
      dfs(t1, t2, c, cnt + 1);
      v[t1][t2] = 0;
    }
    if (a[t1][t2] == c && check(t1, t2)) {
      ans = min(ans, cnt + 1);
      sx = t1;
      sy = t2;
      // printf("%d, ",ans);
    }
  }
}
int main() {
  int i, j, k, m, l;
  while (~scanf("%d %d %d", &h, &w, &n)) {
    s = 0;
    getchar();
    memset(v, 0, sizeof(v));
    for (i = 0; i < h; i++) {
      scanf("%s", &a[i]);
    }
    for (i = 0; i < n; i++) {
      b[i] = i + 1 + '0';
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (a[i][j] == 'S') {
          sx = i;
          sy = j;
        }
      }
    }

    // printf("%d %d\\n",sx,sy);

    for (i = 0; i < n; i++) {
      ans = 100000;
      // printf("%d %d %c\\n",sx,sy,b[i]);

      dfs(sx, sy, b[i], 0);
      s += ans;
    }
    printf("%d\\n", s);
  }
  return 0;
}