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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
vector<map<vi, int>> dp;
vi tmp;
string tms;
int r, n, m, c, MOD = 100000007;
// map<vi,int>q;
vi q;
vi f(int a) {
  vi out(m);
  rep(i, m) {
    out[m - i - 1] = a % 3;
    a /= 3;
  }
  return out;
}
int ff(vi a) {
  int t = 0;
  rep(i, m) {
    t *= 3;
    t += a[i];
  }
  return t;
}
void dfs(int a) {
  if (a == m) {
    if (r)
      return;
    (q[ff(tmp)] += c) %= MOD;
    return;
  }
  if (tms[a] == '.') {
    if (r && tmp[a])
      return;
    dfs(a + 1);
    return;
  }
  int w = r;
  if (tms[a] - '0' < w)
    return;
  tms[a] -= w;
  rep(i, min(3, tms[a] - '0' + 1)) if (tms[a] - '0' - i < 3) {
    tmp[a] = i;
    r = tms[a] - '0' - i;
    dfs(a + 1);
    tmp[a] = 0;
  }

  tms[a] += w;
}
int main() {
  cin >> n >> m;
  vs in(n);
  rep(i, n) cin >> in[i];
  //	map<vi,int>dp;
  int N = pow(3, m);
  vi dp(N);
  dp[0] = 1;
  rep(i, n) {
    q = vi(N);
    rep(z, N) if (dp[z]) {
      //		for(map<vi,int>::iterator
      //it=dp.begin();it!=dp.end();it++){
      tmp = f(z);
      tms = in[i];
      bool h = true;
      rep(j, m) if (tms[j] != '.') {
        if (tmp[j] > tms[j] - '0')
          h = false;
        else {
          tms[j] -= tmp[j];
          tmp[j] = 0;
        }
      }
      if (!h)
        continue;
      r = 0;
      c = dp[z];
      dfs(0);
    }
    dp = q;
  }
  cout << dp[0] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
vector<map<vi, int>> dp;
vi tmp;
string tms;
int r, n, m, c, MOD = 100000007;
map<vi, int> q;
void dfs(int a) {
  if (a == m) {
    if (r)
      return;
    (q[tmp] += c) %= MOD;
    return;
  }
  if (tms[a] == '.') {
    if (r && tmp[a])
      return;
    dfs(a + 1);
    return;
  }
  int w = r;
  if (tms[a] - '0' < w)
    return;
  tms[a] -= w;
  rep(i, min(3, tms[a] - '0' + 1)) if (tms[a] - '0' - i < 3) {
    tmp[a] = i;
    r = tms[a] - '0' - i;
    dfs(a + 1);
    tmp[a] = 0;
  }

  tms[a] += w;
}
int main() {
  cin >> n >> m;
  vs in(n);
  rep(i, n) cin >> in[i];
  map<vi, int> dp;
  dp[vi(m)] = 1;
  rep(i, n) {
    //		cout<<i<<" "<<dp[i].size()<<endl;
    for (map<vi, int>::iterator it = dp.begin(); it != dp.end(); it++) {
      tmp = it->first;
      tms = in[i];
      bool h = true;
      rep(j, m) if (tms[j] != '.') {
        if (tmp[j] > tms[j] - '0')
          h = false;
        else {
          tms[j] -= tmp[j];
          tmp[j] = 0;
        }
      }
      if (!h)
        continue;
      r = 0;
      c = it->second;
      dfs(0);
    }
    dp = q;
    q.clear();
  }
  cout << dp[vi(m)] << endl;
}