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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
clock_t start, end;
using namespace std;

int a[10][10];

vector<pair<int, int>> v;
vector<int> sz;

int dx[][13] = {{},
                {0, 0, 1, 0, -1},
                {-1, -1, -1, 0, 0, 0, 1, 1, 1},
                {-1, -1, -1, 0, 0, 0, 1, 1, 1, 2, 0, -2, 0}};
int dy[][13] = {{},
                {0, 1, 0, -1, 0},
                {-1, 0, 1, -1, 0, 1, -1, 0, 1},
                {-1, 0, 1, -1, 0, 1, -1, 0, 1, 0, 2, 0, -2}};
int szz[4] = {0, 5, 9, 13};

void Output() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++)
      cout << a[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

vector<pair<int, int>> indexes;

int dfs(int w) {
  if (w == 0) {
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++) {
        if (a[i][j] > 0)
          return 0;
      }
    for (int i = 0; i < v.size(); i++)
      cout << v[i].first << " " << v[i].second << " " << sz[i] << endl;
    exit(0);
  }

  int chk[10][20] = {};
  vector<pair<pair<int, int>, int>> good;
  vector<int> idx[10][10];
  random_shuffle(indexes.begin(), indexes.end());
  int sum = 0;
  // for(int i = 0 ; i < 10 ; i++){
  //    for(int j = 0 ; j < 10 ; j++){
  for (int oh = 0; oh < indexes.size(); oh++) {
    int i = indexes[oh].first;
    int j = indexes[oh].second;
    sum += a[i][j];
    for (int t = 3; t >= 1; t--) {
      int f = 1;
      for (int k = 0; k < szz[t]; k++) {

        int tx = j - dx[t][k];
        int ty = i - dy[t][k];
        if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10 || a[ty][tx] == 0)
          f = 0;
      }

      if (f) {
        good.push_back(make_pair(make_pair(j, i), t));
        for (int k = 0; k < szz[t]; k++) {
          int tx = j - dx[t][k];
          int ty = i - dy[t][k];
          chk[ty][tx]++;
          if (idx[ty][tx].size() < 2)
            idx[ty][tx].push_back(good.size() - 1);
        }
      }
    }
  }
  if (sum > 13 * w)
    return 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (a[i][j] && !chk[i][j])
        return 0;
    }
  }
  // Output();
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (chk[i][j] == 1) {
        int _ = idx[i][j][0];
        int I = good[_].first.second;
        int J = good[_].first.first;
        int t = good[_].second;

        for (int k = 0; k < szz[t]; k++) {
          int tx = J - dx[t][k];
          int ty = I - dy[t][k];
          a[ty][tx]--;
        }
        v.push_back(make_pair(J, I));
        sz.push_back(t);
        dfs(w - 1);
        v.pop_back();
        sz.pop_back();
        for (int k = 0; k < szz[t]; k++) {
          int tx = J - dx[t][k];
          int ty = I - dy[t][k];
          a[ty][tx]++;
        }
        return 0;
      }
    }
  }

  for (int _ = 0; _ < good.size(); _++) {
    int i = good[_].first.second;
    int j = good[_].first.first;
    int t = good[_].second;

    for (int k = 0; k < szz[t]; k++) {
      int tx = j - dx[t][k];
      int ty = i - dy[t][k];
      a[ty][tx]--;
    }
    v.push_back(make_pair(j, i));
    sz.push_back(t);
    dfs(w - 1);
    v.pop_back();
    sz.pop_back();
    for (int k = 0; k < szz[t]; k++) {
      int tx = j - dx[t][k];
      int ty = i - dy[t][k];
      a[ty][tx]++;
    }
  }
}

int main() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      indexes.push_back(make_pair(i, j));
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++)
      cin >> a[i][j];
  }
  dfs(n);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
clock_t start, end;
using namespace std;

int a[10][10];

vector<pair<int, int>> v;
vector<int> sz;

int dx[][13] = {{},
                {0, 0, 1, 0, -1},
                {-1, -1, -1, 0, 0, 0, 1, 1, 1},
                {-1, -1, -1, 0, 0, 0, 1, 1, 1, 2, 0, -2, 0}};
int dy[][13] = {{},
                {0, 1, 0, -1, 0},
                {-1, 0, 1, -1, 0, 1, -1, 0, 1},
                {-1, 0, 1, -1, 0, 1, -1, 0, 1, 0, 2, 0, -2}};
int szz[4] = {0, 5, 9, 13};

void Output() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++)
      cout << a[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

vector<pair<int, int>> indexes;

int dfs(int w) {
  if (w == 0) {
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++) {
        if (a[i][j] > 0)
          return 0;
      }
    for (int i = 0; i < v.size(); i++)
      cout << v[i].first << " " << v[i].second << " " << sz[i] << endl;
    exit(0);
  }

  int chk[10][20] = {};
  vector<pair<pair<int, int>, int>> good;
  vector<int> idx[10][10];

  int sum = 0;
  // for(int i = 0 ; i < 10 ; i++){
  //    for(int j = 0 ; j < 10 ; j++){
  for (int oh = 0; oh < indexes.size(); oh++) {
    int i = indexes[oh].first;
    int j = indexes[oh].second;
    sum += a[i][j];
    for (int t = 3; t >= 1; t--) {
      int f = 1;
      for (int k = 0; k < szz[t]; k++) {

        int tx = j - dx[t][k];
        int ty = i - dy[t][k];
        if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10 || a[ty][tx] == 0)
          f = 0;
      }

      if (f) {
        good.push_back(make_pair(make_pair(j, i), t));
        for (int k = 0; k < szz[t]; k++) {
          int tx = j - dx[t][k];
          int ty = i - dy[t][k];
          chk[ty][tx]++;
          if (idx[ty][tx].size() < 2)
            idx[ty][tx].push_back(good.size() - 1);
        }
      }
    }
  }
  if (sum > 13 * w)
    return 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (a[i][j] && !chk[i][j])
        return 0;
    }
  }
  // Output();
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (chk[i][j] == 1) {
        int _ = idx[i][j][0];
        int I = good[_].first.second;
        int J = good[_].first.first;
        int t = good[_].second;

        for (int k = 0; k < szz[t]; k++) {
          int tx = J - dx[t][k];
          int ty = I - dy[t][k];
          a[ty][tx]--;
        }
        v.push_back(make_pair(J, I));
        sz.push_back(t);
        dfs(w - 1);
        v.pop_back();
        sz.pop_back();
        for (int k = 0; k < szz[t]; k++) {
          int tx = J - dx[t][k];
          int ty = I - dy[t][k];
          a[ty][tx]++;
        }
        return 0;
      }
    }
  }

  for (int _ = 0; _ < good.size(); _++) {
    int i = good[_].first.second;
    int j = good[_].first.first;
    int t = good[_].second;

    for (int k = 0; k < szz[t]; k++) {
      int tx = j - dx[t][k];
      int ty = i - dy[t][k];
      a[ty][tx]--;
    }
    v.push_back(make_pair(j, i));
    sz.push_back(t);
    dfs(w - 1);
    v.pop_back();
    sz.pop_back();
    for (int k = 0; k < szz[t]; k++) {
      int tx = j - dx[t][k];
      int ty = i - dy[t][k];
      a[ty][tx]++;
    }
  }
}

int main() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      indexes.push_back(make_pair(i, j));
    }
  }
  random_shuffle(indexes.begin(), indexes.end());
  int n;
  cin >> n;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++)
      cin >> a[i][j];
  }
  dfs(n);
}