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

#include <iostream>
#include <stdlib.h>
using namespace std;

#define r(a) for (int a = 0; a < 10; a++)

int N, x[15], y[15], s[15], c[10][10];

bool paint(int i, int j, int s, int x, int v) {
  if (s == 1)
    for (int p = i - 1; p <= i + 1; p++)
      for (int q = j - 1; q <= j + 1; q++) {
        if (abs(p - i) + abs(q - j) == 2)
          continue;
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 2)
    for (int p = i; p <= i + 2; p++)
      for (int q = j; q <= j + 2; q++) {
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 3)
    for (int p = i - 2; p <= i + 2; p++)
      for (int q = j - 2; q <= j + 2; q++) {
        if (abs(p - i) + abs(q - j) > 2)
          continue;
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  return 1;
}
bool dfs(int n) {
  if (n == N) {
    r(i) r(j) if (c[i][j]) return 0;
    for (int i = 0; i < n; i++)
      cout << x[i] << " " << y[i] << " " << s[i] << endl;
    return 1;
  }
  r(i) r(j) {
    if (!c[i][j])
      continue;
    if (j > 0 && j < 9 && i < 8)
      if (paint(i + 1, j, 1, 0, 0)) {
        paint(i + 1, j, 1, -1, 1);
        x[n] = j, y[n] = i + 1, s[n] = 1;
        if (dfs(n + 1))
          return 1;
        paint(i + 1, j, 1, 1, 1);
      }
    if (i < 8 && j < 8)
      if (paint(i, j, 2, 0, 0)) {
        paint(i, j, 2, -1, 1);
        x[n] = j + 1, y[n] = i + 1, s[n] = 2;
        if (dfs(n + 1))
          return 1;
        paint(i, j, 2, 1, 1);
      }
    if (j > 1 && j < 8 && i < 6)
      if (paint(i + 2, j, 3, 0, 0)) {
        paint(i + 2, j, 3, -1, 1);
        x[n] = j, y[n] = i + 2, s[n] = 3;
        if (dfs(n + 1))
          return 1;
        paint(i + 2, j, 3, 1, 1);
      }
    return 0;
  }
  return 0;
}
int main() {
  cin >> N;
  r(a) r(b) cin >> c[a][b];
  dfs(0);
}

Error: MLE

Fixed submission:

// #include<iostream>
// #include<vector>
// #include<set>
// #include<stdlib.h>
// using namespace std;
// #define r(a) for(int a=0;a<10;a++)
//
// int c[10][10];
// void paint(int i,int j,int s,int x){
//     if(s==1){
//         for(int p=i-1;p<=i+1;p++){
//             for(int q=j-1;q<=j+1;q++){
//                 if(abs(p-i)+abs(q-j)==2)continue;
//                 c[i][j]+=x;
//             }
//         }
//     }
//     if(s==2){
//         for(int p=i-1;p<=i+1;p++){
//             for(int q=j-1;q<=j+1;q++){
//                 c[i][j]+=x;
//             }
//         }
//     }
//     if(s==3){
//         for(int p=i-2;p<=i+2;p++){
//             for(int q=j-2;q<=j+2;q++){
//                 if(abs(p-i)+abs(q-j)>2)continue;
//                 c[i][j]+=x;
//             }
//         }
//     }
// }
// bool dfs(int i){
//     for(int j=0;j<10;j++){
//         if(!c[i][j])continue;
//         if(c[i][j]&&c[i+1][j+1]&&c[i-1][j+1]&&c[i][j+1]&&c[i][j+2]){
//
//         }
//     }
//     return 0;
// }
// int main(){
//     int n;
//     cin>>n;
//     r(a)r(b)cin>>c[a][b];
//     dfs(0);
// }
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define r(a) for (int a = 0; a < 10; a++)

struct pain {
  short int x, y, s, t;
};
vector<pain> vec;
set<vector<short int>> se;

short int n;
vector<vector<short int>> c(10, vector<short int>(10, 0));

short int s(short int i, short int j) {
  if (!i || !j || i == 9 || j == 9)
    return 0;
  return min(c[i][j],
             min(c[i - 1][j], min(c[i + 1][j], min(c[i][j + 1], c[i][j - 1]))));
  return 0;
}
short int m(short int i, short int j) {
  return min(s(i, j),
             min(c[i - 1][j - 1],
                 min(c[i - 1][j + 1], min(c[i + 1][j - 1], c[i + 1][j + 1]))));
}
short int l(short int i, short int j) {
  return min(m(i, j),
             min(c[i - 2][j], min(c[i + 2][j], min(c[i][j - 2], c[i][j + 2]))));
}

void ds(short int i, short int j, short int ma) {
  c[i][j] -= ma;
  c[i - 1][j] -= ma;
  c[i + 1][j] -= ma;
  c[i][j + 1] -= ma;
  c[i][j - 1] -= ma;
}
void dm(short int i, short int j, short int ma) {
  c[i - 1][j - 1] -= ma;
  c[i - 1][j + 1] -= ma;
  c[i + 1][j - 1] -= ma;
  c[i + 1][j + 1] -= ma;
}
void dl(short int i, short int j, short int ma) {
  c[i - 2][j] -= ma;
  c[i + 2][j] -= ma;
  c[i][j - 2] -= ma;
  c[i][j + 2] -= ma;
}

void is(short int i, short int j, short int ma) {
  c[i][j] += ma;
  c[i - 1][j] += ma;
  c[i + 1][j] += ma;
  c[i][j + 1] += ma;
  c[i][j - 1] += ma;
}
void im(short int i, short int j, short int ma) {
  c[i - 1][j - 1] += ma;
  c[i - 1][j + 1] += ma;
  c[i + 1][j - 1] += ma;
  c[i + 1][j + 1] += ma;
}
void il(short int i, short int j, short int ma) {
  c[i - 2][j] += ma;
  c[i + 2][j] += ma;
  c[i][j - 2] += ma;
  c[i][j + 2] += ma;
}

bool eval() {
  int suc = 1;
  r(a) r(b) if (c[a][b]) suc = 0;
  if (suc) {
    for (int k = 0; k < vec.size(); k++)
      while (vec[k].t--)
        cout << vec[k].y << " " << vec[k].x << " " << vec[k].s << endl;
    return 1;
  }
  return 0;
}
bool eva(short int i, short int j) {
  int suc = 1;
  for (int a = 0; a < i - 2; a++)
    for (int b = 0; b < j - 2; b++)
      if (c[a][b])
        suc = 0;
  return suc;
}
vector<short int> tr(vector<vector<short int>> vec) {
  vector<short int> v;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      v.push_back(c[i][j]);
  return v;
}
bool dfs(int i, int j, int r) {
  if (r > n)
    return 0;
  pain tp;
  tp.x = i;
  tp.y = j;
  tp.t = 0;
  if (i > 1 && i < 8 && j > 1 && j < 8) {
    if (l(i, j)) {
      tp.s = 3;
      vec.push_back(tp);
      short int m1 = l(i, j);
      dl(i, j, m1);
      dm(i, j, m1);
      ds(i, j, m1);
      vec[vec.size() - 1].t = m1;
      if (!eva(i, j))
        goto n1;
      if (se.find(tr(c)) != se.end())
        goto n1;

      if (eval())
        return 1;
      for (int a = i; a < 10; a++)
        for (int b = 0; b < 10; b++)
          if (s(a, b) && dfs(a, b, r + 1))
            return 1;
      se.insert(tr(c));
    n1:
      il(i, j, m1);
      im(i, j, m1);
      is(i, j, m1);
      vec.pop_back();
    }
  }
  if (i > 0 && i < 9 && j > 0 && j < 9) {
    if (m(i, j)) {
      tp.s = 2;
      vec.push_back(tp);
      short int m1 = m(i, j);
      dm(i, j, m1);
      ds(i, j, m1);
      vec[vec.size() - 1].t = m1;
      if (!eva(i, j))
        goto n2;
      if (se.find(tr(c)) != se.end())
        goto n2;
      if (eval())
        return 1;
      for (int a = i; a < 10; a++)
        for (int b = 0; b < 10; b++)
          if (s(a, b) && dfs(a, b, r + 1))
            return 1;
      se.insert(tr(c));
    n2:
      im(i, j, m1);
      is(i, j, m1);
      vec.pop_back();
    }
    if (s(i, j)) {

      tp.s = 1;
      vec.push_back(tp);
      short int m1 = s(i, j);
      ds(i, j, m1);
      tp.s = 1;
      vec[vec.size() - 1].t = m1;
      if (!eva(i, j))
        goto n3;
      if (se.find(tr(c)) != se.end())
        goto n3;
      if (eval())
        return 1;
      for (int a = i; a < 10; a++)
        for (int b = 0; b < 10; b++)
          if (s(a, b) && dfs(a, b, r + 1))
            return 1;
      se.insert(tr(c));
    n3:
      is(i, j, m1);
      vec.pop_back();
    }
  }
  return 0;
}
int main() {
  cin >> n;
  r(i) r(j) cin >> c[i][j];
  r(i) r(j) {
    if (s(i, j))
      dfs(i, j, 0);
  }
}