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

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int masu[10][10];
vector<pair<int, int>> kitens;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int done[10][10];
int test[10][10];
int n;

int search(int x, int y, int p, int cv) {
  if (cv < 0)
    return 0;
  else if (cv == 0) {
    p++;
    if (p == kitens.size()) {
      return 1;
    }
    cv = -masu[kitens[p].first][kitens[p].second];
    y = kitens[p].first;
    x = kitens[p].second;
  } else {
    if (x < 0 || y < 0 || y >= n || x >= n || done[y][x] || masu[y][x] < 0)
      return 0;
    cv -= masu[y][x];
    if (cv == 0) {
      done[y][x] = true;
      if (search(x, y, p, cv))
        return true;
      done[y][x] = false;
      return 0;
    }
  }
  if (cv < 0)
    return 0;
  done[y][x] = true;

  for (int i = 0; i < 4; i++) {
    if (search(x + dx[i], y + dy[i], p, cv))
      return 1;
  }
  done[y][x] = false;
  return 0;
}
int main() {
  while (cin >> n && n) {
    kitens.clear();
    int checksum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        done[i][j] = 0;
        cin >> masu[i][j];
        if (masu[i][j] < 0) {
          kitens.push_back(make_pair(i, j));
        }
        checksum += masu[i][j];
      }
    }

    if (checksum != 0 || !search(-1, -1, -1, 0))
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int masu[10][10];
vector<pair<int, int>> kitens;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int done[10][10];
int test[10][10];
int n;

int badSituation(int x, int y, int p) {
  stack<pair<int, int>> S;
  memcpy(test, done, sizeof(test));
  S.push(make_pair(y, x));
  test[y][x] = true;
  for (int i = p + 1; i < kitens.size(); i++) {
    S.push(kitens[i]);
    test[kitens[i].first][kitens[i].second] = true;
  }

  while (S.size()) {
    pair<int, int> pt = S.top();
    S.pop();

    for (int i = 0; i < 4; i++) {
      int tx = pt.second + dx[i];
      int ty = pt.first + dy[i];
      if (tx < 0 || ty < 0 || ty >= n || tx >= n || test[ty][tx])
        continue;
      test[ty][tx] = true;
      S.push(make_pair(ty, tx));
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (!test[i][j])
        return true;
  return false;
}
int search(int x, int y, int p, int cv) {
  if (cv < 0)
    return 0;
  else if (cv == 0) {
    p++;
    if (p == kitens.size()) {
      return 1;
    }
    cv = -masu[kitens[p].first][kitens[p].second];
    y = kitens[p].first;
    x = kitens[p].second;
  } else {
    if (x < 0 || y < 0 || y >= n || x >= n || done[y][x] || masu[y][x] < 0)
      return 0;
    cv -= masu[y][x];
  }
  if (cv < 0)
    return 0;
  if (badSituation(x, y, p)) {
    return 0;
  }
  done[y][x] = true;
  for (int i = 0; i < 4; i++) {
    if (search(x + dx[i], y + dy[i], p, cv))
      return 1;
  }
  done[y][x] = false;
  return 0;
}
int main() {
  while (cin >> n && n) {
    kitens.clear();
    int checksum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        done[i][j] = 0;
        cin >> masu[i][j];
        if (masu[i][j] < 0) {
          kitens.push_back(make_pair(i, j));
        }
        checksum += masu[i][j];
      }
    }

    if (checksum != 0 || !search(-1, -1, -1, 0))
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
}