Problem p00725 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;

using namespace std;

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

const Int M_H(20);
const Int M_W(20);

short tile[M_H][M_W];
int H, W;

struct point {
  Int x, y, step;
  short tile[M_H][M_W];
  point(Int x_, Int y_, Int s, short tile_[M_H][M_W]) : x(x_), y(y_), step(s) {
    memcpy(tile, tile_, sizeof(tile));
  }
};

queue<point> que;

int solve() {
  while (!que.empty()) {
    auto now = que.front();
    que.pop();

    if (now.step >= 10)
      continue;

    int i(4);
    while (i--) {
      int x = now.x + dx[i];
      int y = now.y + dy[i];
      if (now.tile[y][x] == 1)
        continue;
      if (now.tile[y][x] == 4)
        continue;
      while (now.tile[y][x] == 0) {
        x += dx[i];
        y += dy[i];
        if (x >= M_W || x < 0 || y >= M_H || y < 0)
          break;
      }
      if (x >= M_W || x < 0 || y >= M_H || y < 0)
        continue;
      if (now.tile[y][x] == 1) {
        short temp[M_H][M_W];
        memcpy(temp, now.tile, sizeof(temp));
        temp[y][x] = 0;
        que.emplace(x - dx[i], y - dy[i], now.step + 1, temp);
      }
      if (now.tile[y][x] == 3)
        return now.step + 1;

      if (now.tile[y][x] == 4)
        continue;
    }
  }
  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // fstream fs("input.txt");
  ////////////ここから開始///////////
  // fs >> W >> H;

  while (cin >> W >> H, H) {
    REP(i, M_H) REP(j, M_W) { tile[i][j] = 4; }

    REP(i, H) REP(j, W) cin >> tile[i][j];

    que = {};

    REP(i, H) REP(j, W) {
      if (tile[i][j] == 2) {
        tile[i][j] = 0;
        que.emplace(j, i, 0, tile);
      }
    }
    cout << solve() << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;

using namespace std;

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

const Int M_H(20);
const Int M_W(20);

int tile[M_H][M_W];
int H, W;

struct point {
  Int x, y, step;
  int tile[M_H][M_W];
  point(Int x_, Int y_, Int s, int tile_[M_H][M_W]) : x(x_), y(y_), step(s) {
    memcpy(tile, tile_, sizeof(tile));
  }
};

queue<point> que;

int solve() {
  while (!que.empty()) {
    auto now = que.front();
    que.pop();

    if (now.step >= 10)
      continue;

    int i(4);
    while (i--) {
      int x = now.x + dx[i];
      int y = now.y + dy[i];
      if (now.tile[y][x] == 1)
        continue;
      if (now.tile[y][x] == 4)
        continue;
      while (now.tile[y][x] == 0) {
        x += dx[i];
        y += dy[i];
        if (x >= M_W || x < 0 || y >= M_H || y < 0)
          break;
      }
      if (x >= M_W || x < 0 || y >= M_H || y < 0)
        continue;
      if (now.tile[y][x] == 1) {
        int temp[M_H][M_W];
        memcpy(temp, now.tile, sizeof(temp));
        temp[y][x] = 0;
        que.emplace(x - dx[i], y - dy[i], now.step + 1, temp);
      }
      if (now.tile[y][x] == 3)
        return now.step + 1;

      if (now.tile[y][x] == 4)
        continue;
    }
  }
  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // fstream fs("input.txt");
  ////////////ここから開始///////////
  // fs >> W >> H;

  while (cin >> W >> H, H) {
    REP(i, M_H) REP(j, M_W) { tile[i][j] = 4; }

    REP(i, H) REP(j, W) cin >> tile[i][j];

    que = {};

    REP(i, H) REP(j, W) {
      if (tile[i][j] == 2) {
        tile[i][j] = 0;
        que.emplace(j, i, 0, tile);
      }
    }
    cout << solve() << endl;
  }

  return 0;
}