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

//=================================
// Created on: 2018/07/04 14:05:12->14:07; 14:22->
//=================================
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int W, H;;) {
    cin >> W >> H;
    if (W == 0 and H == 0) {
      break;
    }
    using Map = vector<int>;
    Map f(H, 0);
    int sy, sx, gy, gx;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        int c;
        cin >> c;
        if (c == 1) {
          f[i] |= (1 << j);
        }
        if (c == 2) {
          sy = i, sx = j;
        }
        if (c == 3) {
          gy = i, gx = j;
        }
      }
    }
    vector<vector<vector<map<Map, int>>>> memo(
        10, vector<vector<map<Map, int>>>(H, vector<map<Map, int>>(W)));
    constexpr int INF = 1 << 30;
    constexpr int d[] = {1, 0, -1, 0, 1};
    auto in = [&](const int y, const int x) {
      return y >= 0 and y < H and x >= 0 and x < W;
    };
    auto rec = [&, d](auto &&self, const int moved, const int y, const int x,
                      const Map &map) -> int {
      if (y == gy and x == gx) {
        return 0;
      }
      if (moved == 10) {
        return INF;
      }
      if (memo[moved][y][x].find(map) != memo[moved][y][x].end()) {
        return memo[moved][y][x][map];
      }
      int ans = INF;
      for (int di = 0; di < 4; di++) {
        const int dy = d[di], dx = d[di + 1];
        int newy = y + dy, newx = x + dx;
        if (not in(newy, newx) or (map[newy] & (1 << newx))) {
          continue;
        }
        for (; in(newy, newx) and not(map[newy] & (1 << newx)) and
               not(newy == gy and newx == gx);
             newy += dy, newx += dx) {
        }
        if (not in(newy, newx)) {
          continue;
        }
        if (newy == gy and newx == gx) {
          return memo[moved][y][x][map] = 1;
        }
        Map newmap = map;
        newmap[newy] -= (1 << newx);
        newy -= dy, newx -= dx;
        ans = min(ans, self(self, moved + 1, newy, newx, newmap) + 1);
      }
      return memo[moved][y][x][map] = ans;
    };
    const int ans = rec(rec, 0, sy, sx, f);
    cout << (ans == INF ? -1 : ans) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

//=================================
// Created on: 2018/07/04 14:05:12->14:07; 14:22->
//=================================
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int W, H;;) {
    cin >> W >> H;
    if (W == 0 and H == 0) {
      break;
    }
    using Map = vector<vector<bool>>;
    Map f(H, vector<bool>(W, false));
    int sy, sx, gy, gx;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        int c;
        cin >> c;
        if (c == 1) {
          f[i][j] = true;
        }
        if (c == 2) {
          sy = i, sx = j;
        }
        if (c == 3) {
          gy = i, gx = j;
        }
      }
    }
    vector<vector<vector<map<Map, int>>>> memo(
        10, vector<vector<map<Map, int>>>(H, vector<map<Map, int>>(W)));
    constexpr int INF = 1 << 30;
    constexpr int d[] = {1, 0, -1, 0, 1};
    auto in = [&](const int y, const int x) {
      return y >= 0 and y < H and x >= 0 and x < W;
    };
    auto rec = [&, d](auto &&self, const int moved, const int y, const int x,
                      const Map &map) -> int {
      if (y == gy and x == gx) {
        return 0;
      }
      if (moved == 10) {
        return INF;
      }
      if (memo[moved][y][x].find(map) != memo[moved][y][x].end()) {
        return memo[moved][y][x][map];
      }
      int ans = INF;
      for (int di = 0; di < 4; di++) {
        const int dy = d[di], dx = d[di + 1];
        int newy = y + dy, newx = x + dx;
        if (not in(newy, newx) or map[newy][newx]) {
          continue;
        }
        for (; in(newy, newx) and not map[newy][newx] and
               not(newy == gy and newx == gx);
             newy += dy, newx += dx) {
        }
        if (not in(newy, newx)) {
          continue;
        }
        if (newy == gy and newx == gx) {
          return memo[moved][y][x][map] = 1;
        }
        Map newmap = map;
        newmap[newy][newx] = false;
        newy -= dy, newx -= dx;
        ans = min(ans, self(self, moved + 1, newy, newx, newmap) + 1);
      }
      return memo[moved][y][x][map] = ans;
    };
    const int ans = rec(rec, 0, sy, sx, f);
    cout << (ans == INF ? -1 : ans) << endl;
  }
  return 0;
}