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

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int vis[10], ans[10], vis1[10];
int a[100], b[100], k = 0;
char mp[10][10];
struct note {
  int x, y;
} l[1000];
int check(int x, int y) {
  for (int i = 0; i < k; i++) {
    if (l[i].x == x || l[i].y == y || abs(x - l[i].x) == abs(y - l[i].y))
      return 0;
  }
  return 1;
}
void pri() {
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++)
      mp[i][j] = '.';
  }
  for (int i = 0; i < k; i++)
    mp[l[i].x][l[i].y] = 'Q';
  for (int i = 1; i < 9; i++) {
    for (int j = 1; j < 9; j++)
      printf("%c", mp[i][j]);
    printf("\\n");
  }
}
void dfs(int x) {
  if (x > 8) {
    pri();
    return;
  } else if (vis[x] == 0) {
    for (int j = 1; j <= 8; j++) {
      if (vis1[j] == 0 && check(x, j)) {
        l[k].x = x;
        l[k++].y = j;
        vis1[j] = 1;
        dfs(x + 1);
        vis1[j] = 0;
        k--;
      }
    }
  } else
    dfs(x + 1);
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    memset(vis, 0, sizeof(vis));
    memset(vis1, 0, sizeof(vis1));
    k = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &a[i], &b[i]);
      vis[a[i] + 1] = 1;
      vis1[b[i] + 1] = 1;
      l[k].x = a[i] + 1;
      l[k++].y = b[i] + 1;
    }
    dfs(1);
  }
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
using namespace std;

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

bool flag = false;

void dfs(char table[8][8], int k, int i, int j) {
  int m, n;
  if (k == 7) {
    table[i][j] = 'Q';
    for (m = 0; m < 8; m++) {
      for (n = 0;; n++) {
        if (i + n * dx[m] < 0 || i + n * dx[m] > 8 || j + n * dy[m] < 0 ||
            j + n * dy[m] > 8)
          break;
        if (table[i + n * dx[m]][j + n * dy[m]] != 'Q') {
          table[i + n * dx[m]][j + n * dy[m]] = '.';
        }
      }
    }
    for (m = 0; m < 8; m++) {
      for (n = 0; n < 8; n++) {
        printf("%c", table[m][n]);
      }
      printf("\\n");
    }
    flag = true;
    return;
  }
  char copytable[8][8] = {0};
  for (m = 0; m < 8; m++) {
    for (n = 0; n < 8; n++) {
      copytable[m][n] = table[m][n];
    }
  }
  copytable[i][j] = 'Q';

  for (m = 0; m < 8; m++) {
    for (n = 0;; n++) {
      if (i + n * dx[m] < 0 || i + n * dx[m] > 8 || j + n * dy[m] < 0 ||
          j + n * dy[m] > 8)
        break;
      if (copytable[i + n * dx[m]][j + n * dy[m]] != 'Q') {
        copytable[i + n * dx[m]][j + n * dy[m]] = '.';
      }
    }
  }
  for (m = 0; m < 8; m++) {
    for (n = 0; n < 8; n++) {
      if (flag == false) {
        if (copytable[m][n] == '0') {
          dfs(copytable, k + 1, m, n);
        }
      }
    }
  }
}

int main() {
  int k, c, r;
  int i, j, count;
  char table[8][8] = {0};
  memset(table, '0', sizeof(table));
  scanf("%d", &k);
  count = k;
  while (count--) {
    scanf("%d%d", &c, &r);
    for (i = 0; i < 8; i++) {
      for (j = 0;; j++) {
        if (c + j * dx[i] < 0 || c + j * dx[i] >= 8 || r + j * dy[i] < 0 ||
            r + j * dy[i] >= 8)
          break;
        table[c + j * dx[i]][r + j * dy[i]] = '.';
      }
    }
    table[c][r] = 'Q';
  }
  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      if (table[i][j] == '0') {
        dfs(table, k, i, j);
        if (flag == true) {
          goto end;
        }
      }
    }
  }
end:;

  return 0;
}