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

#include <algorithm>
#include <iostream>
#include <map>
#include <tuple>
using namespace std;

struct State {
  int a[10][10];
};

char c[10][10];
int p[10][10], H, W, mindep, cnts;

int range(int sx, int sy, int gx, int gy) {
  int cnt = 0;
  for (int i = sx; i < gx; i++) {
    for (int j = sy; j < gy; j++) {
      if (c[i][j] == '0')
        cnt++;
    }
  }
  return cnt;
}
int range2(int sx, int sy, int gx, int gy, State G) {
  int cnt = 0;
  for (int i = sx; i < gx; i++) {
    if (G.a[i][sy] == 0)
      cnt++;
  }
  for (int i = sy; i < gy; i++) {
    if (G.a[sx][i] == 0)
      cnt++;
  }
  return cnt;
}

State nurie(State P, int cx, int cy) {
  for (int i = cx; i < cx + p[cx][cy]; i++) {
    for (int j = cy; j < cy + p[cx][cy]; j++)
      P.a[i][j] = 1;
  }
  return P;
}

long long hash_value(State I) {
  long long mod = 2555555555555555555LL, II = 0, J = 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (I.a[i][j] == 1)
        II += J;
      J *= 2;
      if (II >= mod)
        II -= mod;
      if (J >= mod)
        J -= mod;
    }
  }
  return II;
}

map<tuple<int, int, int, long long>, int> M;

void dfs(int cx, int cy, int depth, State S) {
  long long K = hash_value(S);
  if (M[make_tuple(cx, cy, depth, K)] == 1)
    return;
  M[make_tuple(cx, cy, depth, K)] = 1;
  cnts++;
  if (depth == mindep)
    return;
  if (cx == H) {
    mindep = min(mindep, depth);
    return;
  }
  int nx = cx, ny = cy + 1;
  if (ny == W) {
    ny = 0;
    nx++;
  }
  if (c[cx][cy] == '0') {
    dfs(nx, ny, depth, S);
  } else {
    if (S.a[cx][cy] == 0) {
      State V = nurie(S, cx, cy);
      dfs(nx, ny, depth + 1, V);
    } else if (range2(cx, cy, cx + p[cx][cy], cy + p[cx][cy], S) == 0) {
      dfs(nx, ny, depth, S);
    } else {
      dfs(nx, ny, depth, S);
      State V = nurie(S, cx, cy);
      dfs(nx, ny, depth + 1, V);
    }
  }
}

int main() {
  while (true) {
    cin >> W >> H;
    M.clear();
    mindep = (1 << 30);
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 1; k <= min(H - i, W - j); k++) {
          if (range(i, j, i + k, j + k) >= 1)
            break;
          p[i][j] = k;
        }
      }
    }
    State S;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++)
        S.a[i][j] = 0;
    }
    dfs(0, 0, 0, S);
    cout << mindep << endl;
  }
  // cout << "Totalcount : " << cnts << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

struct State {
  int a[10][10];
};

char c[10][10];
int p[10][10], H, W, mindep;

int range(int sx, int sy, int gx, int gy) {
  int cnt = 0;
  for (int i = sx; i < gx; i++) {
    for (int j = sy; j < gy; j++) {
      if (c[i][j] == '0')
        cnt++;
    }
  }
  return cnt;
}
int range2(int sx, int sy, int gx, int gy, State G) {
  int cnt = 0;
  for (int i = sx; i < gx; i++) {
    for (int j = sy; j < gy; j++) {
      if (G.a[i][j] == 0)
        cnt++;
    }
  }
  return cnt;
}

State nurie(State P, int cx, int cy) {
  for (int i = cx; i < cx + p[cx][cy]; i++) {
    for (int j = cy; j < cy + p[cx][cy]; j++)
      P.a[i][j] = 1;
  }
  return P;
}

void dfs(int cx, int cy, int depth, State S) {
  if (depth == mindep)
    return;
  if (cx == H) {
    mindep = min(mindep, depth);
    return;
  }
  int nx = cx, ny = cy + 1;
  if (ny == W) {
    ny = 0;
    nx++;
  }
  if (c[cx][cy] == '0') {
    dfs(nx, ny, depth, S);
  } else {
    if (S.a[cx][cy] == 0) {
      State V = nurie(S, cx, cy);
      dfs(nx, ny, depth + 1, V);
    } else if (range2(cx, cy, cx + p[cx][cy], cy + p[cx][cy], S) == 0) {
      dfs(nx, ny, depth, S);
    } else {
      dfs(nx, ny, depth, S);
      State V = nurie(S, cx, cy);
      dfs(nx, ny, depth + 1, V);
    }
  }
}

int main() {
  while (true) {
    cin >> W >> H;
    mindep = (1 << 30);
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 1; k <= min(H - i, W - j); k++) {
          if (range(i, j, i + k, j + k) >= 1)
            break;
          p[i][j] = k;
        }
      }
    }
    State S;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++)
        S.a[i][j] = 0;
    }
    dfs(0, 0, 0, S);
    cout << mindep << endl;
  }
  return 0;
}