Problem p00632 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>

const size_t di[] = {size_t(-1), 0, 1, 0};
const size_t dj[] = {0, size_t(-1), 0, 1};
const int INF = 1e9;
const size_t dti[] = {0, 0, 1, 0, 0, 0, 0, 0, size_t(-1)};
const size_t dtj[] = {0, 0, 0, 0, size_t(-1), 0, 1, 0, 0};

int testcase_ends() {
  size_t H, W;
  scanf("%zu %zu", &H, &W);

  if (H == 0 && W == 0)
    return 1;

  std::vector<std::string> s(H);
  size_t si = -1, sj = -1;
  size_t ti = -1, tj = -1;
  for (size_t i = 0; i < H; ++i) {
    char buf[128];
    scanf("%s", buf);
    s[i] = buf;
    for (size_t j = 0; j < W; ++j) {
      if (s[i][j] == 'A') {
        si = i;
        sj = j;
      } else if (s[i][j] == 'B') {
        ti = i;
        tj = j;
      }
    }
  }

  char buf[128];
  scanf("%s", buf);
  std::string t = buf;

  std::queue<std::pair<size_t, size_t>> q;
  q.emplace(si, sj);
  std::vector<std::vector<int>> dp(H, std::vector<int>(W, INF));
  dp[si][sj] = 0;
  while (!q.empty()) {
    std::pair<size_t, size_t> p = q.front();
    q.pop();
    size_t i = p.first, j = p.second;
    for (size_t k = 0; k < 4; ++k) {
      size_t ni = i + di[k], nj = j + dj[k];
      if (!(ni < H && nj < W))
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dp[ni][nj] < dp[i][j] + 1)
        continue;
      dp[ni][nj] = dp[i][j] + 1;
      q.emplace(ni, nj);
    }
  }

  // for (size_t i=0; i<H; ++i)
  //   for (size_t j=0; j<W; ++j)
  //     if (dp[i][j] < INF)
  //       printf("%d%c", dp[i][j], j+1<W? ' ':'\n');
  //     else
  //       printf("#%c", j+1<W? ' ':'\n');

  size_t tlen = t.length();
  std::pair<size_t, size_t> res(-1, -1);
  int tt = INF;
  for (int i = 0; i < 1000; ++i) {
    size_t nti = ti + dti[t[i % tlen] - '0'];
    size_t ntj = tj + dtj[t[i % tlen] - '0'];
    if (nti < H)
      ti = nti;
    if (ntj < W)
      tj = ntj;
    if (dp[ti][tj] < INF) {
      if (dp[ti][tj] > i + 1)
        continue;
      if (i + 1 < tt) {
        tt = i + 1;
        res = {ti, tj};
      }
    }
  }
  if (tt < INF)
    return !printf("%d %zu %zu\n", tt, res.first, res.second);
  printf("impossible\n");
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>

const size_t di[] = {size_t(-1), 0, 1, 0};
const size_t dj[] = {0, size_t(-1), 0, 1};
const int INF = 1e9;
const size_t dti[] = {0, 0, 1, 0, 0, 0, 0, 0, size_t(-1)};
const size_t dtj[] = {0, 0, 0, 0, size_t(-1), 0, 1, 0, 0};

int testcase_ends() {
  size_t H, W;
  scanf("%zu %zu", &H, &W);

  if (H == 0 && W == 0)
    return 1;

  std::vector<std::string> s(H);
  size_t si = -1, sj = -1;
  size_t ti = -1, tj = -1;
  for (size_t i = 0; i < H; ++i) {
    char buf[128];
    scanf("%s", buf);
    s[i] = buf;
    for (size_t j = 0; j < W; ++j) {
      if (s[i][j] == 'A') {
        si = i;
        sj = j;
      } else if (s[i][j] == 'B') {
        ti = i;
        tj = j;
      }
    }
  }

  char buf[128];
  scanf("%s", buf);
  std::string t = buf;

  std::queue<std::pair<size_t, size_t>> q;
  q.emplace(si, sj);
  std::vector<std::vector<int>> dp(H, std::vector<int>(W, INF));
  dp[si][sj] = 0;
  while (!q.empty()) {
    std::pair<size_t, size_t> p = q.front();
    q.pop();
    size_t i = p.first, j = p.second;
    for (size_t k = 0; k < 4; ++k) {
      size_t ni = i + di[k], nj = j + dj[k];
      if (!(ni < H && nj < W))
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dp[ni][nj] <= dp[i][j] + 1)
        continue;
      dp[ni][nj] = dp[i][j] + 1;
      q.emplace(ni, nj);
    }
  }

  // for (size_t i=0; i<H; ++i)
  //   for (size_t j=0; j<W; ++j)
  //     if (dp[i][j] < INF)
  //       printf("%d%c", dp[i][j], j+1<W? ' ':'\n');
  //     else
  //       printf("#%c", j+1<W? ' ':'\n');

  size_t tlen = t.length();
  std::pair<size_t, size_t> res(-1, -1);
  int tt = INF;
  for (int i = 0; i < 1000; ++i) {
    size_t nti = ti + dti[t[i % tlen] - '0'];
    size_t ntj = tj + dtj[t[i % tlen] - '0'];
    if (nti < H)
      ti = nti;
    if (ntj < W)
      tj = ntj;
    if (dp[ti][tj] < INF) {
      if (dp[ti][tj] > i + 1)
        continue;
      if (i + 1 < tt) {
        tt = i + 1;
        res = {ti, tj};
      }
    }
  }
  if (tt < INF)
    return !printf("%d %zu %zu\n", tt, res.first, res.second);
  printf("impossible\n");
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}