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

#include <cctype>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int DIRECTSIZE = 4;
struct _direct {
  int drow;
  int dcol;
} direct[DIRECTSIZE] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

const int N = 1000;
const int N2 = 10;
char grid[N][N], visited[N][N];
int h, w, n;
struct _factory {
  int row, col;
} factory[N2];

struct _node {
  int row, col, level;
};

int bfs(int n, char goal) {
  memset(visited, 0, sizeof(visited));

  _node start = {factory[n - 1].row, factory[n - 1].col, 0}, temp;

  visited[start.row][start.col] = 1;

  queue<_node> q;
  q.push(start);

  while (!q.empty()) {
    _node front = q.front();
    q.pop();

    for (int i = 0; i < DIRECTSIZE; i++) {
      int nextrow = front.row + direct[i].drow;
      int nextcol = front.col + direct[i].dcol;
      if (0 <= nextrow && nextrow < h && 0 <= nextcol && nextcol < w) {
        if (grid[nextrow][nextcol] == goal) {
          return front.level + 1;
        } else if (visited[nextrow][nextcol] == 0 &&
                   grid[nextrow][nextcol] != 'X') {
          visited[nextrow][nextcol] = 1;
          temp.row = nextrow;
          temp.col = nextcol;
          temp.level = front.level + 1;
          q.push(temp);
        }
      }
    }
  }

  return 0;
}

int main() {
  while (cin >> h >> w >> n) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> grid[i][j];

        if (grid[i][j] == 'S') {
          factory[0].row = i;
          factory[0].col = j;
        } else if (isdigit(grid[i][j])) {
          factory[grid[i][j] - '0'].row = i;
          factory[grid[i][j] - '0'].col = j;
        }
      }

    int minstep = 0;
    char goal = '1';
    for (int i = 1; i <= n; i++) {
      minstep += bfs(i, goal);
      goal++;
    }

    cout << minstep << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int h, w, n, flag[1005][1005];
char map[1005][1005];
struct point {
  int x;
  int y;
  int t;
} chess[15];
int bfs(struct point be, struct point end) {
  point temp, emp;
  queue<point> q;
  q.push(be);
  memset(flag, 0, sizeof(flag));
  while (!q.empty()) {
    temp = q.front();
    q.pop();
    flag[temp.x][temp.y] = 1;
    for (int i = -1; i < 2; i++)
      for (int j = -1; j < 2; j++)
        if (i != j && i != -j) {
          emp.x = temp.x + i;
          emp.y = temp.y + j;
          emp.t = temp.t + 1;
          if (emp.x < 0 || emp.x >= h || emp.y < 0 || emp.y >= w ||
              flag[emp.x][emp.y] == 1)
            continue;
          if (map[emp.x][emp.y] == 'X')
            continue;
          if (emp.x == end.x && emp.y == end.y)
            return emp.t;
          q.push(emp);
        }
  }
  return 0;
}
int main() {
  while (scanf("%d%d%d", &h, &w, &n)) {
    int sum = 0;
    for (int i = 0; i < h; i++)
      scanf("%s", map[i]);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int temp = map[i][j] - '0';
        if (map[i][j] == 'S') {
          chess[0].x = i;
          chess[0].y = j;
          chess[0].t = 0;
        } else if (temp > 0 && temp < 10) {
          chess[temp].x = i;
          chess[temp].y = j;
          chess[temp].t = 0;
        }
      }
    for (int i = 0; i < n; i++)
      sum = sum + bfs(chess[i], chess[i + 1]);
    printf("%d\\n", sum);
  }
  return 0;
}