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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
using namespace std;

struct aa {
  int x;
  int y;
  int t;
  aa(int a, int b, int c) {
    x = a;
    y = b;
    t = c;
  }
};

int inks[3][5][5] = {
    {
        {0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {1, 1, 1, 1, 1},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
    },
};

bool ok(const vector<vector<int>> &v, int ink_x, int ink_y, int ink_id) {
  for (int dy = 0; dy < 5; ++dy) {
    for (int dx = 0; dx < 5; ++dx) {
      if (inks[ink_id][dy][dx]) {
        int ax = ink_x - 2 + dx;
        int ay = ink_y - 2 + dy;

        if (ax < 0 || ax >= 10 || ay < 0 || ay >= 10)
          return false;
        if (v[ay][ax] <= 0)
          return false;
      }
    }
  }
  return true;
}

vector<aa> solve(int now_x, int now_y, int rest, vector<vector<int>> &v) {
  if (rest == 0) {
    bool ok = true;
    for (int i = 0; i < 10; ++i) {
      for (int j = 0; j < 10; ++j) {
        if (v[i][j])
          ok = false;
      }
    }
    aa k = aa(-1, -1, -1);
    if (ok)
      return vector<aa>();
    else
      return vector<aa>(1, aa(-1, -1, -1));
  }
  if (now_y == 10) {
    return vector<aa>(1, aa(-1, -1, -1));
  }
  if (now_y >= 3 && now_x == 0) {
    for (int y = 0; y < now_y - 2; ++y) {
      for (int x = 0; x < 10; ++x) {
        if (v[y][x])
          return vector<aa>(1, aa(-1, -1, -1));
      }
    }
  }

  for (int ink_id = 0; ink_id <= 2; ++ink_id) {
    if (ok(v, now_x, now_y, ink_id)) {
      for (int dy = 0; dy < 5; ++dy) {
        for (int dx = 0; dx < 5; ++dx) {
          if (inks[ink_id][dy][dx]) {
            int ax = now_x - 2 + dx;
            int ay = now_y - 2 + dy;

            if (ax < 0 || ax >= 10 || ay < 0 || ay >= 10)
              assert(false);
            if (v[ay][ax] <= 0)
              assert(false);

            v[ay][ax]--;
          }
        }
      }
      vector<aa> aas = solve(now_x, now_y, rest - 1, v);
      for (int dy = 0; dy < 5; ++dy) {
        for (int dx = 0; dx < 5; ++dx) {
          if (inks[ink_id][dy][dx]) {
            int ax = now_x - 2 + dx;
            int ay = now_y - 2 + dy;
            v[ay][ax]++;
          }
        }
      }
      if (aas.size() == 1 && aas[0].x == -1)
        continue;
      else {
        aas.push_back(aa(now_x, now_y, ink_id));
        return aas;
      }
    }
  }
  int next_x = now_x + 1;
  int next_y = now_y;
  if (next_x == 10) {
    next_x = 0;
    next_y = next_y + 1;
  }
  return solve(next_x, next_y, rest, v);
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> v(10, vector<int>(10));
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cin >> v[i][j];
    }
  }
  auto p = solve(0, 0, N, v);
  for (int i = 0; i < p.size(); ++i) {
    cout << p[i].x << " " << p[i].y << " " << p[i].t + 1 << endl;
  }
  // cout<<"0"<<endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
using namespace std;

struct aa {
  int x;
  int y;
  int t;
  aa(int a, int b, int c) {
    x = a;
    y = b;
    t = c;
  }
};

int inks[3][5][5] = {
    {
        {0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {1, 1, 1, 1, 1},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
    },
};

bool ok(const vector<vector<int>> &v, int ink_x, int ink_y, int ink_id) {
  for (int dy = 0; dy < 5; ++dy) {
    for (int dx = 0; dx < 5; ++dx) {
      if (inks[ink_id][dy][dx]) {
        int ax = ink_x - 2 + dx;
        int ay = ink_y - 2 + dy;

        if (ax < 0 || ax >= 10 || ay < 0 || ay >= 10)
          return false;
        if (v[ay][ax] <= 0)
          return false;
      }
    }
  }
  return true;
}

vector<aa> solve(int now_x, int now_y, int rest, vector<vector<int>> &v) {
  if (rest == 0) {
    bool ok = true;
    for (int i = 0; i < 10; ++i) {
      for (int j = 0; j < 10; ++j) {
        if (v[i][j])
          ok = false;
      }
    }
    aa k = aa(-1, -1, -1);
    if (ok)
      return vector<aa>();
    else
      return vector<aa>(1, aa(-1, -1, -1));
  }
  if (now_y == 10) {
    return vector<aa>(1, aa(-1, -1, -1));
  }

  for (int ink_id = 0; ink_id <= 2; ++ink_id) {
    if (ok(v, now_x, now_y, ink_id)) {
      for (int dy = 0; dy < 5; ++dy) {
        for (int dx = 0; dx < 5; ++dx) {
          if (inks[ink_id][dy][dx]) {
            int ax = now_x - 2 + dx;
            int ay = now_y - 2 + dy;

            if (ax < 0 || ax >= 10 || ay < 0 || ay >= 10)
              assert(false);
            if (v[ay][ax] <= 0)
              assert(false);

            v[ay][ax]--;
          }
        }
      }
      vector<aa> aas = solve(now_x, now_y, rest - 1, v);
      for (int dy = 0; dy < 5; ++dy) {
        for (int dx = 0; dx < 5; ++dx) {
          if (inks[ink_id][dy][dx]) {
            int ax = now_x - 2 + dx;
            int ay = now_y - 2 + dy;
            v[ay][ax]++;
          }
        }
      }
      if (aas.size() == 1 && aas[0].x == -1)
        continue;
      else {
        aas.push_back(aa(now_x, now_y, ink_id));
        return aas;
      }
    }
  }
  int next_x = now_x + 1;
  int next_y = now_y;
  if (next_x == 10) {
    next_x = 0;
    next_y = next_y + 1;
  }
  return solve(next_x, next_y, rest, v);
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> v(10, vector<int>(10));
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cin >> v[i][j];
    }
  }
  auto p = solve(0, 0, N, v);
  for (int i = 0; i < p.size(); ++i) {
    cout << p[i].x << " " << p[i].y << " " << p[i].t + 1 << endl;
  }
  // cout<<"0"<<endl;
  return 0;
}