Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define MAXN 802
#define INF 0x3f3f3f3f

using namespace std;

struct mystruct {
  int x, y, doors, steps;
};

deque<mystruct> Q[MAXN * 2];

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

char s[MAXN][MAXN];

int n, m, k, d[MAXN][MAXN];

bool isEdge(mystruct node) {
  if (node.x == 1 || node.x == n || node.y == 1 || node.y == m)
    return 1;
  return 0;
}

int main() {
  // freopen("input", "r", stdin);
  // freopen("output", "w", stdout);

  int i, j;
  mystruct start, node;

  scanf("%d%d%d\n", &n, &m, &k);

  for (i = 1; i <= n; ++i)
    scanf("%s\n", s[i] + 1);

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (s[i][j] == 'S')
        start.x = i, start.y = j;

  if (isEdge(start)) {
    printf("0");
    return 0;
  }

  start.doors = 0;
  start.steps = 0;

  Q[1].push_back(start);

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      d[i][j] = INF;

  int limit = n + m;
  for (i = 1; i <= limit; ++i)
    while (!Q[i].empty()) {

      node = Q[i].front();
      Q[i].pop_front();

      if (d[node.x][node.y] < i)
        continue;

      d[node.x][node.y] = i;

      if (isEdge(node)) {
        printf("%d", i);
        return 0;
      }

      mystruct nxt;
      for (int j = 0; j < 4; ++j) {
        nxt.x = node.x + dx[j];
        nxt.y = node.y + dy[j];

        if (s[nxt.x][nxt.y] == '.') {
          if (node.steps == k) {
            nxt.doors = 0;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          } else {
            nxt.doors = node.doors;
            nxt.steps = node.steps + 1;
            Q[i].push_back(nxt);
          }
        } else {
          if (node.doors < k) {
            if (node.steps == k || i == 1) {
              nxt.doors = 0;
              nxt.steps = 1;
              Q[i + 1].push_back(nxt);
            } else {
              nxt.doors = node.doors + 1;
              nxt.steps = node.steps + 1;
              Q[i].push_back(nxt);
            }
          } else {
            nxt.doors = 1;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          }
        }
      }
    }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define MAXN 802
#define INF 0x3f3f3f3f

using namespace std;

struct mystruct {
  int x, y, doors, steps;
};

deque<mystruct> Q[MAXN * 2];

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

char s[MAXN][MAXN];

int n, m, k, d[MAXN][MAXN];

bool isEdge(mystruct node) {
  if (node.x == 1 || node.x == n || node.y == 1 || node.y == m)
    return 1;
  return 0;
}

int main() {
  // freopen("input", "r", stdin);
  // freopen("output", "w", stdout);

  int i, j;
  mystruct start, node;

  scanf("%d%d%d\n", &n, &m, &k);

  for (i = 1; i <= n; ++i)
    scanf("%s\n", s[i] + 1);

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (s[i][j] == 'S')
        start.x = i, start.y = j;

  if (isEdge(start)) {
    printf("0");
    return 0;
  }

  start.doors = 0;
  start.steps = 0;

  Q[1].push_back(start);

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      d[i][j] = INF;

  int limit = n + m;
  for (i = 1; i <= limit; ++i)
    while (!Q[i].empty()) {

      node = Q[i].front();
      Q[i].pop_front();

      if (d[node.x][node.y] <= i)
        continue;

      d[node.x][node.y] = i;

      if (isEdge(node)) {
        printf("%d", i);
        return 0;
      }

      mystruct nxt;
      for (int j = 0; j < 4; ++j) {
        nxt.x = node.x + dx[j];
        nxt.y = node.y + dy[j];

        if (s[nxt.x][nxt.y] == '.') {
          if (node.steps == k) {
            nxt.doors = 0;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          } else {
            nxt.doors = node.doors;
            nxt.steps = node.steps + 1;
            Q[i].push_back(nxt);
          }
        } else {
          if (node.doors < k) {
            if (node.steps == k || i == 1) {
              nxt.doors = 0;
              nxt.steps = 1;
              Q[i + 1].push_back(nxt);
            } else {
              nxt.doors = node.doors + 1;
              nxt.steps = node.steps + 1;
              Q[i].push_back(nxt);
            }
          } else {
            nxt.doors = 1;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          }
        }
      }
    }

  return 0;
}