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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string normalize(string s, bool isAdd, bool isUp, bool isLeft) {
  if (isAdd) {
    if (isUp && s[0] != '.')
      replace(s.rbegin(), s.rend(), s[0], 'Z');
    if (isLeft && s[s.size() - 1] != '.')
      replace(s.begin(), s.end(), s[s.size() - 1], 'Z');
    s += 'Z';
  } else {
    if (s[0] != '.' && count(s.begin(), s.end(), '.') == s.size() - 1)
      return "";
    s += '.';
  }
  if (s[0] != '.' && count(s.begin(), s.end(), s[0]) == 1)
    return "";
  s = s.substr(1);

  map<char, char> to;
  for (unsigned i = 0; i < s.size(); ++i) {
    if (s[i] == '.')
      continue;
    if (to.find(s[i]) == to.end()) {
      char c = 'A' + to.size();
      to[s[i]] = c;
    }
    s[i] = to[s[i]];
  }
  return s;
}

int main() {
  for (;;) {
    int h, w;
    cin >> h >> w;
    if (h == 0)
      return 0;
    int sum = h * w;

    vector<vector<int>> chocolate(h, vector<int>(w));
    vector<int> cy, cx;
    map<int, int> yIndex, xIndex;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        cin >> chocolate[y][x];
        if (chocolate[y][x] == 1) {
          cy.push_back(y);
          cx.push_back(x);
          yIndex[y];
          xIndex[x];
        }
      }
    }

    vector<int> dy, dx;
    int prev = 0;
    for (map<int, int>::iterator it = yIndex.begin(); it != yIndex.end();
         ++it) {
      dy.push_back(it->first - prev);
      it->second = dy.size() - 1;
      prev = it->first;
    }
    prev = 0;
    for (map<int, int>::iterator it = xIndex.begin(); it != xIndex.end();
         ++it) {
      dx.push_back(it->first - prev);
      it->second = dx.size() - 1;
      prev = it->first;
    }
    dy[0] = dx[0] = 1;

    h = dy.size();
    w = dx.size();
    chocolate.assign(h, vector<int>(w, 0));
    for (unsigned i = 0; i < cy.size(); ++i)
      chocolate[yIndex[cy[i]]][xIndex[cx[i]]] = 1;

    map<string, int> dp;
    dp[string(w, '.')] = 0;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        map<string, int> nextDp;
        for (map<string, int>::iterator it = dp.begin(); it != dp.end(); ++it) {
          string s = it->first;

          if (chocolate[y][x] == 0) {
            string t = normalize(s, false, false, false);
            if (t != "") {
              if (nextDp.find(t) == nextDp.end())
                nextDp[t] = it->second;
              else
                nextDp[t] = min(nextDp[t], it->second);
            }
          }

          for (int i = 0; i < 4; ++i) {
            bool isUp = i % 2;
            bool isLeft = i / 2;
            if (x == 0 && isLeft)
              continue;

            int cost = 1;
            if (isUp)
              cost += dy[y] - 1;
            if (isLeft)
              cost += dx[x] - 1;

            string t = normalize(s, true, isUp, isLeft);
            if (t != "") {
              if (nextDp.find(t) == nextDp.end())
                nextDp[t] = it->second + cost;
              else
                nextDp[t] = min(nextDp[t], it->second + cost);
            }
          }
        }
        dp.swap(nextDp);
      }
    }

    int ret = INT_MAX;
    for (map<string, int>::iterator it = dp.begin(); it != dp.end(); ++it) {
      string s = it->first;
      if (s.find('B') == string::npos)
        ret = min(ret, it->second);
    }
    ret = sum - ret;
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, -1, 1};

int h, w;
int n;
vector<int> hy, hx;

int solve(bitset<6> used, vector<bitset<12>> &s) {
  if (used.count() == n) {
    int ret = 0;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (!s[y][x])
          ++ret;
      }
    }
    if (ret > 7)
      return 0;
    return ret;
  }

  int ret = 0;
  for (int i = 0; i < n; ++i) {
    if (used[i])
      continue;

    for (int j = 0; j < n; ++j) {
      if (i == j || (!used[j] && j < i))
        continue;
      bitset<6> used2 = used;
      used2[i] = used2[j] = true;

      vector<bitset<12>> t = s;
      for (int y = min(hy[i], hy[j]); y <= max(hy[i], hy[j]); ++y)
        t[y][hx[i]] = true;
      for (int x = min(hx[i], hx[j]); x <= max(hx[i], hx[j]); ++x)
        t[hy[j]][x] = true;
      ret = max(ret, solve(used2, t));

      t = s;
      for (int y = min(hy[i], hy[j]); y <= max(hy[i], hy[j]); ++y)
        t[y][hx[j]] = true;
      for (int x = min(hx[i], hx[j]); x <= max(hx[i], hx[j]); ++x)
        t[hy[i]][x] = true;
      ret = max(ret, solve(used2, t));
    }

    for (int j = 0; j < 4; ++j) {
      int y = hy[i];
      int x = hx[i];
      while (0 <= y && y < h && 0 <= x && x < w && !s[y][x]) {
        y += dy[j];
        x += dx[j];
      }
      if (0 <= y && y < h && 0 <= x && x < w) {
        vector<bitset<12>> t = s;
        y = hy[i];
        x = hx[i];
        while (!t[y][x]) {
          t[y][x] = true;
          y += dy[j];
          x += dx[j];
        }

        bitset<6> used2 = used;
        used2[i] = true;
        ret = max(ret, solve(used2, t));
      }
    }
  }

  return ret;
}

int main() {
  for (;;) {
    cin >> h >> w;
    if (h == 0)
      return 0;

    n = 0;
    hy.clear();
    hx.clear();
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        int a;
        cin >> a;
        if (a == 1) {
          ++n;
          hy.push_back(i);
          hx.push_back(j);
        }
      }
    }

    vector<bitset<12>> s(h, 0);
    cout << solve(0, s) << endl;
  }
}