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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define each(i, n) for (auto &i : n)

const int INF = 1e9;

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

signed main() {

  int w, h;
  while (cin >> w >> h && w) {
    typedef pair<int, int> p;

    char d[20][20];
    p s;
    map<p, int> tb;
    rep(y, h) rep(x, w) {
      cin >> d[y][x];
      if (d[y][x] == 'o') {
        s.first = y, s.second = x;
      }
      if (d[y][x] == '*') {
        tb[p(y, x)] = tb.size();
      }
    }

    typedef bitset<10> bit;
    unordered_map<bit, int> m[20][20];
    m[s.first][s.second][bit()] = 0;
    typedef pair<bit, p> sp;
    queue<sp> q;
    q.push(sp(bit(), s));
    int ans = INF;
    while (q.size()) {
      p n = q.front().second;
      auto se = q.front().first;
      q.pop();
      int y = n.first;
      int x = n.second;
      auto nse = se;
      if (d[y][x] == '*') {
        nse[tb[p(y, x)]] = 1;
      }
      if (nse.count() == tb.size()) {
        ans = min(ans, m[y][x][se]);
      }
      rep(i, 4) {
        int ty = y + dy[i];
        int tx = x + dx[i];
        if (ty >= 0 && tx >= 0 && ty < h && tx < w && d[ty][tx] != 'x') {
          if (!m[ty][tx].count(nse) || m[ty][tx][nse] > m[y][x][se] + 1) {
            m[ty][tx][nse] = m[y][x][se] + 1;
            q.push(sp(nse, p(ty, tx)));
          }
        }
      }
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define each(i, n) for (auto &i : n)

const int INF = 1e9;

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

signed main() {

  int w, h;
  while (cin >> w >> h && w) {
    typedef pair<int, int> p;

    char d[20][20];
    p s;
    int sum = 0;
    rep(y, h) rep(x, w) {
      cin >> d[y][x];
      if (d[y][x] == 'o') {
        s.first = y, s.second = x;
      }
      if (d[y][x] == '*') {
        sum++;
      }
    }

    map<set<p>, int> m[20][20];
    m[s.first][s.second][set<p>()] = 0;
    typedef pair<set<p>, p> sp;
    queue<sp> q;
    q.push(sp(set<p>(), s));
    int ans = INF;
    while (q.size()) {
      p n = q.front().second;
      auto se = q.front().first;
      q.pop();
      int y = n.first;
      int x = n.second;
      auto nse = se;
      if (d[y][x] == '*') {
        nse.insert(p(y, x));
      }
      if (nse.size() == sum) {
        ans = min(ans, m[y][x][se]);
      }
      rep(i, 4) {
        int ty = y + dy[i];
        int tx = x + dx[i];
        if (ty >= 0 && tx >= 0 && ty < h && tx < w && d[ty][tx] != 'x') {
          if (!m[ty][tx].count(nse) || m[ty][tx][nse] > m[y][x][se] + 1) {
            m[ty][tx][nse] = m[y][x][se] + 1;
            q.push(sp(nse, p(ty, tx)));
          }
        }
      }
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}