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

#include <cstdio>
#include <cstdlib>
#include <stack>
#include <utility>
using namespace std;

typedef pair<int, int> Pair;
typedef pair<int, Pair> Data;

int n;
int total;
int field[10][10];
stack<Data> ans;

int ex[] = {9, 0, 0, 9};
int ey[] = {0, 9, 0, 9};
int fx[] = {8, 1, 1, 8};
int fy[] = {1, 8, 1, 8};
int dx1[] = {-1, 0, 0, 0, 1};
int dy1[] = {0, -1, 0, 1, 0};
int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dy2[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dx3[] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int dy3[] = {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0};
int used[10][10];

bool CheckPlace(int y, int x, int type) {
  if (type == 1) {
    for (int i = 0; i < 5; i++) {
      int newy = y + dy1[i];
      int newx = x + dx1[i];
      if (field[newy][newx] == 0)
        return false;
    }
  } else if (type == 2) {
    for (int i = 0; i < 9; i++) {
      int newy = y + dy2[i];
      int newx = x + dx2[i];
      if (field[newy][newx] == 0)
        return false;
    }
  } else if (type == 3) {
    for (int i = 0; i < 13; i++) {
      int newy = y + dy3[i];
      int newx = x + dx3[i];
      if (field[newy][newx] == 0)
        return false;
    }
  }

  return true;
}

void AddPlace(int (&field)[10][10], int y, int x, int type, int t) {
  if (type == 1) {
    for (int i = 0; i < 5; i++) {
      int newy = y + dy1[i];
      int newx = x + dx1[i];
      field[newy][newx] += t;
    }
  } else if (type == 2) {
    for (int i = 0; i < 9; i++) {
      int newy = y + dy2[i];
      int newx = x + dx2[i];
      field[newy][newx] += t;
    }
  } else if (type == 3) {
    for (int i = 0; i < 13; i++) {
      int newy = y + dy3[i];
      int newx = x + dx3[i];
      field[newy][newx] += t;
    }
  }
}

bool CheckImpossible() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      used[i][j] = false;
    }
  }

  for (int i = 1; i < 9; i++) {
    for (int j = 1; j < 9; j++) {
      if (i > 1 && i < 8 && j < 8 && j > 1 && CheckPlace(i, j, 3)) {
        AddPlace(used, i, j, 3, 1);
      } else if (CheckPlace(i, j, 2)) {
        AddPlace(used, i, j, 2, 1);
      } else if (CheckPlace(i, j, 1)) {
        AddPlace(used, i, j, 1, 1);
      }
    }
  }

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (field[i][j] && !used[i][j])
        return true;
    }
  }

  return false;
}
void check(int x, int y, int z) {
  if (CheckImpossible())
    return;

  if (y > 0) {
    for (int i = 1; i < 9; i++) {
      for (int j = 1; j < 9; j++) {
        if (CheckPlace(i, j, 2)) {
          AddPlace(field, i, j, 2, -1);
          ans.push(Data(2, Pair(i, j)));
          check(x, y - 1, z);
          ans.pop();
          AddPlace(field, i, j, 2, 1);
        }
      }
    }

    return;
  }

  if (z > 0) {
    for (int i = 2; i < 8; i++) {
      for (int j = 2; j < 8; j++) {
        if (CheckPlace(i, j, 3)) {
          AddPlace(field, i, j, 3, -1);
          ans.push(Data(3, Pair(i, j)));
          check(x, y, z - 1);
          ans.pop();
          AddPlace(field, i, j, 3, 1);
        }
      }
    }

    return;
  }

  if (x > 0) {
    for (int i = 1; i < 9; i++) {
      for (int j = 1; j < 9; j++) {
        if (CheckPlace(i, j, 1)) {
          AddPlace(field, i, j, 1, -1);
          ans.push(Data(1, Pair(i, j)));
          check(x - 1, y, z);
          ans.pop();
          AddPlace(field, i, j, 1, 1);
        }
      }
    }

    return;
  }

  while (!ans.empty()) {
    Data d = ans.top();
    ans.pop();
    int level = d.first;
    Pair p = d.second;
    printf("%d %d %d\\n", p.second, p.first, level);
  }

  exit(0);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      scanf("%d", &field[i][j]);
      total += field[i][j];
    }
  }

  for (int i = 0; i < 4; i++) {
    int y = ey[i];
    int x = ex[i];

    if (field[y][x]) {
      total -= field[y][x] * 9;
      n -= field[y][x];
      for (int j = 0; j < field[y][x]; j++) {
        ans.push(Data(2, Pair(fy[i], fx[i])));
      }
      AddPlace(field, fy[i], fx[i], 2, -field[y][x]);
    }
  }

  int x = 0;
  int y = (13 * n - total) / 4;
  while (y >= 0) {
    int z = n - (x + y);
    if (z < 0) {
      y -= 2;
      x += 1;
      continue;
    }

    check(x, y, z);
    y -= 2;
    x += 1;
  }

  while (!ans.empty()) {
    Data d = ans.top();
    ans.pop();
    int level = d.first;
    Pair p = d.second;
    printf("%d %d %d\\n", p.second, p.first, level);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <stack>
#include <utility>
using namespace std;

typedef pair<int, int> Pair;
typedef pair<int, Pair> Data;

int n;
int total;
int field[10][10];
stack<Data> ans;

bool AddPlace(int y, int x, int type, int t) {
  bool ret = true;

  if (type == 1) {
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i) + abs(j) >= 2)
          continue;
        int newy = y + i;
        int newx = x + j;
        field[newy][newx] += t;
        if (field[newy][newx] < 0)
          ret = false;
      }
    }
  } else if (type == 2) {
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        int newy = y + i;
        int newx = x + j;
        field[newy][newx] += t;
        if (field[newy][newx] < 0)
          ret = false;
      }
    }
  } else if (type == 3) {
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) >= 3)
          continue;
        int newy = y + i;
        int newx = x + j;
        field[newy][newx] += t;
        if (field[newy][newx] < 0)
          ret = false;
      }
    }
  }

  return ret;
}

void check(int x, int y, int z) {
  if (x > 0) {
    for (int i = 1; i < 9; i++) {
      for (int j = 1; j < 9; j++) {
        if (AddPlace(i, j, 1, -1)) {
          ans.push(Data(1, Pair(i, j)));
          check(x - 1, y, z);
          ans.pop();
        }

        AddPlace(i, j, 1, 1);
      }
    }

    return;
  }

  if (y > 0) {
    for (int i = 1; i < 9; i++) {
      for (int j = 1; j < 9; j++) {
        if (AddPlace(i, j, 2, -1)) {
          ans.push(Data(2, Pair(i, j)));
          check(x, y - 1, z);
          ans.pop();
        }

        AddPlace(i, j, 2, 1);
      }
    }

    return;
  }

  if (z > 0) {
    for (int i = 2; i < 8; i++) {
      for (int j = 2; j < 8; j++) {
        if (AddPlace(i, j, 3, -1)) {
          ans.push(Data(3, Pair(i, j)));
          check(x, y, z - 1);
          ans.pop();
        }

        AddPlace(i, j, 3, 1);
      }
    }

    return;
  }

  while (!ans.empty()) {
    Data d = ans.top();
    ans.pop();
    int level = d.first;
    Pair p = d.second;
    printf("%d %d %d\\n", p.second, p.first, level);
  }

  exit(0);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      scanf("%d", &field[i][j]);
      total += field[i][j];
    }
  }

  int x = 0;
  int y = (13 * n - total) / 4;
  while (y >= 0) {
    int z = n - (x + y);
    if (z < 0) {
      y -= 2;
      x += 1;
      continue;
    }

    check(x, y, z);
    y -= 2;
    x += 1;
  }
}